Skip to content

Commit 62c1c78

Browse files
committed
add helper for committing pending files to bucket on project save
1 parent 852afd2 commit 62c1c78

2 files changed

Lines changed: 64 additions & 15 deletions

File tree

server/controllers/project.controller.js

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Project from '../models/project';
1111
import { User } from '../models/user';
1212
import { resolvePathToFile } from '../utils/filePath';
1313
import { generateFileSystemSafeName } from '../utils/generateFileSystemSafeName';
14+
import { commitPendingFiles } from '../utils/pendingAssets';
1415

1516
const s3Client = new S3Client({
1617
credentials: {
@@ -60,6 +61,11 @@ export async function updateProject(req, res) {
6061
// only allow whitelisted fields so ownership/slug etc can't be overwritten
6162
const allowedFields = ['name', 'files', 'updatedAt', 'visibility'];
6263
const updateData = {};
64+
65+
if (req.body.files !== undefined) {
66+
updateData.files = await commitPendingFiles(req.body.files, req.user.id);
67+
}
68+
6369
allowedFields.forEach((field) => {
6470
if (req.body[field] !== undefined) {
6571
updateData[field] = req.body[field];
@@ -77,21 +83,7 @@ export async function updateProject(req, res) {
7783
)
7884
.populate('user', 'username')
7985
.exec();
80-
if (
81-
req.body.files &&
82-
updatedProject.files.length !== req.body.files.length
83-
) {
84-
const oldFileIds = updatedProject.files.map((file) => file.id);
85-
const newFileIds = req.body.files.map((file) => file.id);
86-
const staleIds = oldFileIds.filter((id) => newFileIds.indexOf(id) === -1);
87-
staleIds.forEach((staleId) => {
88-
updatedProject.files.id(staleId).deleteOne();
89-
});
90-
const savedProject = await updatedProject.save();
91-
res.json(savedProject);
92-
} else {
93-
res.json(updatedProject);
94-
}
86+
res.json(updatedProject);
9587
} catch (error) {
9688
console.error(error);
9789
res.status(500).json({ success: false });

server/utils/pendingAssets.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import {
2+
s3Client,
3+
CopyObjectCommand,
4+
DeleteObjectsCommand
5+
} from '@aws-sdk/client-s3';
6+
7+
export async function commitPendingFiles(files, userId) {
8+
if (!files) {
9+
return [];
10+
}
11+
12+
const s3Base = process.env.S3_BUCKET_URL_BASE;
13+
const s3Bucket = process.env.S3_BUCKET;
14+
15+
return Promise.all(
16+
files.map(async (file) => {
17+
if (!file.url || !file.url.startsWith(s3Base)) {
18+
return [];
19+
}
20+
21+
const assetKey = decodeURIComponent(file.url.slice(s3Base.length));
22+
console.log('asset key: ', assetKey);
23+
24+
if (!assetKey.startsWith(`pending/${userId}/`)) {
25+
return [];
26+
}
27+
28+
const fileName = assetKey.split('/').pop();
29+
const newKey = `${userId}/${fileName}`;
30+
31+
console.log('filename, newKey: ', fileName, newKey);
32+
33+
await s3Client.send(
34+
new CopyObjectCommand({
35+
Bucket: s3Bucket,
36+
CopySource: `${s3Bucket}/${assetKey}`,
37+
Key: newKey,
38+
ACL: 'public-read'
39+
})
40+
);
41+
42+
await s3Client.send(
43+
new DeleteObjectsCommand({
44+
Bucket: s3Bucket,
45+
Delete: { Objects: [{ Key: assetKey }] }
46+
})
47+
);
48+
49+
return {
50+
...file,
51+
url: `${s3Base}${newKey}`
52+
};
53+
})
54+
);
55+
}
56+
57+
export default commitPendingFiles;

0 commit comments

Comments
 (0)