Skip to content

Commit

Permalink
[feat] Allow use of mime-types in file router config (#135)
Browse files Browse the repository at this point in the history
Co-authored-by: Theo Browne <[email protected]>
  • Loading branch information
markflorkowski and t3dotgg authored Jun 8, 2023
1 parent 552351b commit 8a23937
Show file tree
Hide file tree
Showing 9 changed files with 4,676 additions and 8,582 deletions.
5 changes: 5 additions & 0 deletions .changeset/warm-rivers-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"uploadthing": patch
---

[feat] Allow use of mime-types in file router config
5 changes: 4 additions & 1 deletion packages/react/src/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ const INTERNAL_doFormatting = (config?: ExpandedRouteConfig): string => {

const allowedTypes = Object.keys(config) as (keyof ExpandedRouteConfig)[];

const formattedTypes = allowedTypes.map((f) => (f === "blob" ? "file" : f));
const formattedTypes = allowedTypes.map((f) => {
if (f.includes("/")) return `${f.split("/")[1].toUpperCase()} file`;
return f === "blob" ? "file" : f;
});

// Format multi-type uploader label as "Supports videos, images and files";
if (formattedTypes.length > 1) {
Expand Down
5 changes: 4 additions & 1 deletion packages/uploadthing/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ export const DANGEROUS__uploadFiles = async <T extends string>(

// Give content type to blobs because S3 is dumb
// check if content-type is one of the allowed types, or if not and blobs are allowed, use application/octet-stream
if (presigned.fileType === file.type.split("/")[0]) {
if (
presigned.fileType === file.type.split("/")[0] ||
presigned.fileType === file.type
) {
formData.append("Content-Type", file.type);
} else if (presigned.fileType === "blob") {
formData.append("Content-Type", "application/octet-stream");
Expand Down
26 changes: 12 additions & 14 deletions packages/uploadthing/src/internal/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import type { NextApiResponse } from "next";

import { UPLOADTHING_VERSION } from "../constants";
import type {
AllowedFileType,
AnyRuntime,
ExpandedRouteConfig,
FileRouter,
FileRouterInputKey,
UploadedFile,
} from "../types";
import {
Expand Down Expand Up @@ -42,28 +42,26 @@ const fileCountLimitHit = (
files: string[],
routeConfig: ExpandedRouteConfig,
) => {
const counts: Record<AllowedFileType, number> = {
image: 0,
video: 0,
audio: 0,
text: 0,
pdf: 0,
blob: 0,
};
const counts: { [k: string]: number } = {};

files.forEach((file) => {
const type = getTypeFromFileName(
file,
Object.keys(routeConfig) as AllowedFileType[],
);
counts[type] += 1;
Object.keys(routeConfig) as FileRouterInputKey[],
) as FileRouterInputKey;

if (!counts[type]) {
counts[type] = 1;
} else {
counts[type] += 1;
}
});

return Object.keys(counts).some((key) => {
const count = counts[key as AllowedFileType];
const count = counts[key as FileRouterInputKey];
if (count === 0) return false;

const limit = routeConfig[key as AllowedFileType]?.maxFileCount;
const limit = routeConfig[key as FileRouterInputKey]?.maxFileCount;
if (!limit) {
console.error(routeConfig, key);
throw new Error("invalid config during file count");
Expand Down
Loading

1 comment on commit 8a23937

@vercel
Copy link

@vercel vercel bot commented on 8a23937 Jun 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.