@unchainedshop/core-files 4.6.0 → 4.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/build-hashed-filename.d.ts +1 -0
- package/lib/build-hashed-filename.js +12 -0
- package/lib/director/FileAdapter.d.ts +19 -0
- package/lib/director/FileAdapter.js +23 -0
- package/lib/file-types.d.ts +18 -0
- package/lib/file-types.js +1 -0
- package/lib/put-expiration.d.ts +3 -0
- package/lib/put-expiration.js +4 -0
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function buildHashedFilename(directoryName: string, fileName: string, expiryDate: Date): Promise<string>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { sha1, slugify } from '@unchainedshop/utils';
|
|
2
|
+
export default async function buildHashedFilename(directoryName, fileName, expiryDate) {
|
|
3
|
+
const hashed = await sha1(`${directoryName}${fileName}${expiryDate.getTime()}`);
|
|
4
|
+
const splittedFilename = fileName.split('.');
|
|
5
|
+
const ext = splittedFilename?.pop();
|
|
6
|
+
const fileNameWithoutExtension = splittedFilename.join('.') || '';
|
|
7
|
+
const slugifiedFilenameWithExtension = [slugify(fileNameWithoutExtension), ext]
|
|
8
|
+
.filter(Boolean)
|
|
9
|
+
.join('.');
|
|
10
|
+
const base64url = Buffer.from(hashed).toString('base64url');
|
|
11
|
+
return `${base64url}.${slugifiedFilenameWithExtension}`;
|
|
12
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Readable } from 'node:stream';
|
|
2
|
+
import { type IBaseAdapter } from '@unchainedshop/utils';
|
|
3
|
+
import type { UploadedFile, UploadFileData } from '../file-types.ts';
|
|
4
|
+
export interface IFileAdapter<Context = unknown> extends IBaseAdapter {
|
|
5
|
+
createDownloadURL: (file: UploadedFile, expiry?: number) => Promise<string | null>;
|
|
6
|
+
createSignedURL: (directoryName: string, fileName: string, unchainedAPI: Context) => Promise<(UploadFileData & {
|
|
7
|
+
putURL: string;
|
|
8
|
+
}) | null>;
|
|
9
|
+
removeFiles: (files: UploadedFile[], unchainedContext: Context) => Promise<void>;
|
|
10
|
+
uploadFileFromStream: (directoryName: string, rawFile: any, unchainedAPI: Context, options?: Record<string, any>) => Promise<UploadFileData>;
|
|
11
|
+
uploadFileFromURL: (directoryName: string, fileInput: {
|
|
12
|
+
fileLink: string;
|
|
13
|
+
fileName: string;
|
|
14
|
+
fileId?: string;
|
|
15
|
+
headers?: Record<string, unknown>;
|
|
16
|
+
}, unchainedAPI: Context) => Promise<UploadFileData>;
|
|
17
|
+
createDownloadStream: (file: UploadedFile, unchainedAPI: Context) => Promise<Readable>;
|
|
18
|
+
}
|
|
19
|
+
export declare const FileAdapter: Omit<IFileAdapter, 'key' | 'label' | 'version'>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { BaseAdapter } from '@unchainedshop/utils';
|
|
2
|
+
export const FileAdapter = {
|
|
3
|
+
...BaseAdapter,
|
|
4
|
+
adapterType: Symbol.for('unchained:adapter:file'),
|
|
5
|
+
async createSignedURL() {
|
|
6
|
+
throw new Error('Method not implemented');
|
|
7
|
+
},
|
|
8
|
+
async removeFiles() {
|
|
9
|
+
throw new Error('Method not implemented');
|
|
10
|
+
},
|
|
11
|
+
async createDownloadURL() {
|
|
12
|
+
throw new Error('Method not implemented');
|
|
13
|
+
},
|
|
14
|
+
async uploadFileFromStream() {
|
|
15
|
+
throw new Error('Method not implemented');
|
|
16
|
+
},
|
|
17
|
+
async createDownloadStream() {
|
|
18
|
+
throw new Error('Method not implemented');
|
|
19
|
+
},
|
|
20
|
+
async uploadFileFromURL() {
|
|
21
|
+
throw new Error('Method not implemented');
|
|
22
|
+
},
|
|
23
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface UploadFileData {
|
|
2
|
+
_id: string;
|
|
3
|
+
directoryName: string;
|
|
4
|
+
expiryDate: Date | null;
|
|
5
|
+
fileName: string;
|
|
6
|
+
size?: number;
|
|
7
|
+
type: string;
|
|
8
|
+
url: string;
|
|
9
|
+
}
|
|
10
|
+
export interface UploadedFile {
|
|
11
|
+
_id: string;
|
|
12
|
+
path: string;
|
|
13
|
+
meta?: Record<string, unknown>;
|
|
14
|
+
name: string;
|
|
15
|
+
size?: number;
|
|
16
|
+
type?: string;
|
|
17
|
+
url?: string;
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
const { UNCHAINED_PUT_URL_EXPIRY = '86400000' } = process.env;
|
|
2
|
+
export const expiryOffsetInMs = () => parseInt(UNCHAINED_PUT_URL_EXPIRY, 10) || 24 * 60 * 60 * 1000;
|
|
3
|
+
const resolveExpirationDate = () => new Date(new Date().getTime() + expiryOffsetInMs());
|
|
4
|
+
export default resolveExpirationDate;
|
package/package.json
CHANGED