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

fix: New messages export overwrites previous one from the same day when using Amazon S3 #32062

Merged
merged 15 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions .changeset/twelve-seas-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

Added file id to message exports zip file names in order to avoid overwriting previous exports generated in the same day when using external storage services (such as Amazon S3)
matheusbsilva137 marked this conversation as resolved.
Show resolved Hide resolved
101 changes: 101 additions & 0 deletions apps/meteor/server/lib/dataExport/uploadZipFile.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { expect } from 'chai';
import proxyquire from 'proxyquire';
import sinon from 'sinon';

// Create stubs for dependencies
const stubs = {
findOneUserById: sinon.stub(),
randomId: sinon.stub(),
stat: sinon.stub(),
getStore: sinon.stub(),
insertFileStub: sinon.stub(),
createReadStream: sinon.stub(),
};

const { uploadZipFile } = proxyquire.noCallThru().load('./uploadZipFile.ts', {
'@rocket.chat/models': {
Users: {
findOneById: stubs.findOneUserById,
},
},
'@rocket.chat/random': {
Random: {
id: stubs.randomId,
},
},
'fs/promises': {
stat: stubs.stat,
},
'fs': {
createReadStream: stubs.createReadStream,
},
'../../../app/file-upload/server': {
FileUpload: {
getStore: stubs.getStore,
},
},
});

describe('Export - uploadZipFile', () => {
const randomId = 'random-id';
const fileStat = 100;
const userName = 'John Doe';
const userId = 'user-id';
const filePath = 'random-path';

beforeAll(() => {
stubs.findOneUserById.returns({ name: userName });
matheusbsilva137 marked this conversation as resolved.
Show resolved Hide resolved
stubs.stat.returns(fileStat);
stubs.randomId.returns(randomId);
stubs.getStore.returns({ insert: stubs.insertFileStub });
stubs.insertFileStub.callsFake((details) => ({ _id: details._id, name: details.name }));
});

it('should correctly build file name for json exports', async () => {
const result = await uploadZipFile(filePath, userId, 'json');

expect(stubs.findOneUserById.calledWith(userId)).to.be.true;
expect(stubs.stat.calledWith(filePath)).to.be.true;
expect(stubs.createReadStream.calledWith(filePath)).to.be.true;
expect(stubs.getStore.calledWith('UserDataFiles')).to.be.true;
expect(
stubs.insertFileStub.calledWith(
sinon.match({
_id: randomId,
userId,
type: 'application/zip',
size: fileStat,
}),
),
).to.be.true;

expect(result).to.have.property('_id', randomId);
expect(result).to.have.property('name').that.is.a.string;
const fileName: string = result.name;
expect(fileName.endsWith(`${userId}-data-${randomId}.zip`));
matheusbsilva137 marked this conversation as resolved.
Show resolved Hide resolved
});

it('should correctly build file name for html exports', async () => {
const result = await uploadZipFile(filePath, userId, 'html');

expect(stubs.findOneUserById.calledWith(userId)).to.be.true;
expect(stubs.stat.calledWith(filePath)).to.be.true;
expect(stubs.createReadStream.calledWith(filePath)).to.be.true;
expect(stubs.getStore.calledWith('UserDataFiles')).to.be.true;
expect(
stubs.insertFileStub.calledWith(
sinon.match({
_id: randomId,
userId,
type: 'application/zip',
size: fileStat,
}),
),
).to.be.true;

expect(result).to.have.property('_id', randomId);
expect(result).to.have.property('name').that.is.a.string;
const fileName: string = result.name;
expect(fileName.endsWith(`${userId}-${randomId}.zip`));
matheusbsilva137 marked this conversation as resolved.
Show resolved Hide resolved
});
});
5 changes: 4 additions & 1 deletion apps/meteor/server/lib/dataExport/uploadZipFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { stat } from 'fs/promises';

import type { IUser } from '@rocket.chat/core-typings';
import { Users } from '@rocket.chat/models';
import { Random } from '@rocket.chat/random';

import { FileUpload } from '../../../app/file-upload/server';

Expand All @@ -18,10 +19,12 @@ export const uploadZipFile = async (filePath: string, userId: IUser['_id'], expo

const utcDate = new Date().toISOString().split('T')[0];
const fileSuffix = exportType === 'json' ? '-data' : '';
const fileId = Random.id();

const newFileName = encodeURIComponent(`${utcDate}-${userDisplayName}${fileSuffix}.zip`);
const newFileName = encodeURIComponent(`${utcDate}-${userDisplayName}${fileSuffix}-${fileId}.zip`);

const details = {
_id: fileId,
userId,
type: contentType,
size,
Expand Down
Loading