Skip to content

Commit

Permalink
feat(upload folder): defer to use user defined content type on files …
Browse files Browse the repository at this point in the history
…when provided PE-4643
  • Loading branch information
fedellen committed Aug 8, 2024
1 parent b3ab120 commit 5d5ef89
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 20 deletions.
35 changes: 24 additions & 11 deletions src/node/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,34 @@ export class TurboAuthenticatedNodeUploadService extends TurboAuthenticatedBaseU
const limit = pLimit(maxConcurrentUploads);

const uploadFile = async (absoluteFilePath: string) => {
const mimeType = lookup(absoluteFilePath);
const contentType =
mimeType === false ? 'application/octet-stream' : mimeType;
const contentType = (() => {
const userDefinedContentType = dataItemOpts?.tags?.find(
(tag) => tag.name === 'Content-Type',
)?.value;
if (userDefinedContentType !== undefined) {
return undefined;
}
const mimeType = lookup(absoluteFilePath);
return mimeType !== false ? mimeType : 'application/octet-stream';
})();

const dataItemOptsWithContentType =
contentType === undefined
? dataItemOpts
: {
...dataItemOpts,
tags: [
...(dataItemOpts?.tags ?? []),
{ name: 'Content-Type', value: contentType },
],
};

try {
const result = await this.uploadFile({
fileStreamFactory: () => createReadStream(absoluteFilePath),
fileSizeFactory: () => statSync(absoluteFilePath).size,
signal,
dataItemOpts: {
...dataItemOpts,
tags: [
...(dataItemOpts?.tags ?? []),
{ name: 'Content-Type', value: contentType },
],
},
dataItemOpts: dataItemOptsWithContentType,
});
fileResponses.push(result);
const relativePath = absoluteFilePath.replace(folderPath + '/', '');
Expand All @@ -129,7 +141,8 @@ export class TurboAuthenticatedNodeUploadService extends TurboAuthenticatedBaseU
});

const tagsWithManifestContentType = [
...(dataItemOpts?.tags ?? []),
...(dataItemOpts?.tags?.filter((tag) => tag.name !== 'Content-Type') ??
[]),
{ name: 'Content-Type', value: 'application/x.arweave-manifest+json' },
];
const manifestBuffer = Buffer.from(JSON.stringify(manifest));
Expand Down
32 changes: 23 additions & 9 deletions src/web/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,34 @@ export class TurboAuthenticatedWebUploadService extends TurboAuthenticatedBaseUp
const limit = pLimit(maxConcurrentUploads);

const uploadFile = async (file: File) => {
const contentType = file.type ?? 'application/octet-stream';
const contentType = (() => {
const userDefinedContentType = dataItemOpts?.tags?.find(
(tag) => tag.name === 'Content-Type',
)?.value;
if (userDefinedContentType !== undefined) {
return undefined;
}
file.type ?? 'application/octet-stream';
})();

const dataItemOptsWithContentType =
contentType === undefined
? dataItemOpts
: {
...dataItemOpts,
tags: [
...(dataItemOpts?.tags ?? []),
{ name: 'Content-Type', value: contentType },
],
};

try {
const result = await this.uploadFile({
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fileStreamFactory: () => file.stream() as any,
fileSizeFactory: () => file.size,
signal,
dataItemOpts: {
...dataItemOpts,
tags: [
...(dataItemOpts?.tags ?? []),
{ name: 'Content-Type', value: contentType },
],
},
dataItemOpts: dataItemOptsWithContentType,
});

const relativePath = file.name ?? file.webkitRelativePath;
Expand All @@ -98,7 +111,8 @@ export class TurboAuthenticatedWebUploadService extends TurboAuthenticatedBaseUp
});

const tagsWithManifestContentType = [
...(dataItemOpts?.tags ?? []),
...(dataItemOpts?.tags?.filter((tag) => tag.name !== 'Content-Type') ??
[]),
{ name: 'Content-Type', value: 'application/x.arweave-manifest+json' },
];

Expand Down

0 comments on commit 5d5ef89

Please sign in to comment.