Add company logo portability support
This commit is contained in:
41
ui/src/lib/portable-files.ts
Normal file
41
ui/src/lib/portable-files.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import type { CompanyPortabilityFileEntry } from "@paperclipai/shared";
|
||||
|
||||
const contentTypeByExtension: Record<string, string> = {
|
||||
".gif": "image/gif",
|
||||
".jpeg": "image/jpeg",
|
||||
".jpg": "image/jpeg",
|
||||
".png": "image/png",
|
||||
".svg": "image/svg+xml",
|
||||
".webp": "image/webp",
|
||||
};
|
||||
|
||||
export function getPortableFileText(entry: CompanyPortabilityFileEntry | null | undefined) {
|
||||
return typeof entry === "string" ? entry : null;
|
||||
}
|
||||
|
||||
export function getPortableFileContentType(
|
||||
filePath: string,
|
||||
entry: CompanyPortabilityFileEntry | null | undefined,
|
||||
) {
|
||||
if (entry && typeof entry === "object" && entry.contentType) return entry.contentType;
|
||||
const extensionIndex = filePath.toLowerCase().lastIndexOf(".");
|
||||
if (extensionIndex === -1) return null;
|
||||
return contentTypeByExtension[filePath.toLowerCase().slice(extensionIndex)] ?? null;
|
||||
}
|
||||
|
||||
export function getPortableFileDataUrl(
|
||||
filePath: string,
|
||||
entry: CompanyPortabilityFileEntry | null | undefined,
|
||||
) {
|
||||
if (!entry || typeof entry === "string") return null;
|
||||
const contentType = getPortableFileContentType(filePath, entry) ?? "application/octet-stream";
|
||||
return `data:${contentType};base64,${entry.data}`;
|
||||
}
|
||||
|
||||
export function isPortableImageFile(
|
||||
filePath: string,
|
||||
entry: CompanyPortabilityFileEntry | null | undefined,
|
||||
) {
|
||||
const contentType = getPortableFileContentType(filePath, entry);
|
||||
return typeof contentType === "string" && contentType.startsWith("image/");
|
||||
}
|
||||
@@ -70,4 +70,28 @@ describe("createZipArchive", () => {
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("round-trips binary image files without coercing them to text", () => {
|
||||
const archive = createZipArchive(
|
||||
{
|
||||
"images/company-logo.png": {
|
||||
encoding: "base64",
|
||||
data: Buffer.from("png-bytes").toString("base64"),
|
||||
contentType: "image/png",
|
||||
},
|
||||
},
|
||||
"paperclip-demo",
|
||||
);
|
||||
|
||||
expect(readZipArchive(archive)).toEqual({
|
||||
rootPath: "paperclip-demo",
|
||||
files: {
|
||||
"images/company-logo.png": {
|
||||
encoding: "base64",
|
||||
data: Buffer.from("png-bytes").toString("base64"),
|
||||
contentType: "image/png",
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user