Skip to content

Commit

Permalink
feat: keep original name #247
Browse files Browse the repository at this point in the history
  • Loading branch information
diced committed Jan 15, 2023
1 parent a8020ec commit 4ea1775
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- AlterTable
ALTER TABLE "File" ADD COLUMN "originalName" TEXT;

-- RenameForeignKey
ALTER TABLE "File" RENAME CONSTRAINT "Image_userId_fkey" TO "File_userId_fkey";

-- RenameIndex
ALTER INDEX "InvisibleImage_invis_key" RENAME TO "InvisibleFile_invis_key";
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ enum FileNameFormat {
model File {
id Int @id @default(autoincrement())
name String
originalName String?
mimetype String @default("image/png")
createdAt DateTime @default(now())
expiresAt DateTime?
Expand Down
2 changes: 2 additions & 0 deletions src/components/pages/Upload/File.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export default function File({ chunks: chunks_config }) {
options.embedded && req.setRequestHeader('Embed', 'true');
options.zeroWidth && req.setRequestHeader('Zws', 'true');
options.format !== 'default' && req.setRequestHeader('Format', options.format);
options.originalName && req.setRequestHeader('Original-Name', 'true');

req.send(body);

Expand Down Expand Up @@ -274,6 +275,7 @@ export default function File({ chunks: chunks_config }) {
options.embedded && req.setRequestHeader('Embed', 'true');
options.zeroWidth && req.setRequestHeader('Zws', 'true');
options.format !== 'default' && req.setRequestHeader('Format', options.format);
options.originalName && req.setRequestHeader('Original-Name', 'true');

req.send(body);
}
Expand Down
1 change: 1 addition & 0 deletions src/components/pages/Upload/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export default function Text() {
options.embedded && req.setRequestHeader('Embed', 'true');
options.zeroWidth && req.setRequestHeader('Zws', 'true');
options.format !== 'default' && req.setRequestHeader('Format', options.format);
options.originalName && req.setRequestHeader('Original-Name', 'true');

req.send(body);
};
Expand Down
10 changes: 10 additions & 0 deletions src/components/pages/Upload/useUploadOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default function useUploadOptions(): [
zeroWidth: boolean;
embedded: boolean;
format: string;
originalName: boolean;
},
Dispatch<SetStateAction<boolean>>,
React.FC
Expand All @@ -34,6 +35,7 @@ export default function useUploadOptions(): [
zeroWidth: false,
embedded: false,
format: 'default',
originalName: false,
});

const [opened, setOpened] = useState(false);
Expand All @@ -47,6 +49,7 @@ export default function useUploadOptions(): [
zeroWidth: false,
embedded: false,
format: 'default',
originalName: false,
});
};

Expand Down Expand Up @@ -146,6 +149,13 @@ export default function useUploadOptions(): [
checked={state.embedded}
onChange={(e) => setState({ embedded: e.currentTarget.checked })}
/>

<Switch
label='Keep Original Name'
description='Whether or not to show the original name when downloading this specific file. This will not change the name format in the URL.'
checked={state.originalName}
onChange={(e) => setState({ originalName: e.currentTarget.checked })}
/>
</Group>
<Group grow>
<Button onClick={() => reset()} color='red'>
Expand Down
26 changes: 14 additions & 12 deletions src/pages/api/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ async function handler(req: NextApiReq, res: NextApiRes) {
password,
expiresAt: expiry,
maxViews: fileMaxViews,
originalName: req.headers['original-name'] ? filename ?? null : null,
},
});

Expand Down Expand Up @@ -290,7 +291,7 @@ async function handler(req: NextApiReq, res: NextApiRes) {

const compressionUsed = imageCompressionPercent && file.mimetype.startsWith('image/');
let invis: InvisibleFile;
const image = await prisma.file.create({
const fileUpload = await prisma.file.create({
data: {
name: `${fileName}${compressionUsed ? '.jpg' : `${ext ? '.' : ''}${ext}`}`,
mimetype: req.headers.uploadtext ? 'text/plain' : compressionUsed ? 'image/jpeg' : file.mimetype,
Expand All @@ -300,34 +301,35 @@ async function handler(req: NextApiReq, res: NextApiRes) {
password,
expiresAt: expiry,
maxViews: fileMaxViews,
originalName: req.headers['original-name'] ? file.originalname ?? null : null,
},
});

if (req.headers.zws) invis = await createInvisImage(zconfig.uploader.length, image.id);
if (req.headers.zws) invis = await createInvisImage(zconfig.uploader.length, fileUpload.id);

if (compressionUsed) {
const buffer = await sharp(file.buffer).jpeg({ quality: imageCompressionPercent }).toBuffer();
await datasource.save(image.name, buffer);
await datasource.save(fileUpload.name, buffer);
logger.info(
`User ${user.username} (${user.id}) compressed image from ${file.buffer.length} -> ${buffer.length} bytes`
);
} else {
await datasource.save(image.name, file.buffer);
await datasource.save(fileUpload.name, file.buffer);
}

logger.info(`User ${user.username} (${user.id}) uploaded ${image.name} (${image.id})`);
logger.info(`User ${user.username} (${user.id}) uploaded ${fileUpload.name} (${fileUpload.id})`);
if (user.domains.length) {
const domain = user.domains[Math.floor(Math.random() * user.domains.length)];
response.files.push(
`${domain}${zconfig.uploader.route === '/' ? '' : zconfig.uploader.route}/${
invis ? invis.invis : image.name
invis ? invis.invis : fileUpload.name
}`
);
} else {
response.files.push(
`${zconfig.core.return_https ? 'https' : 'http'}://${req.headers.host}${
zconfig.uploader.route === '/' ? '' : zconfig.uploader.route
}/${invis ? invis.invis : image.name}`
}/${invis ? invis.invis : fileUpload.name}`
);
}

Expand All @@ -336,18 +338,18 @@ async function handler(req: NextApiReq, res: NextApiRes) {
if (zconfig.discord?.upload) {
await sendUpload(
user,
image,
fileUpload,
`${zconfig.core.return_https ? 'https' : 'http'}://${req.headers.host}/r/${
invis ? invis.invis : image.name
invis ? invis.invis : fileUpload.name
}`,
`${zconfig.core.return_https ? 'https' : 'http'}://${req.headers.host}${
zconfig.uploader.route === '/' ? '' : zconfig.uploader.route
}/${invis ? invis.invis : image.name}`
}/${invis ? invis.invis : fileUpload.name}`
);
}

if (zconfig.exif.enabled && zconfig.exif.remove_gps && image.mimetype.startsWith('image/')) {
await removeGPSData(image);
if (zconfig.exif.enabled && zconfig.exif.remove_gps && fileUpload.mimetype.startsWith('image/')) {
await removeGPSData(fileUpload);
response.removed_gps = true;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/server/decorators/dbFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ function dbFileDecorator(fastify: FastifyInstance, _, done) {

this.header('Content-Length', size);
this.header('Content-Type', file.mimetype);
this.header('Content-Disposition', `inline; filename="${file.originalName}"`);

return this.send(data);
}
}
Expand Down

0 comments on commit 4ea1775

Please sign in to comment.