Skip to content

Commit

Permalink
Splits findOrAttach file into 2 functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Spoffy committed Jan 14, 2025
1 parent af5f08d commit 5372657
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app/server/lib/AttachmentFileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ export class AttachmentFileManager implements IAttachmentFileManager {
// Uploads the file to local storage, overwriting the current DB record for the file.
private async _storeFileInLocalStorage(fileIdent: string, fileData: Buffer): Promise<AddFileResult> {
// Insert (or overwrite) the entry for this file in the document database.
const isNewFile = await this._docStorage.findOrAttachFile(fileIdent, fileData, undefined, true);
const isNewFile = await this._docStorage.attachOrUpdateFile(fileIdent, fileData, undefined);

Check failure on line 404 in app/server/lib/AttachmentFileManager.ts

View workflow job for this annotation

GitHub Actions / build_and_test (3.11, 22.x, :nbrowser-^[M-N]:)

Property 'attachOrUpdateFile' does not exist on type 'DocStorage'.

Check failure on line 404 in app/server/lib/AttachmentFileManager.ts

View workflow job for this annotation

GitHub Actions / build_and_test (3.11, 22.x, :nbrowser-^[A-D]:)

Property 'attachOrUpdateFile' does not exist on type 'DocStorage'.

Check failure on line 404 in app/server/lib/AttachmentFileManager.ts

View workflow job for this annotation

GitHub Actions / build_and_test (3.11, 22.x, :server-2-of-2:)

Property 'attachOrUpdateFile' does not exist on type 'DocStorage'.

Check failure on line 404 in app/server/lib/AttachmentFileManager.ts

View workflow job for this annotation

GitHub Actions / build_and_test (3.11, 22.x, :nbrowser-^[^A-R]:)

Property 'attachOrUpdateFile' does not exist on type 'DocStorage'.

Check failure on line 404 in app/server/lib/AttachmentFileManager.ts

View workflow job for this annotation

GitHub Actions / build_and_test (3.11, 22.x, :nbrowser-^[E-L]:)

Property 'attachOrUpdateFile' does not exist on type 'DocStorage'.

Check failure on line 404 in app/server/lib/AttachmentFileManager.ts

View workflow job for this annotation

GitHub Actions / build_and_test (3.11, 22.x, :nbrowser-^[O-R]:)

Property 'attachOrUpdateFile' does not exist on type 'DocStorage'.

Check failure on line 404 in app/server/lib/AttachmentFileManager.ts

View workflow job for this annotation

GitHub Actions / build_and_test (3.11, 22.x, :gen-server:)

Property 'attachOrUpdateFile' does not exist on type 'DocStorage'.

Check failure on line 404 in app/server/lib/AttachmentFileManager.ts

View workflow job for this annotation

GitHub Actions / build_and_test (3.11, 22.x, :server-1-of-2:)

Property 'attachOrUpdateFile' does not exist on type 'DocStorage'.

Check failure on line 404 in app/server/lib/AttachmentFileManager.ts

View workflow job for this annotation

GitHub Actions / build_and_test (:lint:python:client:common:smoke:, 22.x, 3.10)

Property 'attachOrUpdateFile' does not exist on type 'DocStorage'.

Check failure on line 404 in app/server/lib/AttachmentFileManager.ts

View workflow job for this annotation

GitHub Actions / build_and_test (3.11, 22.x, :lint:python:client:common:smoke:stubs:)

Property 'attachOrUpdateFile' does not exist on type 'DocStorage'.

return {
fileIdent,
Expand All @@ -418,7 +418,7 @@ export class AttachmentFileManager implements IAttachmentFileManager {
await store.upload(this._getDocPoolId(), fileIdent, Readable.from(fileData));

// Insert (or overwrite) the entry for this file in the document database.
await this._docStorage.findOrAttachFile(fileIdent, undefined, store.id, true);
await this._docStorage.attachOrUpdateFile(fileIdent, undefined, store.id);

Check failure on line 421 in app/server/lib/AttachmentFileManager.ts

View workflow job for this annotation

GitHub Actions / build_and_test (3.11, 22.x, :nbrowser-^[M-N]:)

Property 'attachOrUpdateFile' does not exist on type 'DocStorage'.

Check failure on line 421 in app/server/lib/AttachmentFileManager.ts

View workflow job for this annotation

GitHub Actions / build_and_test (3.11, 22.x, :nbrowser-^[A-D]:)

Property 'attachOrUpdateFile' does not exist on type 'DocStorage'.

Check failure on line 421 in app/server/lib/AttachmentFileManager.ts

View workflow job for this annotation

GitHub Actions / build_and_test (3.11, 22.x, :server-2-of-2:)

Property 'attachOrUpdateFile' does not exist on type 'DocStorage'.

Check failure on line 421 in app/server/lib/AttachmentFileManager.ts

View workflow job for this annotation

GitHub Actions / build_and_test (3.11, 22.x, :nbrowser-^[^A-R]:)

Property 'attachOrUpdateFile' does not exist on type 'DocStorage'.

Check failure on line 421 in app/server/lib/AttachmentFileManager.ts

View workflow job for this annotation

GitHub Actions / build_and_test (3.11, 22.x, :nbrowser-^[E-L]:)

Property 'attachOrUpdateFile' does not exist on type 'DocStorage'.

Check failure on line 421 in app/server/lib/AttachmentFileManager.ts

View workflow job for this annotation

GitHub Actions / build_and_test (3.11, 22.x, :nbrowser-^[O-R]:)

Property 'attachOrUpdateFile' does not exist on type 'DocStorage'.

Check failure on line 421 in app/server/lib/AttachmentFileManager.ts

View workflow job for this annotation

GitHub Actions / build_and_test (3.11, 22.x, :gen-server:)

Property 'attachOrUpdateFile' does not exist on type 'DocStorage'.

Check failure on line 421 in app/server/lib/AttachmentFileManager.ts

View workflow job for this annotation

GitHub Actions / build_and_test (3.11, 22.x, :server-1-of-2:)

Property 'attachOrUpdateFile' does not exist on type 'DocStorage'.

Check failure on line 421 in app/server/lib/AttachmentFileManager.ts

View workflow job for this annotation

GitHub Actions / build_and_test (:lint:python:client:common:smoke:, 22.x, 3.10)

Property 'attachOrUpdateFile' does not exist on type 'DocStorage'.

Check failure on line 421 in app/server/lib/AttachmentFileManager.ts

View workflow job for this annotation

GitHub Actions / build_and_test (3.11, 22.x, :lint:python:client:common:smoke:stubs:)

Property 'attachOrUpdateFile' does not exist on type 'DocStorage'.

return fileIdent;
}
Expand Down
23 changes: 19 additions & 4 deletions test/server/lib/AttachmentFileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import * as sinon from "sinon";
import * as stream from "node:stream";

// Minimum features of doc storage that are needed to make AttachmentFileManager work.
type IMinimalDocStorage = Pick<DocStorage, 'docName' | 'getFileInfo' | 'findOrAttachFile' | 'listAllFiles'>
type IMinimalDocStorage = Pick<
DocStorage,
'docName' | 'getFileInfo' | 'findOrAttachFile' | 'attachOrUpdateFile' | 'listAllFiles'
>

// Implements the minimal functionality needed for the AttachmentFileManager to work.
class DocStorageFake implements IMinimalDocStorage {
Expand All @@ -37,17 +40,29 @@ class DocStorageFake implements IMinimalDocStorage {

// Needs to match the semantics of DocStorage's implementation.
public async findOrAttachFile(
fileIdent: string, fileData: Buffer | undefined, storageId?: string | undefined, shouldUpdate: boolean = true
fileIdent: string, fileData: Buffer | undefined, storageId?: string | undefined
): Promise<boolean> {
if (fileIdent in this._files && !shouldUpdate) {
if (fileIdent in this._files) {
return false;
}
this._setFileRecord(fileIdent, fileData, storageId);
return true;
}

public async attachOrUpdateFile(
fileIdent: string, fileData: Buffer | undefined, storageId?: string | undefined
): Promise<boolean> {
const exists = fileIdent in this._files;
this._setFileRecord(fileIdent, fileData, storageId);
return !exists;
}

private _setFileRecord(fileIdent: string, fileData: Buffer | undefined, storageId?: string | undefined) {
this._files[fileIdent] = {
ident: fileIdent,
data: fileData ?? Buffer.alloc(0),
storageId: storageId ?? null,
};
return true;
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/server/lib/DocStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ describe('DocStorage', function() {
.then(fileInfo => assert.equal(fileInfo.data.toString('utf8'), correctFileContents))

// The update parameter should allow the record to be overwritten
.then(() => docStorage.findOrAttachFile("hello_world.txt", Buffer.from(replacementFileContents), undefined, true))
.then(() => docStorage.attachOrUpdateFile("hello_world.txt", Buffer.from(replacementFileContents), undefined))
.then(result => assert.isFalse(result))
.then(() => docStorage.getFileInfo("hello_world.txt"))
.then(fileInfo => assert.equal(fileInfo.data.toString('utf8'), replacementFileContents));
Expand Down

0 comments on commit 5372657

Please sign in to comment.