@opengis/fastify-table 2.2.4 → 2.2.5
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uploadMultiPart.d.ts","sourceRoot":"","sources":["../../../../server/plugins/file/uploadMultiPart.ts"],"names":[],"mappings":"AAqBA,MAAM,WAAW,KAAK;IACpB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC7B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;
|
|
1
|
+
{"version":3,"file":"uploadMultiPart.d.ts","sourceRoot":"","sources":["../../../../server/plugins/file/uploadMultiPart.ts"],"names":[],"mappings":"AAqBA,MAAM,WAAW,KAAK;IACpB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC7B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AA+DD,wBAAsB,eAAe,CACnC,GAAG,EAAE,GAAG,EACR,EACE,UAAU,EACV,GAAG,EACH,MAAM,EACN,gBAAwB,GACzB,GAAE;IACD,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IACtB,GAAG,CAAC,EAAE,OAAO,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,gBAAgB,CAAC,EAAE,OAAO,CAAC;CACvB,GACL,OAAO,CAAC,KAAK,CAAC,CAoGhB;AAED,eAAe,eAAe,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/* eslint-disable no-console */
|
|
2
2
|
import path from "node:path";
|
|
3
|
-
import { mkdir, writeFile } from "node:fs/promises";
|
|
3
|
+
import { mkdir, writeFile, readdir } from "node:fs/promises";
|
|
4
4
|
import { randomUUID } from "node:crypto";
|
|
5
5
|
import { imageSize } from "image-size";
|
|
6
6
|
import { existsSync } from "node:fs";
|
|
@@ -36,6 +36,20 @@ async function writeFileToDisk(file, buffer) {
|
|
|
36
36
|
await writeFile(file.filepath, buffer);
|
|
37
37
|
return null;
|
|
38
38
|
}
|
|
39
|
+
async function rotate(filename, dirpath) {
|
|
40
|
+
const { name, ext } = path.parse(filename);
|
|
41
|
+
// ? read whole dir once, rotate later - better works for large dirs, worse - for small ones
|
|
42
|
+
// ? in practice, latency is more consistent than w/ multiple existsSync calls
|
|
43
|
+
const files = new Set(await readdir(dirpath));
|
|
44
|
+
if (!files.has(filename)) {
|
|
45
|
+
return filename;
|
|
46
|
+
}
|
|
47
|
+
let idx = 1;
|
|
48
|
+
while (files.has(`${name}_${idx}${ext}`)) {
|
|
49
|
+
idx++;
|
|
50
|
+
}
|
|
51
|
+
return `${name}_${idx}${ext}`;
|
|
52
|
+
}
|
|
39
53
|
export async function uploadMultiPart(req, { extensions, raw, subdir, originalFilename = false, } = {}) {
|
|
40
54
|
const allowedExtensions = req.routeOptions?.url === "/file/upload-image/*" ? images : all;
|
|
41
55
|
const parts = req.parts();
|
|
@@ -73,6 +87,7 @@ export async function uploadMultiPart(req, { extensions, raw, subdir, originalFi
|
|
|
73
87
|
buffer: fileBuffer,
|
|
74
88
|
filename: value.filename,
|
|
75
89
|
extension: ext.substring(1),
|
|
90
|
+
mimetype: value.mimetype,
|
|
76
91
|
fields: value.fields,
|
|
77
92
|
};
|
|
78
93
|
}
|
|
@@ -83,7 +98,7 @@ export async function uploadMultiPart(req, { extensions, raw, subdir, originalFi
|
|
|
83
98
|
const reldirpath = path.join("/files", dir, yearMonthDay);
|
|
84
99
|
const folder = path.join(rootDir, config.folder || "", reldirpath);
|
|
85
100
|
const newFilename = originalFilename
|
|
86
|
-
? value.filename
|
|
101
|
+
? await rotate(value.filename, folder)
|
|
87
102
|
: `${randomUUID()}${ext}`;
|
|
88
103
|
if (existsSync(path.join(folder, newFilename).replace(/\\/g, "/"))) {
|
|
89
104
|
throw new BadRequestError("file with specified name already exists in directory");
|