Skip to content

Commit 80e66c0

Browse files
committed
Drop use of deprecated getUniqueFileName
* Switched to getUniqueSecureFilePath which is the recommended function here * See: https://github.com/TryGhost/Ghost-Storage-Base/blob/c78655b1cd54c93b53327bc8e1cf3123c085ebd2/BaseStorage.js#L131
1 parent 1f96105 commit 80e66c0

File tree

2 files changed

+37
-32
lines changed

2 files changed

+37
-32
lines changed

index.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,18 @@ class ObjectStoreStorage extends StorageBase {
6464
async save(file, targetDir) {
6565
// NOTE: the base implementation of `getTargetDir` returns the format this.storagePath/YYYY/MM
6666
const storagePath = targetDir || this.getTargetDir(this.storagePath);
67-
68-
// NOTE: getUniqueFileName is deprecated in v1.1.1 of ghost-storage-base, it is recommended to use getUniqueSecureFilePath instead.
69-
// See: https://github.com/TryGhost/Ghost-Storage-Base/blob/c78655b1cd54c93b53327bc8e1cf3123c085ebd2/BaseStorage.js#L131
70-
// However, calling that method results in files being corrupted in Object Store.
71-
//
72-
// TODO: Figure out why getUniqueSecureFilePath corrupts files and switch to using it.
73-
const fileName = await this.getUniqueFileName(file, storagePath);
74-
67+
const fileName = await this.getUniqueSecureFilePath(file, storagePath);
7568
const objectKey = fileName.replace(/\\/g, '/');
7669

70+
// The file object contains metadata but not the actual file content
71+
// We need to read the file from the filesystem path to get the content
72+
const fs = require('fs').promises;
73+
const fileContent = await fs.readFile(file.path);
74+
7775
const command = new PutObjectCommand({
7876
Bucket: this.bucket,
7977
Key: objectKey,
80-
Body: file.contents || file.path,
78+
Body: fileContent,
8179
ContentType: file.type
8280
});
8381

test/index.spec.js

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,41 +58,48 @@ describe('ObjectStoreStorage', () => {
5858
});
5959

6060
describe('save', () => {
61-
it('should save a file to Object Store and return the URL', async () => {
62-
const mockFile = {
63-
contents: Buffer.from('test content'),
64-
type: 'image/jpeg',
65-
path: '/local/path/to/file.jpg'
66-
};
61+
let fs;
62+
const mockFile = {
63+
fieldname: 'file',
64+
originalname: '0-umnaaktb3eos233i.png',
65+
encoding: '7bit',
66+
mimetype: 'image/png',
67+
destination: '/tmp',
68+
filename: '3bab289aad1a167c811b5767350facdf',
69+
path: '/tmp/3bab289aad1a167c811b5767350facdf',
70+
size: 7158,
71+
name: '0-umnaaktb3eos233i.png',
72+
type: 'image/png',
73+
ext: '.png'
74+
};
6775

68-
const mockUniqueFileName = 'test-file-123.jpg';
69-
const mockUrl = '/test-file-123.jpg';
76+
beforeEach(() => {
77+
fs = require('fs');
78+
fs.promises.readFile = jest.fn();
79+
});
7080

71-
// Mock the getUniqueFileName method to return a test filename
72-
objectStoreStorage.getUniqueFileName = jest.fn().mockResolvedValue(mockUniqueFileName);
81+
it('should save a file to Object Store and return the URL', async () => {
82+
const mockFileContent = Buffer.from('test file content');
7383

74-
// Mock the S3 client send to resolve with successful response
84+
fs.promises.readFile.mockResolvedValue(mockFileContent);
85+
objectStoreStorage.getUniqueSecureFilePath = jest.fn().mockResolvedValue(mockFile.name);
7586
mockS3Client.send.mockResolvedValue({});
7687

7788
const result = await objectStoreStorage.save(mockFile, '/images');
7889

79-
expect(objectStoreStorage.getUniqueFileName).toHaveBeenCalledWith(mockFile, '/images');
90+
expect(fs.promises.readFile).toHaveBeenCalledWith(mockFile.path);
91+
expect(objectStoreStorage.getUniqueSecureFilePath).toHaveBeenCalledWith(mockFile, '/images');
8092
expect(mockS3Client.send).toHaveBeenCalledWith(expect.any(PutObjectCommand));
81-
expect(result).toBe(mockUrl);
93+
94+
expect(result).toBe(`/${mockFile.name}`);
8295
});
8396

8497
it('should handle save errors properly', async () => {
85-
const mockFile = {
86-
contents: Buffer.from('test content'),
87-
type: 'image/jpeg',
88-
path: '/local/path/to/file.jpg'
89-
};
90-
const mockUniqueFileName = 'test-file-123.jpg';
91-
92-
// Mock the getUniqueFileName method to return a test filename
93-
objectStoreStorage.getUniqueFileName = jest.fn().mockResolvedValue(mockUniqueFileName);
98+
const mockFileContent = Buffer.from('test file content');
99+
const mockUniqueFileName = 'test-file-123.png';
94100

95-
// Mock an error from the S3 client
101+
fs.promises.readFile.mockResolvedValue(mockFileContent);
102+
objectStoreStorage.getUniqueSecureFilePath = jest.fn().mockResolvedValue(mockUniqueFileName);
96103
mockS3Client.send.mockRejectedValue(new Error('Save failed'));
97104

98105
await expect(objectStoreStorage.save(mockFile, '/images')).rejects.toThrow('Failed to save file to Object Store');

0 commit comments

Comments
 (0)