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

Add error handling for non-existent files and implement thumbnail del… #7589

Open
wants to merge 4 commits into
base: production
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
9 changes: 9 additions & 0 deletions app/api/files/files.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable max-statements */
import entities from 'api/entities';
import { applicationEventsBus } from 'api/eventsbus';
import { mimeTypeFromUrl } from 'api/files/extensionHelper';
Expand Down Expand Up @@ -25,11 +26,19 @@ const deduceMimeType = (_file: FileType) => {
return file;
};

export class UpdateFileError extends Error {
constructor() {
super('Can not update a File that does not exist');
}
}

export const files = {
async save(_file: FileType, index = true) {
const file = deduceMimeType(_file);

const existingFile = file._id ? await filesModel.getById(file._id) : undefined;
if (file._id && !existingFile) throw new UpdateFileError();

const savedFile = await filesModel.save(await validateFile(file));
if (index) {
await search.indexEntities({ sharedId: savedFile.entity }, '+fullText');
Expand Down
19 changes: 11 additions & 8 deletions app/api/files/processDocument.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
/* eslint-disable max-statements */
import { convertToPDFService } from 'api/services/convertToPDF/convertToPdfService';
import settings from 'api/settings';
import { FileType } from 'shared/types/fileType';

import { files } from './files';
import { files, UpdateFileError } from './files';

Check failure on line 6 in app/api/files/processDocument.ts

View workflow job for this annotation

GitHub Actions / eslint

'UpdateFileError' is defined but never used
import { PDF } from './PDF';

export const processPDF = async (
entitySharedId: string,
file: FileType & { destination?: string },
detectLanguage = true
) => {
let thumbnail;
const pdf = new PDF(file);
const upload = await files.save({
...file,
Expand All @@ -24,7 +26,13 @@
conversion.language = file.language;
}

const thumbnail = await pdf.createThumbnail(upload._id.toString());
const saved = await files.save({
...upload,
...conversion,
status: 'ready',
});

thumbnail = await pdf.createThumbnail(upload._id.toString());

await files.save({
entity: entitySharedId,
Expand All @@ -34,18 +42,13 @@
mimetype: 'image/jpeg',
});

const saved = await files.save({
...upload,
...conversion,
status: 'ready',
});

return saved;
} catch (e) {
await files.save({
...upload,
status: 'failed',
});

throw e;
}
};
Expand Down
27 changes: 27 additions & 0 deletions app/api/files/specs/files.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import testingDB from 'api/utils/testing_db';
import { testingEnvironment } from 'api/utils/testingEnvironment';
import { files, UpdateFileError } from '../files';

describe('Files', () => {
beforeEach(async () => {
await testingEnvironment.setUp({});
});

afterAll(async () => testingEnvironment.tearDown());

it('should not update a File if no longer exist', async () => {
const promise = files.save({
_id: testingDB.id(),
filename: 'any_file_name',
originalname: 'any_original_name',
entity: '123',
language: 'en',
});

await expect(promise).rejects.toEqual(new UpdateFileError());

const [result] = await files.get({});

expect(result).toBeUndefined();
});
});
19 changes: 18 additions & 1 deletion app/api/files/specs/processDocument.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import testingDB from 'api/utils/testing_db';
import {
convertToPDFService,
MimeTypeNotSupportedForConversion,
} from 'api/services/convertToPDF/convertToPdfService';
import { testingEnvironment } from 'api/utils/testingEnvironment';
// eslint-disable-next-line node/no-restricted-import
import { writeFile } from 'fs/promises';
import { files } from '../files';
import { files, UpdateFileError } from '../files';
import { attachmentsPath, setupTestUploadedPaths } from '../filesystem';
import { processDocument } from '../processDocument';

Expand Down Expand Up @@ -76,4 +77,20 @@ describe('processDocument', () => {
expect(file).toBeUndefined();
});
});

it('should not persist file or thumbnail if there is an UpdateFileError', async () => {
const _id = testingDB.id();
const promise = processDocument('any_entity_shared_id', {
_id,
filename: 'any_file_name',
originalname: 'any_original_name',
});

await expect(promise).rejects.toEqual(new UpdateFileError());
const [file] = await files.get({ _id });
const [thumbnail] = await files.get({ entity: _id, type: 'thumbnail' });

expect(file).toBeUndefined();
expect(thumbnail).toBeUndefined();
});
});
Loading