Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/production' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
daneryl committed Jan 28, 2025
2 parents 11fafaa + d0d5969 commit 86cb9a8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
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
17 changes: 10 additions & 7 deletions app/api/files/processDocument.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable max-statements */
import { convertToPDFService } from 'api/services/convertToPDF/convertToPdfService';
import settings from 'api/settings';
import { FileType } from 'shared/types/fileType';
Expand All @@ -10,6 +11,7 @@ export const processPDF = async (
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 @@ export const processPDF = async (
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 @@ export const processPDF = async (
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();
});
});

0 comments on commit 86cb9a8

Please sign in to comment.