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

feat: add support to resize attachments #210

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions plugins/qeta-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"knex": "^3.0.0",
"lodash": "^4.17.21",
"multiparty": "^4.2.3",
"sharp": "^0.33.5",
"uuid": "^9.0.1",
"zod": "^3.22.4"
},
Expand Down
23 changes: 22 additions & 1 deletion plugins/qeta-backend/src/service/routes/attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import { File, RouteOptions } from '../types';
import { GetObjectCommand, GetObjectCommandOutput } from '@aws-sdk/client-s3';
import { getS3Client } from '../util';
import sharp from 'sharp';

const DEFAULT_IMAGE_SIZE_LIMIT = 2500000;
const DEFAULT_MIME_TYPES = [
Expand All @@ -18,6 +19,10 @@
'image/gif',
];

const resizeImage = (buffer: Buffer, width?: number, height?: number) => {
return sharp(buffer).resize(width, height).toBuffer();
};

export const attachmentsRoutes = (router: Router, options: RouteOptions) => {
const { database, config } = options;

Expand Down Expand Up @@ -46,7 +51,7 @@
}

const fileRequest = files.image[0];
const fileBuffer = await fs.promises.readFile(`${fileRequest?.path}`);
let fileBuffer = await fs.promises.readFile(`${fileRequest?.path}`);
Dismissed Show dismissed Hide dismissed
const mimeType = await FileType.fromBuffer(fileBuffer);

if (mimeType && !supportedFilesTypes.includes(mimeType.mime)) {
Expand All @@ -58,6 +63,22 @@
return;
}

if (request.query.width || request.query.height) {
const width = request.query.width
? parseInt(request.query.width as string, 10)
: undefined;
const height = request.query.height
? parseInt(request.query.height as string, 10)
: undefined;
if ((width && isNaN(width)) || (height && isNaN(height))) {
response.status(400).json({
errors: [{ message: `Width and height must be integers.` }],
});
return;
}
fileBuffer = await resizeImage(fileBuffer, width, height);
}

if (fileBuffer.byteLength > maxSizeImage) {
response.status(400).json({
errors: [
Expand Down
5 changes: 4 additions & 1 deletion plugins/qeta-common/src/api/QetaApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ export interface QetaApi {

postAnswer(answer: AnswerRequest): Promise<AnswerResponseBody>;

postAttachment(file: Blob): Promise<AttachmentResponseBody>;
postAttachment(
file: Blob,
options?: { width?: number; height?: number },
): Promise<AttachmentResponseBody>;

commentAnswer(
questionId: number,
Expand Down
12 changes: 10 additions & 2 deletions plugins/qeta-common/src/api/QetaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,16 @@ export class QetaClient implements QetaApi {
return response.ok;
}

async postAttachment(file: Blob): Promise<AttachmentResponseBody> {
const qetaUrl = `${await this.getBaseUrl()}/attachments`;
async postAttachment(
file: Blob,
options?: { width?: number; height?: number },
): Promise<AttachmentResponseBody> {
const query = this.getQueryParameters(options);

let qetaUrl = `${await this.getBaseUrl()}/attachments`;
if (query) {
qetaUrl += `?${query}`;
}
const formData = new FormData();

formData.append('image', file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const HeaderImageInput = (props: {
qetaApi,
errorApi,
onImageUpload,
options: { height: 500 },
})(buffer).next();
if (typeof uri.value === 'string') {
onChange(uri.value);
Expand Down
7 changes: 6 additions & 1 deletion plugins/qeta-react/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@ export const imageUpload = (opts: {
qetaApi: QetaApi;
errorApi: ErrorApi;
onImageUpload?: (id: number) => void;
options?: {
width?: number;
height?: number;
};
}) => {
const { qetaApi, errorApi, onImageUpload } = opts;
const { qetaApi, errorApi, onImageUpload, options } = opts;
// eslint-disable-next-line func-names
return async function* (data: ArrayBuffer) {
const fileType = await FileType.fromBuffer(data);

const mimeType = fileType ? fileType.mime : 'text/plain';
const attachment = await qetaApi.postAttachment(
new Blob([data], { type: mimeType }),
options,
);
if ('errors' in attachment) {
errorApi.post({
Expand Down
Loading