mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-17 21:06:04 +00:00
added format command and added trailing commas to reduce conflicts
This commit is contained in:
@@ -7,9 +7,9 @@ export const cloudinaryAdapterConfig = Type.Object(
|
||||
cloud_name: Type.String(),
|
||||
api_key: Type.String(),
|
||||
api_secret: Type.String(),
|
||||
upload_preset: Type.Optional(Type.String())
|
||||
upload_preset: Type.Optional(Type.String()),
|
||||
},
|
||||
{ title: "Cloudinary", description: "Cloudinary media storage" }
|
||||
{ title: "Cloudinary", description: "Cloudinary media storage" },
|
||||
);
|
||||
|
||||
export type CloudinaryConfig = Static<typeof cloudinaryAdapterConfig>;
|
||||
@@ -80,7 +80,7 @@ export class StorageCloudinaryAdapter implements StorageAdapter {
|
||||
private getAuthorizationHeader() {
|
||||
const credentials = btoa(`${this.config.api_key}:${this.config.api_secret}`);
|
||||
return {
|
||||
Authorization: `Basic ${credentials}`
|
||||
Authorization: `Basic ${credentials}`,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -102,12 +102,12 @@ export class StorageCloudinaryAdapter implements StorageAdapter {
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Accept: "application/json"
|
||||
Accept: "application/json",
|
||||
// content type must be undefined to use correct boundaries
|
||||
//"Content-Type": "multipart/form-data",
|
||||
},
|
||||
body: formData
|
||||
}
|
||||
body: formData,
|
||||
},
|
||||
);
|
||||
|
||||
if (!result.ok) {
|
||||
@@ -121,8 +121,8 @@ export class StorageCloudinaryAdapter implements StorageAdapter {
|
||||
etag: data.etag,
|
||||
meta: {
|
||||
type: this.getMimeType(data),
|
||||
size: data.bytes
|
||||
}
|
||||
size: data.bytes,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -133,9 +133,9 @@ export class StorageCloudinaryAdapter implements StorageAdapter {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
...this.getAuthorizationHeader()
|
||||
}
|
||||
}
|
||||
...this.getAuthorizationHeader(),
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
if (!result.ok) {
|
||||
@@ -146,7 +146,7 @@ export class StorageCloudinaryAdapter implements StorageAdapter {
|
||||
return data.resources.map((item) => ({
|
||||
key: item.public_id,
|
||||
last_modified: new Date(item.uploaded_at),
|
||||
size: item.bytes
|
||||
size: item.bytes,
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -155,8 +155,8 @@ export class StorageCloudinaryAdapter implements StorageAdapter {
|
||||
return await fetch(url, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Range: "bytes=0-1"
|
||||
}
|
||||
Range: "bytes=0-1",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -172,7 +172,7 @@ export class StorageCloudinaryAdapter implements StorageAdapter {
|
||||
const size = Number(result.headers.get("content-range")?.split("/")[1]);
|
||||
return {
|
||||
type: type as string,
|
||||
size: size
|
||||
size: size,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -182,7 +182,7 @@ export class StorageCloudinaryAdapter implements StorageAdapter {
|
||||
private guessType(key: string): string | undefined {
|
||||
const extensions = {
|
||||
image: ["jpg", "jpeg", "png", "gif", "webp", "svg"],
|
||||
video: ["mp4", "webm", "ogg"]
|
||||
video: ["mp4", "webm", "ogg"],
|
||||
};
|
||||
|
||||
const ext = key.split(".").pop();
|
||||
@@ -199,13 +199,13 @@ export class StorageCloudinaryAdapter implements StorageAdapter {
|
||||
async getObject(key: string, headers: Headers): Promise<Response> {
|
||||
const res = await fetch(this.getObjectUrl(key), {
|
||||
method: "GET",
|
||||
headers: pickHeaders(headers, ["range"])
|
||||
headers: pickHeaders(headers, ["range"]),
|
||||
});
|
||||
|
||||
return new Response(res.body, {
|
||||
status: res.status,
|
||||
statusText: res.statusText,
|
||||
headers: res.headers
|
||||
headers: res.headers,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -216,14 +216,14 @@ export class StorageCloudinaryAdapter implements StorageAdapter {
|
||||
|
||||
await fetch(`https://res.cloudinary.com/${this.config.cloud_name}/${type}/upload/`, {
|
||||
method: "DELETE",
|
||||
body: formData
|
||||
body: formData,
|
||||
});
|
||||
}
|
||||
|
||||
toJSON(secrets?: boolean) {
|
||||
return {
|
||||
type: "cloudinary",
|
||||
config: secrets ? this.config : { cloud_name: this.config.cloud_name }
|
||||
config: secrets ? this.config : { cloud_name: this.config.cloud_name },
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,15 +5,15 @@ import type {
|
||||
FileListObject,
|
||||
FileMeta,
|
||||
FileUploadPayload,
|
||||
StorageAdapter
|
||||
StorageAdapter,
|
||||
} from "../../Storage";
|
||||
import { guess } from "../../mime-types-tiny";
|
||||
|
||||
export const localAdapterConfig = Type.Object(
|
||||
{
|
||||
path: Type.String({ default: "./" })
|
||||
path: Type.String({ default: "./" }),
|
||||
},
|
||||
{ title: "Local", description: "Local file system storage" }
|
||||
{ title: "Local", description: "Local file system storage" },
|
||||
);
|
||||
export type LocalAdapterConfig = Static<typeof localAdapterConfig>;
|
||||
|
||||
@@ -42,9 +42,9 @@ export class StorageLocalAdapter implements StorageAdapter {
|
||||
return {
|
||||
key: file,
|
||||
last_modified: stats.mtime,
|
||||
size: stats.size
|
||||
size: stats.size,
|
||||
};
|
||||
})
|
||||
}),
|
||||
);
|
||||
return fileStats;
|
||||
}
|
||||
@@ -95,8 +95,8 @@ export class StorageLocalAdapter implements StorageAdapter {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": mimeType || "application/octet-stream",
|
||||
"Content-Length": content.length.toString()
|
||||
}
|
||||
"Content-Length": content.length.toString(),
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
// Handle file reading errors
|
||||
@@ -112,14 +112,14 @@ export class StorageLocalAdapter implements StorageAdapter {
|
||||
const stats = await stat(`${this.config.path}/${key}`);
|
||||
return {
|
||||
type: guess(key) || "application/octet-stream",
|
||||
size: stats.size
|
||||
size: stats.size,
|
||||
};
|
||||
}
|
||||
|
||||
toJSON(secrets?: boolean) {
|
||||
return {
|
||||
type: this.getName(),
|
||||
config: this.config
|
||||
config: this.config,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
export {
|
||||
StorageLocalAdapter,
|
||||
type LocalAdapterConfig,
|
||||
localAdapterConfig
|
||||
localAdapterConfig,
|
||||
} from "./StorageLocalAdapter";
|
||||
|
||||
@@ -4,7 +4,7 @@ import type {
|
||||
HeadObjectRequest,
|
||||
ListObjectsV2Output,
|
||||
ListObjectsV2Request,
|
||||
PutObjectRequest
|
||||
PutObjectRequest,
|
||||
} from "@aws-sdk/client-s3";
|
||||
import { AwsClient, isDebug } from "core";
|
||||
import { type Static, Type, isFile, parse, pickHeaders } from "core/utils";
|
||||
@@ -20,14 +20,14 @@ export const s3AdapterConfig = Type.Object(
|
||||
description: "URL to S3 compatible endpoint without trailing slash",
|
||||
examples: [
|
||||
"https://{account_id}.r2.cloudflarestorage.com/{bucket}",
|
||||
"https://{bucket}.s3.{region}.amazonaws.com"
|
||||
]
|
||||
})
|
||||
"https://{bucket}.s3.{region}.amazonaws.com",
|
||||
],
|
||||
}),
|
||||
},
|
||||
{
|
||||
title: "AWS S3",
|
||||
description: "AWS S3 or compatible storage"
|
||||
}
|
||||
description: "AWS S3 or compatible storage",
|
||||
},
|
||||
);
|
||||
|
||||
export type S3AdapterConfig = Static<typeof s3AdapterConfig>;
|
||||
@@ -40,12 +40,12 @@ export class StorageS3Adapter extends AwsClient implements StorageAdapter {
|
||||
{
|
||||
accessKeyId: config.access_key,
|
||||
secretAccessKey: config.secret_access_key,
|
||||
retries: isDebug() ? 0 : 10
|
||||
retries: isDebug() ? 0 : 10,
|
||||
},
|
||||
{
|
||||
convertParams: "pascalToKebab",
|
||||
responseType: "xml"
|
||||
}
|
||||
responseType: "xml",
|
||||
},
|
||||
);
|
||||
this.#config = parse(s3AdapterConfig, config);
|
||||
}
|
||||
@@ -78,12 +78,12 @@ export class StorageS3Adapter extends AwsClient implements StorageAdapter {
|
||||
async listObjects(key: string = ""): Promise<FileListObject[]> {
|
||||
const params: Omit<ListObjectsV2Request, "Bucket"> & { ListType: number } = {
|
||||
ListType: 2,
|
||||
Prefix: key
|
||||
Prefix: key,
|
||||
};
|
||||
|
||||
const url = this.getUrl("", params);
|
||||
const res = await this.fetchJson<{ ListBucketResult: ListObjectsV2Output }>(url, {
|
||||
method: "GET"
|
||||
method: "GET",
|
||||
});
|
||||
|
||||
// absolutely weird, but if only one object is there, it's an object, not an array
|
||||
@@ -98,11 +98,11 @@ export class StorageS3Adapter extends AwsClient implements StorageAdapter {
|
||||
acc.push({
|
||||
key: obj.Key,
|
||||
last_modified: obj.LastModified,
|
||||
size: obj.Size
|
||||
size: obj.Size,
|
||||
});
|
||||
}
|
||||
},
|
||||
[] as FileListObject[]
|
||||
[] as FileListObject[],
|
||||
);
|
||||
|
||||
return transformed;
|
||||
@@ -112,12 +112,12 @@ export class StorageS3Adapter extends AwsClient implements StorageAdapter {
|
||||
key: string,
|
||||
body: FileBody,
|
||||
// @todo: params must be added as headers, skipping for now
|
||||
params: Omit<PutObjectRequest, "Bucket" | "Key"> = {}
|
||||
params: Omit<PutObjectRequest, "Bucket" | "Key"> = {},
|
||||
) {
|
||||
const url = this.getUrl(key, {});
|
||||
const res = await this.fetch(url, {
|
||||
method: "PUT",
|
||||
body
|
||||
body,
|
||||
});
|
||||
|
||||
if (res.ok) {
|
||||
@@ -130,14 +130,14 @@ export class StorageS3Adapter extends AwsClient implements StorageAdapter {
|
||||
|
||||
private async headObject(
|
||||
key: string,
|
||||
params: Pick<HeadObjectRequest, "PartNumber" | "VersionId"> = {}
|
||||
params: Pick<HeadObjectRequest, "PartNumber" | "VersionId"> = {},
|
||||
) {
|
||||
const url = this.getUrl(key, {});
|
||||
return await this.fetch(url, {
|
||||
method: "HEAD",
|
||||
headers: {
|
||||
Range: "bytes=0-1"
|
||||
}
|
||||
Range: "bytes=0-1",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -148,7 +148,7 @@ export class StorageS3Adapter extends AwsClient implements StorageAdapter {
|
||||
|
||||
return {
|
||||
type,
|
||||
size
|
||||
size,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -159,7 +159,7 @@ export class StorageS3Adapter extends AwsClient implements StorageAdapter {
|
||||
*/
|
||||
async objectExists(
|
||||
key: string,
|
||||
params: Pick<HeadObjectRequest, "PartNumber" | "VersionId"> = {}
|
||||
params: Pick<HeadObjectRequest, "PartNumber" | "VersionId"> = {},
|
||||
) {
|
||||
return (await this.headObject(key)).ok;
|
||||
}
|
||||
@@ -171,14 +171,14 @@ export class StorageS3Adapter extends AwsClient implements StorageAdapter {
|
||||
const url = this.getUrl(key);
|
||||
const res = await this.fetch(url, {
|
||||
method: "GET",
|
||||
headers: pickHeaders(headers, ["range"])
|
||||
headers: pickHeaders(headers, ["range"]),
|
||||
});
|
||||
|
||||
// Response has to be copied, because of middlewares that might set headers
|
||||
return new Response(res.body, {
|
||||
status: res.status,
|
||||
statusText: res.statusText,
|
||||
headers: res.headers
|
||||
headers: res.headers,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -187,18 +187,18 @@ export class StorageS3Adapter extends AwsClient implements StorageAdapter {
|
||||
*/
|
||||
async deleteObject(
|
||||
key: string,
|
||||
params: Omit<DeleteObjectRequest, "Bucket" | "Key"> = {}
|
||||
params: Omit<DeleteObjectRequest, "Bucket" | "Key"> = {},
|
||||
): Promise<void> {
|
||||
const url = this.getUrl(key, params);
|
||||
const res = await this.fetch(url, {
|
||||
method: "DELETE"
|
||||
method: "DELETE",
|
||||
});
|
||||
}
|
||||
|
||||
toJSON(secrets?: boolean) {
|
||||
return {
|
||||
type: this.getName(),
|
||||
config: secrets ? this.#config : undefined
|
||||
config: secrets ? this.#config : undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user