pangea-server 3.3.154 → 3.3.155
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.
|
@@ -67,10 +67,10 @@ class FileStorage {
|
|
|
67
67
|
if (previousFile)
|
|
68
68
|
await this.DeleteFile(previousFile);
|
|
69
69
|
if (Buffer.isBuffer(file))
|
|
70
|
-
return uploadFile(file, { folder, fileType:
|
|
70
|
+
return uploadFile(file, { folder, fileType: getBufferMimeType(file) });
|
|
71
71
|
if (typeof file === 'string') {
|
|
72
72
|
const buffer = await promises_1.default.readFile(path_1.default.join(process.cwd(), file));
|
|
73
|
-
return uploadFile(buffer, { folder, fileType:
|
|
73
|
+
return uploadFile(buffer, { folder, fileType: getBufferMimeType(buffer) });
|
|
74
74
|
}
|
|
75
75
|
const stream = fs_1.default.createReadStream(file.path);
|
|
76
76
|
try {
|
|
@@ -102,7 +102,6 @@ class FileStorage {
|
|
|
102
102
|
exports.FileStorage = FileStorage;
|
|
103
103
|
// internal functions
|
|
104
104
|
function getGeneralUploadParams(config) {
|
|
105
|
-
console.log(config.fileType);
|
|
106
105
|
const extension = mimeExtensions[config.fileType];
|
|
107
106
|
const suffix = extension ? `.${extension}` : '';
|
|
108
107
|
const fileName = `${config.folder}/${(0, random_helpers_1.getRandomString)('short')}${suffix}`;
|
|
@@ -114,3 +113,20 @@ async function uploadFile(body, config) {
|
|
|
114
113
|
(0, print_helpers_1.printInfo)('file', `file uploaded to ${res.Location}`);
|
|
115
114
|
return params.Key;
|
|
116
115
|
}
|
|
116
|
+
function getBufferMimeType(buffer) {
|
|
117
|
+
if (buffer.toString('ascii', 0, 4) === '%PDF')
|
|
118
|
+
return 'application/pdf';
|
|
119
|
+
if (buffer[0] === 0x89 && buffer.toString('ascii', 1, 4) === 'PNG')
|
|
120
|
+
return 'image/png';
|
|
121
|
+
if (buffer[0] === 0xff && buffer[1] === 0xd8 && buffer[2] === 0xff)
|
|
122
|
+
return 'image/jpeg';
|
|
123
|
+
if (buffer.toString('ascii', 0, 4) === 'GIF8')
|
|
124
|
+
return 'image/gif';
|
|
125
|
+
if (buffer.toString('ascii', 0, 4) === 'RIFF' && buffer.toString('ascii', 8, 12) === 'WEBP')
|
|
126
|
+
return 'image/webp';
|
|
127
|
+
if (buffer[0] === 0x1a && buffer[1] === 0x45 && buffer[2] === 0xdf && buffer[3] === 0xa3)
|
|
128
|
+
return 'video/webm';
|
|
129
|
+
if (buffer.toString('ascii', 4, 8) === 'ftyp')
|
|
130
|
+
return 'video/mp4';
|
|
131
|
+
return 'application/octet-stream';
|
|
132
|
+
}
|