mirror of https://github.com/fantasticit/think.git
server: return url if file alreay exist
This commit is contained in:
parent
1f94d0e465
commit
757eac171a
|
@ -11,7 +11,7 @@ const pipeWriteStream = (filepath, writeStream): Promise<void> => {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
const readStream = fs.createReadStream(filepath);
|
const readStream = fs.createReadStream(filepath);
|
||||||
readStream.on('end', () => {
|
readStream.on('end', () => {
|
||||||
fs.removeSync(filepath);
|
fs.unlinkSync(filepath);
|
||||||
resolve();
|
resolve();
|
||||||
});
|
});
|
||||||
readStream.pipe(writeStream);
|
readStream.pipe(writeStream);
|
||||||
|
@ -73,14 +73,21 @@ export class LocalOssClient extends BaseOssClient {
|
||||||
* @param file
|
* @param file
|
||||||
* @param query
|
* @param query
|
||||||
*/
|
*/
|
||||||
async uploadChunk(file: Express.Multer.File, query: FileQuery): Promise<void> {
|
async uploadChunk(file: Express.Multer.File, query: FileQuery): Promise<void | string> {
|
||||||
const { md5, chunkIndex } = query;
|
const { filename, md5, chunkIndex } = query;
|
||||||
|
|
||||||
if (!('chunkIndex' in query)) {
|
if (!('chunkIndex' in query)) {
|
||||||
throw new Error('请指定 chunkIndex');
|
throw new Error('请指定 chunkIndex');
|
||||||
}
|
}
|
||||||
|
|
||||||
const { absolute } = this.storeFilePath(md5);
|
const { absolute, relative } = this.storeFilePath(md5);
|
||||||
|
const absoluteFilepath = path.join(absolute, filename);
|
||||||
|
|
||||||
|
if (fs.existsSync(absoluteFilepath)) {
|
||||||
|
const relativeFilePath = path.join(relative, filename);
|
||||||
|
return this.serveFilePath(relativeFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
const chunksDir = path.join(absolute, 'chunks');
|
const chunksDir = path.join(absolute, 'chunks');
|
||||||
fs.ensureDirSync(chunksDir);
|
fs.ensureDirSync(chunksDir);
|
||||||
fs.writeFileSync(path.join(chunksDir, '' + chunkIndex), file.buffer);
|
fs.writeFileSync(path.join(chunksDir, '' + chunkIndex), file.buffer);
|
||||||
|
@ -104,19 +111,17 @@ export class LocalOssClient extends BaseOssClient {
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
chunks.map((chunk, index) => {
|
chunks.map((chunk, index) => {
|
||||||
const writeStream = fs.createWriteStream(absoluteFilepath, {
|
return pipeWriteStream(
|
||||||
|
path.join(chunksDir, chunk),
|
||||||
|
fs.createWriteStream(absoluteFilepath, {
|
||||||
start: index * FILE_CHUNK_SIZE,
|
start: index * FILE_CHUNK_SIZE,
|
||||||
});
|
end: (index + 1) * FILE_CHUNK_SIZE,
|
||||||
|
|
||||||
if (index === chunks.length - 1) {
|
|
||||||
writeStream.on('finish', () => {
|
|
||||||
fs.removeSync(chunksDir);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
pipeWriteStream(path.join(chunksDir, chunk), writeStream);
|
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
fs.removeSync(chunksDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.serveFilePath(relativeFilePath);
|
return this.serveFilePath(relativeFilePath);
|
||||||
|
|
|
@ -8,7 +8,7 @@ export type FileQuery = {
|
||||||
|
|
||||||
export abstract class OssClient {
|
export abstract class OssClient {
|
||||||
abstract uploadFile(file: Express.Multer.File, query: FileQuery): Promise<string>;
|
abstract uploadFile(file: Express.Multer.File, query: FileQuery): Promise<string>;
|
||||||
abstract uploadChunk(file: Express.Multer.File, query: FileQuery): Promise<void>;
|
abstract uploadChunk(file: Express.Multer.File, query: FileQuery): Promise<void | string>;
|
||||||
abstract mergeChunk(query: FileQuery): Promise<string>;
|
abstract mergeChunk(query: FileQuery): Promise<string>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ export class BaseOssClient implements OssClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
uploadChunk(file: Express.Multer.File, query: FileQuery): Promise<void> {
|
uploadChunk(file: Express.Multer.File, query: FileQuery): Promise<void | string> {
|
||||||
throw new Error('Method not implemented.');
|
throw new Error('Method not implemented.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue