Просмотр исходного кода

fix(asset-server-plugin): Correctly handle non-latin filenames

Fixes #1343
Michael Bromley 4 лет назад
Родитель
Сommit
4cf8434df4

+ 30 - 0
packages/asset-server-plugin/e2e/asset-server-plugin.e2e-spec.ts

@@ -84,6 +84,36 @@ describe('AssetServerPlugin', () => {
         expect(Buffer.compare(responseBuffer, previewFile)).toBe(0);
     });
 
+    it('can handle non-latin filenames', async () => {
+        const FILE_NAME_ZH = '白飯';
+        const filesToUpload = [path.join(__dirname, `fixtures/assets/${FILE_NAME_ZH}.jpg`)];
+        const { createAssets }: CreateAssets.Mutation = await adminClient.fileUploadMutation({
+            mutation: CREATE_ASSETS,
+            filePaths: filesToUpload,
+            mapVariables: filePaths => ({
+                input: filePaths.map(p => ({ file: null })),
+            }),
+        });
+        expect(createAssets[0].name).toBe(`${FILE_NAME_ZH}.jpg`);
+        expect(createAssets[0].source).toContain(`${FILE_NAME_ZH}.jpg`);
+
+        const previewUrl = encodeURI(`${createAssets[0].preview}`);
+        const res = await fetch(previewUrl);
+
+        expect(res.status).toBe(200);
+
+        const previewFilePathZH = path.join(
+            __dirname,
+            TEST_ASSET_DIR,
+            `preview/3f/${FILE_NAME_ZH}__preview.jpg`,
+        );
+
+        const responseBuffer = await res.buffer();
+        const previewFile = await fs.readFile(previewFilePathZH);
+
+        expect(Buffer.compare(responseBuffer, previewFile)).toBe(0);
+    });
+
     describe('caching', () => {
         const cacheDir = path.join(__dirname, TEST_ASSET_DIR, 'cache');
         const cacheFileDir = path.join(__dirname, TEST_ASSET_DIR, 'cache', 'preview', '71');

BIN
packages/asset-server-plugin/e2e/fixtures/assets/白飯.jpg


+ 6 - 4
packages/asset-server-plugin/src/plugin.ts

@@ -231,10 +231,11 @@ export class AssetServerPlugin implements NestModule, OnApplicationBootstrap {
         return async (err: any, req: Request, res: Response, next: NextFunction) => {
             if (err && (err.status === 404 || err.statusCode === 404)) {
                 if (req.query) {
-                    Logger.debug(`Pre-cached Asset not found: ${req.path}`, loggerCtx);
+                    const decodedReqPath = decodeURIComponent(req.path);
+                    Logger.debug(`Pre-cached Asset not found: ${decodedReqPath}`, loggerCtx);
                     let file: Buffer;
                     try {
-                        file = await AssetServerPlugin.assetStorage.readFileToBuffer(req.path);
+                        file = await AssetServerPlugin.assetStorage.readFileToBuffer(decodedReqPath);
                     } catch (err) {
                         res.status(404).send('Resource not found');
                         return;
@@ -278,10 +279,11 @@ export class AssetServerPlugin implements NestModule, OnApplicationBootstrap {
             }
         }
 
+        const decodedReqPath = decodeURIComponent(req.path);
         if (imageParamHash) {
-            return path.join(this.cacheDir, this.addSuffix(req.path, imageParamHash));
+            return path.join(this.cacheDir, this.addSuffix(decodedReqPath, imageParamHash));
         } else {
-            return req.path;
+            return decodedReqPath;
         }
     }