diff --git a/src/api/common/add-file-to-drive.ts b/src/api/common/add-file-to-drive.ts index f9c22ccacd..e1baf08191 100644 --- a/src/api/common/add-file-to-drive.ts +++ b/src/api/common/add-file-to-drive.ts @@ -144,11 +144,10 @@ export default ( } // Create DriveFile document - const file = await addToGridFS(`${user._id}/${name}`, data, { + const file = await addToGridFS(name, data, { user_id: user._id, folder_id: folder !== null ? folder._id : null, type: mime, - name: name, comment: comment, properties: properties }); diff --git a/src/api/endpoints/drive/files/find.ts b/src/api/endpoints/drive/files/find.ts index 1c818131d7..a1cdf1643e 100644 --- a/src/api/endpoints/drive/files/find.ts +++ b/src/api/endpoints/drive/files/find.ts @@ -24,7 +24,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => { // Issue query const files = await DriveFile .find({ - 'metadata.name': name, + filename: name, 'metadata.user_id': user._id, 'metadata.folder_id': folderId }); diff --git a/src/api/endpoints/drive/files/update.ts b/src/api/endpoints/drive/files/update.ts index d7b858c2ba..f265142c4d 100644 --- a/src/api/endpoints/drive/files/update.ts +++ b/src/api/endpoints/drive/files/update.ts @@ -34,7 +34,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'name' parameter const [name, nameErr] = $(params.name).optional.string().pipe(validateFileName).$; if (nameErr) return rej('invalid name param'); - if (name) file.metadata.name = name; + if (name) file.filename = name; // Get 'folder_id' parameter const [folderId, folderIdErr] = $(params.folder_id).optional.nullable.id().$; @@ -61,7 +61,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => { await DriveFile.update(file._id, { $set: { - 'metadata.name': file.metadata.name, + filename: file.filename, 'metadata.folder_id': file.metadata.folder_id } }); diff --git a/src/api/serializers/drive-file.ts b/src/api/serializers/drive-file.ts index 2af7db5726..57b74cd975 100644 --- a/src/api/serializers/drive-file.ts +++ b/src/api/serializers/drive-file.ts @@ -47,6 +47,7 @@ export default ( _target.id = _file._id; _target.created_at = _file.uploadDate; + _target.name = _file.filename; _target = Object.assign(_target, _file.metadata); diff --git a/src/file/server.ts b/src/file/server.ts index 1152b650be..39c2cdd2a1 100644 --- a/src/file/server.ts +++ b/src/file/server.ts @@ -97,7 +97,7 @@ async function sendFileById(req: express.Request, res: express.Response): Promis const file = await DriveFile.findOne({ _id: fileId }); // validate name - if (req.params.name !== undefined && req.params.name !== file.metadata.name) { + if (req.params.name !== undefined && req.params.name !== file.filename) { res.status(404).send('there is no file has given name'); return; } diff --git a/tools/migration/use-gridfs.js b/tools/migration/use-gridfs.js index d41514416c..148f9be261 100644 --- a/tools/migration/use-gridfs.js +++ b/tools/migration/use-gridfs.js @@ -21,15 +21,17 @@ const migrateToGridFS = async (doc) => { const id = doc._id const buffer = doc.data.buffer const created_at = doc.created_at + const name = doc.name delete doc._id delete doc.created_at delete doc.datasize delete doc.hash delete doc.data + delete doc.name const bucket = await getGridFSBucket() - const added = await writeToGridFS(bucket, buffer, id, `${id}/${doc.name}`, { metadata: doc }) + const added = await writeToGridFS(bucket, buffer, id, name, { metadata: doc }) const result = await DriveFile.update(id, { $set: {