rudeshark.net/src/server/api/endpoints/drive/files/check_existence.ts
sei0o inoue 3e897727ca Fix #2773 (#2841)
* Added an API endpoint to check the existence of the file

* fix #2773: Now we can prevent users from posting the same images

* bug fix
2018-10-07 16:51:46 +09:00

39 lines
920 B
TypeScript

import $ from 'cafy';
import DriveFile, { pack } from '../../../../../models/drive-file';
import { ILocalUser } from '../../../../../models/user';
export const meta = {
desc: {
'ja-JP': '与えられたMD5ハッシュ値を持つファイルがドライブに存在するかどうかを返します。',
'en-US': 'Returns whether the file with the given MD5 hash exists in the user\'s drive.'
},
requireCredential: true,
kind: 'drive-read',
params: {
md5: $.str.note({
desc: {
'ja-JP': 'ファイルのMD5ハッシュ'
}
})
}
};
export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
const [md5, md5Err] = $.str.get(params.md5);
if (md5Err) return rej('invalid md5 param');
const file = await DriveFile.findOne({
md5: md5,
'metadata.userId': user._id
});
if (file === null) {
res({ file: null });
} else {
res({ file: await pack(file) });
}
});