Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide thumbnail for protected file uploads #10626

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
{
"targets": {
"node": true
}
},
"exclude": [
"proposal-dynamic-import"
]
}
]
],
Expand All @@ -16,7 +19,7 @@
"allowDeclareFields": true
}
],
"add-module-exports",
"add-module-exports"
],
"env": {
"test": {
Expand Down
206 changes: 206 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
"passport": "0.7.0",
"passport-github": "1.1.0",
"paypal-adaptive": "0.4.2",
"pdfjs-dist": "4.9.155",
"pg": "8.13.1",
"pg-connection-string": "2.7.0",
"pg-format": "1.0.4",
Expand Down Expand Up @@ -145,6 +146,7 @@
"@babel/register": "^7.23.7",
"@eslint/compat": "^1.2.1",
"@graphql-eslint/eslint-plugin": "^3.20.1",
"@napi-rs/canvas": "^0.1.65",
"@opentelemetry/api": "^1.7.0",
"@opentelemetry/auto-instrumentations-node": "^0.55.0",
"@opentelemetry/exporter-trace-otlp-proto": "^0.54.0",
Expand Down
15 changes: 15 additions & 0 deletions server/controllers/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Request, Response } from 'express';
import { hasUploadedFilePermission } from '../graphql/common/uploaded-file';
import { idDecode, IDENTIFIER_TYPES } from '../graphql/v2/identifiers';
import { getSignedGetURL, parseS3Url } from '../lib/awsS3';
import { generateThumbnailFromBucketUrl } from '../lib/thumbnails';
import { UploadedFile } from '../models';
import { SUPPORTED_FILE_TYPES_IMAGES } from '../models/UploadedFile';

Expand Down Expand Up @@ -49,6 +50,14 @@ export async function getFile(req: Request, res: Response) {

const actualUrl = uploadedFile.getDataValue('url');

if (uploadedFile.isPublicFile()) {
if (isJsonAccepted) {
return res.send({ url: actualUrl });
} else {
return res.redirect(307, actualUrl);
}
}

if (
!(await hasUploadedFilePermission(req, uploadedFile, {
expenseId: expenseId ? idDecode(expenseId as string, IDENTIFIER_TYPES.EXPENSE) : null,
Expand All @@ -61,6 +70,12 @@ export async function getFile(req: Request, res: Response) {
let redirectUrl: string;

if (isThumbnail) {
const thumbail = await generateThumbnailFromBucketUrl(actualUrl);
if (thumbail) {
res.setHeader('Content-Type', 'image/png');
return res.send(thumbail);
}

if (SUPPORTED_FILE_TYPES_IMAGES.includes(uploadedFile.fileType as (typeof SUPPORTED_FILE_TYPES_IMAGES)[number])) {
redirectUrl = `${config.host.website}/static/images/file-text.svg`;
} else {
Expand Down
16 changes: 8 additions & 8 deletions server/graphql/common/expenses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1508,14 +1508,14 @@ export async function prepareAttachedFiles(req: Request, attachedFiles: ExpenseD
return [];
}

const mapItemUrlToUploadedFile: Record<string, string> = {};
const mapItemUrlToUploadedFileUrl: Record<string, string> = {};
for (const item of attachedFiles) {
if (!item.url) {
continue;
}

if (!UploadedFile.isUploadedFileURL(item.url)) {
mapItemUrlToUploadedFile[item.url] = item.url;
mapItemUrlToUploadedFileUrl[item.url] = item.url;
continue;
}

Expand All @@ -1525,12 +1525,12 @@ export async function prepareAttachedFiles(req: Request, attachedFiles: ExpenseD

const uploadedFile = await UploadedFile.getFromURL(item.url);

mapItemUrlToUploadedFile[item.url] = uploadedFile.getDataValue('url');
mapItemUrlToUploadedFileUrl[item.url] = uploadedFile.getDataValue('url');
}

return attachedFiles.map(file => ({
...file,
url: mapItemUrlToUploadedFile[file.url],
url: mapItemUrlToUploadedFileUrl[file.url],
}));
}

Expand Down Expand Up @@ -1568,14 +1568,14 @@ export const prepareExpenseItemInputs = async (
);
}

const mapItemUrlToUploadedFile: Record<string, string> = {};
const mapItemUrlToUploadedFileUrl: Record<string, string> = {};
for (const item of itemsInput) {
if (!item.url) {
continue;
}

if (!UploadedFile.isUploadedFileURL(item.url)) {
mapItemUrlToUploadedFile[item.url] = item.url;
mapItemUrlToUploadedFileUrl[item.url] = item.url;
continue;
}

Expand All @@ -1584,7 +1584,7 @@ export const prepareExpenseItemInputs = async (
}

const uploadedFile = await UploadedFile.getFromURL(item.url);
mapItemUrlToUploadedFile[item.url] = uploadedFile.getDataValue('url');
mapItemUrlToUploadedFileUrl[item.url] = uploadedFile.getDataValue('url');
}

// Prepare items
Expand All @@ -1593,7 +1593,7 @@ export const prepareExpenseItemInputs = async (
const values: Partial<ExpenseItem> = pick(itemInput, fieldsToPick);

if (values.url) {
values.url = mapItemUrlToUploadedFile[values.url];
values.url = mapItemUrlToUploadedFileUrl[values.url];
}

if (itemInput['amount'] && itemInput['amountV2']) {
Expand Down
Loading
Loading