mirror of
https://github.com/strapi/strapi.git
synced 2025-10-21 13:02:46 +00:00
use a transaction for bulk move
This commit is contained in:
parent
cd69fca6f9
commit
71f43f0f7a
@ -1,5 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const { joinBy } = require('@strapi/utils');
|
||||||
const { getService } = require('../utils');
|
const { getService } = require('../utils');
|
||||||
const { ACTIONS, FOLDER_MODEL_UID, FILE_MODEL_UID } = require('../constants');
|
const { ACTIONS, FOLDER_MODEL_UID, FILE_MODEL_UID } = require('../constants');
|
||||||
const {
|
const {
|
||||||
@ -43,7 +44,7 @@ module.exports = {
|
|||||||
async moveMany(ctx) {
|
async moveMany(ctx) {
|
||||||
const { body } = ctx.request;
|
const { body } = ctx.request;
|
||||||
const {
|
const {
|
||||||
state: { userAbility, user },
|
state: { userAbility },
|
||||||
} = ctx;
|
} = ctx;
|
||||||
|
|
||||||
const pmFolder = strapi.admin.services.permission.createPermissionsManager({
|
const pmFolder = strapi.admin.services.permission.createPermissionsManager({
|
||||||
@ -60,25 +61,136 @@ module.exports = {
|
|||||||
await validateMoveManyFoldersFiles(body);
|
await validateMoveManyFoldersFiles(body);
|
||||||
const { folderIds = [], fileIds = [], destinationFolderId } = body;
|
const { folderIds = [], fileIds = [], destinationFolderId } = body;
|
||||||
|
|
||||||
const uploadService = getService('upload');
|
const trx = await strapi.db.transaction();
|
||||||
const folderService = getService('folder');
|
try {
|
||||||
|
// fetch folders
|
||||||
|
const existingFolders = await strapi.db
|
||||||
|
.queryBuilder(FOLDER_MODEL_UID)
|
||||||
|
.select(['id', 'uid', 'path'])
|
||||||
|
.where({ id: { $in: folderIds } })
|
||||||
|
.transacting(trx)
|
||||||
|
.forUpdate()
|
||||||
|
.execute();
|
||||||
|
|
||||||
const updatedFolders = [];
|
// fetch files
|
||||||
// updates are done in order (not in parallele) to avoid mixing queries (path)
|
const existingFiles = await strapi.db
|
||||||
for (let folderId of folderIds) {
|
.queryBuilder(FILE_MODEL_UID)
|
||||||
const updatedFolder = await folderService.update(
|
.select(['id'])
|
||||||
folderId,
|
.where({ id: { $in: fileIds } })
|
||||||
{ parent: destinationFolderId },
|
.transacting(trx)
|
||||||
{ user }
|
.forUpdate()
|
||||||
);
|
.execute();
|
||||||
updatedFolders.push(updatedFolder);
|
|
||||||
|
// fetch destinationFolder path
|
||||||
|
let destinationFolderPath = '/';
|
||||||
|
if (destinationFolderId !== null) {
|
||||||
|
const destinationFolder = await strapi.db
|
||||||
|
.queryBuilder(FOLDER_MODEL_UID)
|
||||||
|
.select('path')
|
||||||
|
.where({ id: destinationFolderId })
|
||||||
|
.transacting(trx)
|
||||||
|
.first()
|
||||||
|
.execute();
|
||||||
|
destinationFolderPath = destinationFolder.path;
|
||||||
|
}
|
||||||
|
|
||||||
|
const fileTable = strapi.getModel(FILE_MODEL_UID).collectionName;
|
||||||
|
const folderTable = strapi.getModel(FOLDER_MODEL_UID).collectionName;
|
||||||
|
const folderPathColName = strapi.db.metadata.get(FILE_MODEL_UID).attributes.folderPath
|
||||||
|
.columnName;
|
||||||
|
const pathColName = strapi.db.metadata.get(FOLDER_MODEL_UID).attributes.path.columnName;
|
||||||
|
|
||||||
|
if (existingFolders.length > 0) {
|
||||||
|
// update folders' parent relation (delete + insert; upsert not possible)
|
||||||
|
const joinTable = strapi.db.metadata.get(FOLDER_MODEL_UID).attributes.parent.joinTable;
|
||||||
|
await strapi.db
|
||||||
|
.queryBuilder(joinTable.name)
|
||||||
|
.transacting(trx)
|
||||||
|
.delete()
|
||||||
|
.where({ [joinTable.joinColumn.name]: { $in: folderIds } })
|
||||||
|
.execute();
|
||||||
|
await strapi.db
|
||||||
|
.queryBuilder(joinTable.name)
|
||||||
|
.transacting(trx)
|
||||||
|
.insert(
|
||||||
|
existingFolders.map(folder => ({
|
||||||
|
[joinTable.inverseJoinColumn.name]: destinationFolderId,
|
||||||
|
[joinTable.joinColumn.name]: folder.id,
|
||||||
|
}))
|
||||||
|
)
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
for (const existingFolder of existingFolders) {
|
||||||
|
// update path for folders themselves & folders below
|
||||||
|
await strapi.db
|
||||||
|
.connection(folderTable)
|
||||||
|
.transacting(trx)
|
||||||
|
.where(pathColName, 'like', `${existingFolder.path}%`)
|
||||||
|
.update(
|
||||||
|
pathColName,
|
||||||
|
strapi.db.connection.raw('REPLACE(??, ?, ?)', [
|
||||||
|
pathColName,
|
||||||
|
existingFolder.path,
|
||||||
|
joinBy('/', destinationFolderPath, existingFolder.uid),
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
|
// update path of files below
|
||||||
|
await strapi.db
|
||||||
|
.connection(fileTable)
|
||||||
|
.transacting(trx)
|
||||||
|
.where(folderPathColName, 'like', `${existingFolder.path}%`)
|
||||||
|
.update(
|
||||||
|
folderPathColName,
|
||||||
|
strapi.db.connection.raw('REPLACE(??, ?, ?)', [
|
||||||
|
folderPathColName,
|
||||||
|
existingFolder.path,
|
||||||
|
joinBy('/', destinationFolderPath, existingFolder.uid),
|
||||||
|
])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (existingFiles.length > 0) {
|
||||||
|
// update files' folder relation (delete + insert; upsert not possible)
|
||||||
|
const fileJoinTable = strapi.db.metadata.get(FILE_MODEL_UID).attributes.folder.joinTable;
|
||||||
|
await strapi.db
|
||||||
|
.queryBuilder(fileJoinTable.name)
|
||||||
|
.transacting(trx)
|
||||||
|
.delete()
|
||||||
|
.where({ [fileJoinTable.joinColumn.name]: { $in: fileIds } })
|
||||||
|
.execute();
|
||||||
|
await strapi.db
|
||||||
|
.queryBuilder(fileJoinTable.name)
|
||||||
|
.transacting(trx)
|
||||||
|
.insert(
|
||||||
|
existingFiles.map(file => ({
|
||||||
|
[fileJoinTable.inverseJoinColumn.name]: destinationFolderId,
|
||||||
|
[fileJoinTable.joinColumn.name]: file.id,
|
||||||
|
}))
|
||||||
|
)
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
// update files main fields (path + updatedBy)
|
||||||
|
await strapi.db
|
||||||
|
.connection(fileTable)
|
||||||
|
.transacting(trx)
|
||||||
|
.whereIn('id', fileIds)
|
||||||
|
.update(folderPathColName, destinationFolderPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
await trx.commit();
|
||||||
|
} catch (e) {
|
||||||
|
await trx.rollback();
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
const updatedFiles = await Promise.all(
|
const updatedFolders = await strapi.entityService.findMany(FOLDER_MODEL_UID, {
|
||||||
fileIds.map(fileId =>
|
filters: { id: { $in: folderIds } },
|
||||||
uploadService.updateFileInfo(fileId, { folder: destinationFolderId }, { user })
|
});
|
||||||
)
|
const updatedFiles = await strapi.entityService.findMany(FILE_MODEL_UID, {
|
||||||
);
|
filters: { id: { $in: fileIds } },
|
||||||
|
});
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
data: {
|
data: {
|
||||||
|
@ -102,12 +102,18 @@ const update = async (id, { name, parent }, { user }) => {
|
|||||||
.first()
|
.first()
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
// update parent folder
|
// update parent folder (delete + insert; upsert not possible)
|
||||||
const joinTable = strapi.db.metadata.get(FOLDER_MODEL_UID).attributes.parent.joinTable;
|
const joinTable = strapi.db.metadata.get(FOLDER_MODEL_UID).attributes.parent.joinTable;
|
||||||
await strapi.db
|
await strapi.db
|
||||||
.queryBuilder(joinTable.name)
|
.queryBuilder(joinTable.name)
|
||||||
.transacting(trx)
|
.transacting(trx)
|
||||||
.update({ [joinTable.inverseJoinColumn.name]: parent })
|
.delete()
|
||||||
|
.where({ [joinTable.joinColumn.name]: id })
|
||||||
|
.execute();
|
||||||
|
await strapi.db
|
||||||
|
.queryBuilder(joinTable.name)
|
||||||
|
.transacting(trx)
|
||||||
|
.insert({ [joinTable.inverseJoinColumn.name]: parent, [joinTable.joinColumn.name]: id })
|
||||||
.where({ [joinTable.joinColumn.name]: id })
|
.where({ [joinTable.joinColumn.name]: id })
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user