Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
21 changes: 17 additions & 4 deletions client/modules/IDE/actions/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ export function cloneProject(project) {
generateNewIdsForChildren(rootFile, newFiles);

// duplicate all files hosted on S3
const copiedAssetKeys = [];
each(
newFiles,
(file, callback) => {
Expand All @@ -319,10 +320,16 @@ export function cloneProject(project) {
const formParams = {
url: file.url
};
apiClient.post('/S3/copy', formParams).then((response) => {
file.url = response.data.url;
callback(null);
});
apiClient
.post('/S3/copy', formParams)
.then((response) => {
file.url = response.data.url;

const objectKeyFromUrl = file.url.split('/').pop();
copiedAssetKeys.push(objectKeyFromUrl);
callback(null);
})
.catch(callback);
} else {
callback(null);
}
Expand All @@ -343,6 +350,12 @@ export function cloneProject(project) {
dispatch(setNewProject(response.data));
})
.catch((error) => {
copiedAssetKeys.forEach((assetKey) => {
apiClient.delete(
`/S3/delete?objectKey=${encodeURIComponent(assetKey)}`
);
});

dispatch({
type: ActionTypes.PROJECT_SAVE_FAIL,
error: error?.response?.data
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/aws.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export async function signS3(req, res) {
const acl = 'public-read';
const policy = S3Policy.generate({
acl,
key: `${req.body.userId}/${filename}`,
key: `pending/${req.user.id}/${filename}`,
bucket: process.env.S3_BUCKET,
contentType: req.body.type,
region: process.env.AWS_REGION,
Expand Down
22 changes: 7 additions & 15 deletions server/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Project from '../models/project';
import { User } from '../models/user';
import { resolvePathToFile } from '../utils/filePath';
import { generateFileSystemSafeName } from '../utils/generateFileSystemSafeName';
import { commitPendingFiles } from '../utils/pendingAssets';

const s3Client = new S3Client({
credentials: {
Expand Down Expand Up @@ -60,6 +61,11 @@ export async function updateProject(req, res) {
// only allow whitelisted fields so ownership/slug etc can't be overwritten
const allowedFields = ['name', 'files', 'updatedAt', 'visibility'];
const updateData = {};

if (req.body.files !== undefined) {
updateData.files = await commitPendingFiles(req.body.files, req.user.id);
}

allowedFields.forEach((field) => {
if (req.body[field] !== undefined) {
updateData[field] = req.body[field];
Expand All @@ -77,21 +83,7 @@ export async function updateProject(req, res) {
)
.populate('user', 'username')
.exec();
if (
req.body.files &&
updatedProject.files.length !== req.body.files.length
) {
const oldFileIds = updatedProject.files.map((file) => file.id);
const newFileIds = req.body.files.map((file) => file.id);
const staleIds = oldFileIds.filter((id) => newFileIds.indexOf(id) === -1);
staleIds.forEach((staleId) => {
updatedProject.files.id(staleId).deleteOne();
});
const savedProject = await updatedProject.save();
res.json(savedProject);
} else {
res.json(updatedProject);
}
res.json(updatedProject);
} catch (error) {
console.error(error);
res.status(500).json({ success: false });
Expand Down
57 changes: 57 additions & 0 deletions server/utils/pendingAssets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {
s3Client,
CopyObjectCommand,
DeleteObjectsCommand
} from '@aws-sdk/client-s3';

export async function commitPendingFiles(files, userId) {
if (!files) {
return [];
}

const s3Base = process.env.S3_BUCKET_URL_BASE;
const s3Bucket = process.env.S3_BUCKET;

return Promise.all(
files.map(async (file) => {
if (!file.url || !file.url.startsWith(s3Base)) {
return [];
}

const assetKey = decodeURIComponent(file.url.slice(s3Base.length));
console.log('asset key: ', assetKey);

if (!assetKey.startsWith(`pending/${userId}/`)) {
return [];
}

const fileName = assetKey.split('/').pop();
const newKey = `${userId}/${fileName}`;

console.log('filename, newKey: ', fileName, newKey);

await s3Client.send(
new CopyObjectCommand({
Bucket: s3Bucket,
CopySource: `${s3Bucket}/${assetKey}`,
Key: newKey,
ACL: 'public-read'
})
);

await s3Client.send(
new DeleteObjectsCommand({
Bucket: s3Bucket,
Delete: { Objects: [{ Key: assetKey }] }
})
);

return {
...file,
url: `${s3Base}${newKey}`
};
})
);
}

export default commitPendingFiles;
Loading