Skip to content

Commit

Permalink
use file.type as fallback type to getTypeFromFileName
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusmarminge committed Sep 30, 2024
1 parent e48abf5 commit 15d6c33
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
30 changes: 13 additions & 17 deletions packages/shared/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ export function getDefaultSizeForType(fileType: FileRouterInputKey): FileSize {
return "4MB";
}

export function getDefaultRouteConfigValues(type: FileRouterInputKey) {
return {
maxFileSize: getDefaultSizeForType(type),
maxFileCount: 1,
minFileCount: 1,
contentDisposition: "inline" as const,
};
}

/**
* This function takes in the user's input and "upscales" it to a full config
* Additionally, it replaces numbers with "safe" equivalents
Expand All @@ -55,13 +64,7 @@ export const fillInputRouteConfig = (
if (isRouteArray(routeConfig)) {
return Micro.succeed(
routeConfig.reduce<ExpandedRouteConfig>((acc, fileType) => {
acc[fileType] = {
// Apply defaults
maxFileSize: getDefaultSizeForType(fileType),
maxFileCount: 1,
minFileCount: 1,
contentDisposition: "inline" as const,
};
acc[fileType] = getDefaultRouteConfigValues(fileType);
return acc;
}, {}),
);
Expand All @@ -72,15 +75,7 @@ export const fillInputRouteConfig = (
for (const key of objectKeys(routeConfig)) {
const value = routeConfig[key];
if (!value) return Micro.fail(new InvalidRouteConfigError(key));

const defaultValues = {
maxFileSize: getDefaultSizeForType(key),
maxFileCount: 1,
minFileCount: 1,
contentDisposition: "inline" as const,
};

newConfig[key] = { ...defaultValues, ...value };
newConfig[key] = { ...getDefaultRouteConfigValues(key), ...value };
}

// we know that the config is valid, so we can stringify it and parse it back
Expand All @@ -95,11 +90,12 @@ export const fillInputRouteConfig = (
export const getTypeFromFileName = (
fileName: string,
allowedTypes: FileRouterInputKey[],
fallbackType?: string,
): Micro.Micro<
FileRouterInputKey,
UnknownFileTypeError | InvalidFileTypeError
> => {
const mimeType = lookup(fileName);
const mimeType = lookup(fileName) || fallbackType;
if (!mimeType) {
if (allowedTypes.includes("blob")) return Micro.succeed("blob");
return Micro.fail(new UnknownFileTypeError(fileName));
Expand Down
1 change: 1 addition & 0 deletions packages/uploadthing/src/internal/route-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export const assertFilesMeetConfig = (
const type = yield* getTypeFromFileName(
file.name,
objectKeys(routeConfig),
file.type,
);
counts[type] = (counts[type] ?? 0) + 1;

Expand Down

0 comments on commit 15d6c33

Please sign in to comment.