mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-12-23 21:33:08 +02:00
add-file-to-drive - 見通しを良くする
This commit is contained in:
parent
519bb82b03
commit
51faa7a227
1 changed files with 149 additions and 146 deletions
|
@ -32,29 +32,18 @@ const addToGridFS = (name: string, readable: stream.Readable, type: string, meta
|
|||
readable.pipe(writeStream);
|
||||
}));
|
||||
|
||||
/**
|
||||
* Add file to drive
|
||||
*
|
||||
* @param user User who wish to add file
|
||||
* @param file File path or readableStream
|
||||
* @param comment Comment
|
||||
* @param type File type
|
||||
* @param folderId Folder ID
|
||||
* @param force If set to true, forcibly upload the file even if there is a file with the same hash.
|
||||
* @return Object that represents added file
|
||||
*/
|
||||
export default (
|
||||
const addFile = async (
|
||||
user: any,
|
||||
file: string | stream.Readable,
|
||||
name: string = null,
|
||||
comment: string = null,
|
||||
folderId: mongodb.ObjectID = null,
|
||||
force: boolean = false
|
||||
) => new Promise<any>((resolve, reject) => {
|
||||
) => {
|
||||
log(`registering ${name} (user: ${user.username})`);
|
||||
|
||||
// Get file path
|
||||
new Promise((res: (v: string) => void, rej) => {
|
||||
const path = await new Promise((res: (v: string) => void, rej) => {
|
||||
if (typeof file === 'string') {
|
||||
res(file);
|
||||
return;
|
||||
|
@ -75,10 +64,10 @@ export default (
|
|||
.catch(rej);
|
||||
}
|
||||
rej(new Error('un-compatible file.'));
|
||||
})
|
||||
});
|
||||
|
||||
// Calculate hash, get content type and get file size
|
||||
.then(path => Promise.all([
|
||||
path,
|
||||
const [hash, [mime, ext], size] = await Promise.all([
|
||||
// hash
|
||||
((): Promise<string> => new Promise((res, rej) => {
|
||||
const readable = fs.createReadStream(path);
|
||||
|
@ -115,8 +104,8 @@ export default (
|
|||
res(stats.size);
|
||||
});
|
||||
}))()
|
||||
]))
|
||||
.then(async ([path, hash, [mime, ext], size]) => {
|
||||
]);
|
||||
|
||||
log(`hash: ${hash}, mime: ${mime}, ext: ${ext}, size: ${size}`);
|
||||
|
||||
// detect name
|
||||
|
@ -131,7 +120,7 @@ export default (
|
|||
|
||||
if (much !== null) {
|
||||
log('file with same hash is found');
|
||||
return resolve(much);
|
||||
return much;
|
||||
} else {
|
||||
log('file with same hash is not found');
|
||||
}
|
||||
|
@ -209,7 +198,21 @@ export default (
|
|||
comment: comment,
|
||||
properties: properties
|
||||
});
|
||||
})
|
||||
};
|
||||
|
||||
/**
|
||||
* Add file to drive
|
||||
*
|
||||
* @param user User who wish to add file
|
||||
* @param file File path or readableStream
|
||||
* @param comment Comment
|
||||
* @param type File type
|
||||
* @param folderId Folder ID
|
||||
* @param force If set to true, forcibly upload the file even if there is a file with the same hash.
|
||||
* @return Object that represents added file
|
||||
*/
|
||||
export default (user: any, file: string | stream.Readable, ...args) => new Promise<any>((resolve, reject) => {
|
||||
addFile(user, file, ...args)
|
||||
.then(file => {
|
||||
log(`drive file has been created ${file._id}`);
|
||||
resolve(file);
|
||||
|
|
Loading…
Reference in a new issue