Skip to content

Commit

Permalink
fix(api-file-manager): extract file extension from an optional key
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel910 committed Dec 12, 2023
1 parent d43a537 commit a8908e8
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 9 deletions.
5 changes: 5 additions & 0 deletions packages/api-file-manager-s3/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const base = require("../../jest.config.base");

module.exports = {
...base({ path: __dirname })
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const plugin: GraphQLSchemaPlugin<FileManagerContext> = {
name: String!
type: String!
size: Long!
key: String
keyPrefix: String
}
type GetPreSignedPostPayloadResponseDataFile {
Expand Down
13 changes: 5 additions & 8 deletions packages/api-file-manager-s3/src/utils/FileExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@ export class FileExtension {
}

getValue() {
const name = this.data.name.toLowerCase();
const maybeHasExtension = name.includes(".");
const name = (this.data.key || this.data.name).toLowerCase();

if (maybeHasExtension) {
const maybeExt = name.split(".").pop() as string;
const extensions = mimeTypes[this.data.type];
if (extensions && !extensions.includes(maybeExt)) {
return extensions[0];
}
const maybeExt = name.split(".").pop() as string;
const extensions = mimeTypes[this.data.type];
if (extensions && !extensions.includes(maybeExt)) {
return extensions[0];
}

return "";
Expand Down
68 changes: 68 additions & 0 deletions packages/api-file-manager-s3/src/utils/FileKey.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { FileKey } from "./FileKey";

describe("FileKey", () => {
it("should generate a file key (extension is included in the name)", () => {
const fileKey = new FileKey({
size: 1071690,
name: "image-14.jpg",
type: "image/jpeg"
});

expect(fileKey.toString()).toEqual("image-14.jpg");
});

it("should generate a file key (extension derived from file type)", () => {
const fileKey = new FileKey({
size: 1071690,
name: "image-14",
type: "image/jpeg"
});

expect(fileKey.toString()).toEqual("image-14.jpeg");
});

it("should generate a file key containing id", () => {
const fileKey = new FileKey({
size: 1071690,
name: "image-14.jpeg",
type: "image/jpeg",
id: "12345678"
});

expect(fileKey.toString()).toEqual("12345678/image-14.jpeg");
});

it("should generate a file key containing prefix", () => {
const fileKey = new FileKey({
size: 1071690,
name: "image-14.jpeg",
type: "image/jpeg",
keyPrefix: "prefix"
});

expect(fileKey.toString()).toEqual("prefix/image-14.jpeg");
});

it("should generate a file key containing id and prefix", () => {
const fileKey = new FileKey({
size: 1071690,
name: "image-14.jpeg",
type: "image/jpeg",
id: "12345678",
keyPrefix: "prefix"
});

expect(fileKey.toString()).toEqual("prefix/12345678/image-14.jpeg");
});

it("should use the key defined in the input", () => {
const fileKey = new FileKey({
size: 1071690,
name: "image",
type: "image/jpeg",
key: "image-14.jpg"
});

expect(fileKey.toString()).toEqual("image-14.jpg");
});
});
2 changes: 1 addition & 1 deletion packages/api-file-manager-s3/src/utils/FileKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class FileKey {
}

private getSanitizedKey() {
const key = sanitizeFilename(this.data.name).replace(/\s/g, "");
const key = sanitizeFilename(this.data.key || this.data.name).replace(/\s/g, "");

return [key, this.getExtension()].filter(Boolean).join(".");
}
Expand Down

0 comments on commit a8908e8

Please sign in to comment.