良い感じにした

This commit is contained in:
syuilo 2018-08-10 14:33:34 +09:00
parent 6372451d17
commit ab7725ff69
2 changed files with 30 additions and 11 deletions

View File

@ -10,7 +10,7 @@ import DriveFileThumbnail, { deleteDriveFileThumbnail } from './drive-file-thumb
const DriveFile = monkDb.get<IDriveFile>('driveFiles.files'); const DriveFile = monkDb.get<IDriveFile>('driveFiles.files');
DriveFile.createIndex('md5'); DriveFile.createIndex('md5');
DriveFile.createIndex('metadata.uri', { sparse: true, unique: true }); DriveFile.createIndex(['metadata.uri', 'metadata.userId'], { sparse: true, unique: true });
export default DriveFile; export default DriveFile;
export const DriveFileChunk = monkDb.get('driveFiles.chunks'); export const DriveFileChunk = monkDb.get('driveFiles.chunks');

View File

@ -294,16 +294,35 @@ export default async function(
metadata.uri = uri; metadata.uri = uri;
} }
const driveFile = isLink let driveFile: IDriveFile;
? await DriveFile.insert({
length: 0, if (isLink) {
uploadDate: new Date(), try {
md5: hash, driveFile = await DriveFile.insert({
filename: detectedName, length: 0,
metadata: metadata, uploadDate: new Date(),
contentType: mime md5: hash,
}) filename: detectedName,
: await (save(fs.createReadStream(path), detectedName, mime, hash, size, metadata)); metadata: metadata,
contentType: mime
});
} catch (e) {
// duplicate key error (when already registered)
if (e.code === 11000) {
log(`already registered ${metadata.uri}`);
driveFile = await DriveFile.findOne({
'metadata.uri': metadata.uri,
'metadata.userId': user._id
});
} else {
console.error(e);
throw e;
}
}
} else {
driveFile = await (save(fs.createReadStream(path), detectedName, mime, hash, size, metadata));
}
log(`drive file has been created ${driveFile._id}`); log(`drive file has been created ${driveFile._id}`);