良い感じにした

This commit is contained in:
syuilo 2018-08-10 14:33:34 +09:00
parent 6f20a2fff2
commit 29e8264907
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');
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 const DriveFileChunk = monkDb.get('driveFiles.chunks');

View file

@ -294,16 +294,35 @@ export default async function(
metadata.uri = uri;
}
const driveFile = isLink
? await DriveFile.insert({
length: 0,
uploadDate: new Date(),
md5: hash,
filename: detectedName,
metadata: metadata,
contentType: mime
})
: await (save(fs.createReadStream(path), detectedName, mime, hash, size, metadata));
let driveFile: IDriveFile;
if (isLink) {
try {
driveFile = await DriveFile.insert({
length: 0,
uploadDate: new Date(),
md5: hash,
filename: detectedName,
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}`);