mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
@@ -245,10 +245,10 @@ exports.getAssets = async (req, res) => {
|
||||
}
|
||||
|
||||
const data = await attachStreamTokens(result.data, req);
|
||||
return R.success(res, "Assets retrieved.", { ...result, data });
|
||||
return R.success(res, "Files retrieved.", { ...result, data });
|
||||
} catch (err) {
|
||||
console.error("[ASSET][GET ALL]", err);
|
||||
return R.error(res, "Could not retrieve assets.", 500);
|
||||
return R.error(res, "Could not retrieve files.", 500);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -257,7 +257,7 @@ exports.getAssets = async (req, res) => {
|
||||
exports.getAsset = async (req, res) => {
|
||||
try {
|
||||
const { assetId } = req.params;
|
||||
if (!assetId || assetId === "undefined") return R.error(res, "Invalid asset ID.", 400);
|
||||
if (!assetId || assetId === "undefined") return R.error(res, "Invalid file ID.", 400);
|
||||
|
||||
const asset = await Asset.findOne({
|
||||
where: { asset_id: assetId, ...notDeleted },
|
||||
@@ -270,7 +270,7 @@ exports.getAsset = async (req, res) => {
|
||||
],
|
||||
});
|
||||
|
||||
if (!asset) return R.error(res, "Asset not found.", 404);
|
||||
if (!asset) return R.error(res, "File not found.", 404);
|
||||
|
||||
const json = asset.toJSON();
|
||||
|
||||
@@ -298,7 +298,7 @@ exports.getAsset = async (req, res) => {
|
||||
delete json.storage_key;
|
||||
|
||||
redactS3Url(json);
|
||||
return R.success(res, "Asset retrieved.", { data: json });
|
||||
return R.success(res, "File retrieved.", { data: json });
|
||||
} catch (err) {
|
||||
console.error("[ASSET][GET ONE]", err);
|
||||
return R.error(res, "Internal server error.", 500);
|
||||
@@ -578,7 +578,7 @@ exports.uploadAsset = async (req, res) => {
|
||||
const { storage_key, thumbnail_storage_key, original_name } = req.body;
|
||||
const asset = await finalizeAssetFromStorage({ storage_key, thumbnail_storage_key, original_name, body: req.body, user: req.user });
|
||||
invalidateListCache();
|
||||
return R.success(res, "Asset uploaded.", { data: asset }, 201);
|
||||
return R.success(res, "File uploaded.", { data: asset }, 201);
|
||||
|
||||
} catch (err) {
|
||||
console.error("[ASSET][UPLOAD]", err);
|
||||
@@ -594,10 +594,10 @@ exports.updateAsset = async (req, res) => {
|
||||
|
||||
try {
|
||||
const { assetId } = req.params;
|
||||
if (!assetId || assetId === "undefined") return R.error(res, "Invalid asset ID.", 400);
|
||||
if (!assetId || assetId === "undefined") return R.error(res, "Invalid file ID.", 400);
|
||||
|
||||
const asset = await Asset.findOne({ where: { asset_id: assetId, ...notDeleted } });
|
||||
if (!asset) return R.error(res, "Asset not found.", 404);
|
||||
if (!asset) return R.error(res, "File not found.", 404);
|
||||
|
||||
// Browser already PUT the replacement file straight to storage via a
|
||||
// presigned URL (see presignAssetUpload) — this is plain JSON, no
|
||||
@@ -607,7 +607,7 @@ exports.updateAsset = async (req, res) => {
|
||||
const isDocument = asset.file_type === "document";
|
||||
|
||||
if (isDocument && storage_key) return R.error(res, "Document files cannot be replaced.", 400);
|
||||
if (isVideo && storage_key && !req.body.is_thumbnail) return R.error(res, "Video files cannot be replaced. Upload a new asset instead.", 400);
|
||||
if (isVideo && storage_key && !req.body.is_thumbnail) return R.error(res, "Video files cannot be replaced. Upload a new file instead.", 400);
|
||||
|
||||
const storageProvider = asset.storage_provider;
|
||||
const usesProvider = ["chibisafe", "s3"].includes(storageProvider);
|
||||
@@ -641,7 +641,7 @@ exports.updateAsset = async (req, res) => {
|
||||
|
||||
invalidateListCache();
|
||||
logActivity(req.user?.user_id, 'update_asset', { entityType: 'asset', entityId: Number(assetId) });
|
||||
return R.success(res, "Asset updated.", { data: asset });
|
||||
return R.success(res, "File updated.", { data: asset });
|
||||
|
||||
} catch (err) {
|
||||
if (newUpload) await rollbackUploads([newUpload]);
|
||||
@@ -656,16 +656,16 @@ exports.updateAsset = async (req, res) => {
|
||||
exports.archiveAsset = async (req, res) => {
|
||||
try {
|
||||
const { assetId } = req.params;
|
||||
if (!assetId || assetId === "undefined") return R.error(res, "Invalid asset ID.", 400);
|
||||
if (!assetId || assetId === "undefined") return R.error(res, "Invalid file ID.", 400);
|
||||
|
||||
const asset = await Asset.findOne({ where: { asset_id: assetId, ...notDeleted } });
|
||||
if (!asset) return R.error(res, "Asset not found.", 404);
|
||||
if (!asset) return R.error(res, "File not found.", 404);
|
||||
|
||||
await asset.update({ deletedBy: req.body.deletedBy ?? null });
|
||||
await asset.destroy();
|
||||
invalidateListCache();
|
||||
logActivity(req.user?.user_id, 'archive_asset', { entityType: 'asset', entityId: Number(assetId) });
|
||||
return R.success(res, "Asset archived.");
|
||||
return R.success(res, "File archived.");
|
||||
} catch (err) {
|
||||
console.error("[ASSET][ARCHIVE]", err);
|
||||
return R.error(res, "Internal server error.", 500);
|
||||
@@ -680,7 +680,7 @@ exports.archiveAssets = async (req, res) => {
|
||||
if (!Array.isArray(ids) || !ids.length) return R.error(res, "ids must be a non-empty array.", 400);
|
||||
|
||||
const assets = await Asset.findAll({ where: { asset_id: { [Op.in]: ids }, ...notDeleted } });
|
||||
if (!assets.length) return R.error(res, "No assets found.", 404);
|
||||
if (!assets.length) return R.error(res, "No files found.", 404);
|
||||
|
||||
const activeIds = assets.map((a) => a.asset_id);
|
||||
|
||||
@@ -689,7 +689,7 @@ exports.archiveAssets = async (req, res) => {
|
||||
|
||||
invalidateListCache();
|
||||
logActivity(req.user?.user_id, 'bulk_archive_assets', { entityType: 'asset', details: { ids: activeIds, count: activeIds.length } });
|
||||
return R.success(res, `${activeIds.length} asset(s) archived.`, {
|
||||
return R.success(res, `${activeIds.length} file(s) archived.`, {
|
||||
archived_ids: activeIds,
|
||||
skipped_ids: ids.filter((id) => !activeIds.includes(id)),
|
||||
});
|
||||
@@ -706,14 +706,14 @@ exports.restoreAsset = async (req, res) => {
|
||||
const { assetId } = req.params;
|
||||
|
||||
const asset = await Asset.findOne({ where: { asset_id: assetId }, paranoid: false });
|
||||
if (!asset) return R.error(res, "Asset not found.", 404);
|
||||
if (!asset.deletedAt) return R.error(res, "Asset is not archived.", 400);
|
||||
if (!asset) return R.error(res, "File not found.", 404);
|
||||
if (!asset.deletedAt) return R.error(res, "File is not archived.", 400);
|
||||
|
||||
await asset.restore();
|
||||
await asset.update({ deletedBy: null });
|
||||
invalidateListCache();
|
||||
logActivity(req.user?.user_id, 'restore_asset', { entityType: 'asset', entityId: Number(assetId) });
|
||||
return R.success(res, "Asset restored.", { data: asset });
|
||||
return R.success(res, "File restored.", { data: asset });
|
||||
} catch (err) {
|
||||
console.error("[ASSET][RESTORE]", err);
|
||||
return R.error(res, "Internal server error.", 500);
|
||||
@@ -728,10 +728,10 @@ exports.restoreAssets = async (req, res) => {
|
||||
if (!Array.isArray(ids) || !ids.length) return R.error(res, "ids must be a non-empty array.", 400);
|
||||
|
||||
const assets = await Asset.findAll({ where: { asset_id: { [Op.in]: ids } }, paranoid: false });
|
||||
if (!assets.length) return R.error(res, "No assets found.", 404);
|
||||
if (!assets.length) return R.error(res, "No files found.", 404);
|
||||
|
||||
const archivedAssets = assets.filter((a) => a.deletedAt);
|
||||
if (!archivedAssets.length) return R.error(res, "All selected assets are already active.", 400);
|
||||
if (!archivedAssets.length) return R.error(res, "All selected files are already active.", 400);
|
||||
|
||||
const archivedIds = archivedAssets.map((a) => a.asset_id);
|
||||
|
||||
@@ -740,7 +740,7 @@ exports.restoreAssets = async (req, res) => {
|
||||
|
||||
invalidateListCache();
|
||||
logActivity(req.user?.user_id, 'bulk_restore_assets', { entityType: 'asset', details: { ids: archivedIds, count: archivedIds.length } });
|
||||
return R.success(res, `${archivedIds.length} asset(s) restored.`, {
|
||||
return R.success(res, `${archivedIds.length} file(s) restored.`, {
|
||||
restored_ids: archivedIds,
|
||||
skipped_ids: ids.filter((id) => !archivedIds.includes(id)),
|
||||
});
|
||||
@@ -757,8 +757,8 @@ exports.permanentlyDeleteAsset = async (req, res) => {
|
||||
const { assetId } = req.params;
|
||||
|
||||
const asset = await Asset.findOne({ where: { asset_id: assetId }, paranoid: false });
|
||||
if (!asset) return R.error(res, "Asset not found.", 404);
|
||||
if (!asset.deletedAt) return R.error(res, "Asset must be archived before it can be permanently deleted.", 400);
|
||||
if (!asset) return R.error(res, "File not found.", 404);
|
||||
if (!asset.deletedAt) return R.error(res, "File must be archived before it can be permanently deleted.", 400);
|
||||
|
||||
const { storage_provider, storage_key, thumbnail_storage_key } = asset;
|
||||
|
||||
@@ -778,7 +778,7 @@ exports.permanentlyDeleteAsset = async (req, res) => {
|
||||
|
||||
invalidateListCache();
|
||||
logActivity(req.user?.user_id, 'permanently_delete_asset', { entityType: 'asset', entityId: Number(assetId) });
|
||||
return R.success(res, "Asset permanently deleted.");
|
||||
return R.success(res, "File permanently deleted.");
|
||||
} catch (err) {
|
||||
console.error("[ASSET][PERMANENT DELETE]", err);
|
||||
return R.error(res, "Internal server error.", 500);
|
||||
@@ -793,10 +793,10 @@ exports.permanentlyDeleteAssets = async (req, res) => {
|
||||
if (!Array.isArray(ids) || !ids.length) return R.error(res, "ids must be a non-empty array.", 400);
|
||||
|
||||
const assets = await Asset.findAll({ where: { asset_id: { [Op.in]: ids } }, paranoid: false });
|
||||
if (!assets.length) return R.error(res, "No assets found.", 404);
|
||||
if (!assets.length) return R.error(res, "No files found.", 404);
|
||||
|
||||
const archivedAssets = assets.filter((a) => a.deletedAt);
|
||||
if (!archivedAssets.length) return R.error(res, "All selected assets must be archived before they can be permanently deleted.", 400);
|
||||
if (!archivedAssets.length) return R.error(res, "All selected files must be archived before they can be permanently deleted.", 400);
|
||||
|
||||
const archivedIds = archivedAssets.map((a) => a.asset_id);
|
||||
|
||||
@@ -817,7 +817,7 @@ exports.permanentlyDeleteAssets = async (req, res) => {
|
||||
|
||||
invalidateListCache();
|
||||
logActivity(req.user?.user_id, 'bulk_permanently_delete_assets', { entityType: 'asset', details: { ids: archivedIds, count: archivedIds.length } });
|
||||
return R.success(res, `${archivedIds.length} asset(s) permanently deleted.`, {
|
||||
return R.success(res, `${archivedIds.length} file(s) permanently deleted.`, {
|
||||
deleted_ids: archivedIds,
|
||||
skipped_ids: ids.filter((id) => !archivedIds.includes(id)),
|
||||
});
|
||||
@@ -840,10 +840,10 @@ exports.getArchivedAssets = async (req, res) => {
|
||||
findOptions: { paranoid: false, where: { deletedAt: { [Op.ne]: null } } },
|
||||
});
|
||||
result.data = result.data.map(redactS3Url);
|
||||
return R.success(res, "Archived assets retrieved.", result);
|
||||
return R.success(res, "Archived files retrieved.", result);
|
||||
} catch (err) {
|
||||
console.error("[ASSET][GET ARCHIVED]", err);
|
||||
return R.error(res, "Could not retrieve archived assets.", 500);
|
||||
return R.error(res, "Could not retrieve archived files.", 500);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user