Refactor
This commit is contained in:
parent
c7f322b327
commit
ad18136b04
2 changed files with 13 additions and 19 deletions
|
@ -23,10 +23,10 @@ const gm = _gm.subClass({
|
||||||
|
|
||||||
const log = debug('misskey:drive:add-file');
|
const log = debug('misskey:drive:add-file');
|
||||||
|
|
||||||
const tmpFile = (): Promise<string> => new Promise((resolve, reject) => {
|
const tmpFile = (): Promise<[string, any]> => new Promise((resolve, reject) => {
|
||||||
tmp.file((e, path) => {
|
tmp.file((e, path, fd, cleanup) => {
|
||||||
if (e) return reject(e);
|
if (e) return reject(e);
|
||||||
resolve(path);
|
resolve([path, cleanup]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -254,18 +254,18 @@ export default (user: any, file: string | stream.Readable, ...args) => new Promi
|
||||||
const isStream = typeof file === 'object' && typeof file.read === 'function';
|
const isStream = typeof file === 'object' && typeof file.read === 'function';
|
||||||
|
|
||||||
// Get file path
|
// Get file path
|
||||||
new Promise<string>((res, rej) => {
|
new Promise<[string, any]>((res, rej) => {
|
||||||
if (typeof file === 'string') {
|
if (typeof file === 'string') {
|
||||||
res(file);
|
res([file, null]);
|
||||||
} else if (isStream) {
|
} else if (isStream) {
|
||||||
tmpFile()
|
tmpFile()
|
||||||
.then(path => {
|
.then(([path, cleanup]) => {
|
||||||
const readable: stream.Readable = file;
|
const readable: stream.Readable = file;
|
||||||
const writable = fs.createWriteStream(path);
|
const writable = fs.createWriteStream(path);
|
||||||
readable
|
readable
|
||||||
.on('error', rej)
|
.on('error', rej)
|
||||||
.on('end', () => {
|
.on('end', () => {
|
||||||
res(path);
|
res([path, cleanup]);
|
||||||
})
|
})
|
||||||
.pipe(writable)
|
.pipe(writable)
|
||||||
.on('error', rej);
|
.on('error', rej);
|
||||||
|
@ -275,15 +275,11 @@ export default (user: any, file: string | stream.Readable, ...args) => new Promi
|
||||||
rej(new Error('un-compatible file.'));
|
rej(new Error('un-compatible file.'));
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then(path => new Promise<IDriveFile>((res, rej) => {
|
.then(([path, cleanup]) => new Promise<IDriveFile>((res, rej) => {
|
||||||
addFile(user, path, ...args)
|
addFile(user, path, ...args)
|
||||||
.then(file => {
|
.then(file => {
|
||||||
res(file);
|
res(file);
|
||||||
if (isStream) {
|
if (cleanup) cleanup();
|
||||||
fs.unlink(path, e => {
|
|
||||||
if (e) console.error(e.stack);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.catch(rej);
|
.catch(rej);
|
||||||
}))
|
}))
|
||||||
|
|
|
@ -19,10 +19,10 @@ export default async (url, user, folderId = null, uri = null): Promise<IDriveFil
|
||||||
log(`name: ${name}`);
|
log(`name: ${name}`);
|
||||||
|
|
||||||
// Create temp file
|
// Create temp file
|
||||||
const path = await new Promise<string>((res, rej) => {
|
const [path, cleanup] = await new Promise<[string, any]>((res, rej) => {
|
||||||
tmp.file((e, path) => {
|
tmp.file((e, path, fd, cleanup) => {
|
||||||
if (e) return rej(e);
|
if (e) return rej(e);
|
||||||
res(path);
|
res([path, cleanup]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -44,9 +44,7 @@ export default async (url, user, folderId = null, uri = null): Promise<IDriveFil
|
||||||
log(`created: ${driveFile._id}`);
|
log(`created: ${driveFile._id}`);
|
||||||
|
|
||||||
// clean-up
|
// clean-up
|
||||||
fs.unlink(path, e => {
|
cleanup();
|
||||||
if (e) console.error(e);
|
|
||||||
});
|
|
||||||
|
|
||||||
return driveFile;
|
return driveFile;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue