修复模型下载问题
This commit is contained in:
@ -68,7 +68,7 @@ export abstract class Model {
|
|||||||
}
|
}
|
||||||
//不存在则下载
|
//不存在则下载
|
||||||
let cache = cacheJsonData.find(c => c.url === url);
|
let cache = cacheJsonData.find(c => c.url === url);
|
||||||
if (!cache) {
|
if (!cache || !fs.existsSync(path.join(cacheDir, cache.filename))) {
|
||||||
let saveType = option?.saveType ?? null;
|
let saveType = option?.saveType ?? null;
|
||||||
const saveTypeDict: Record<string, ModelType> = {
|
const saveTypeDict: Record<string, ModelType> = {
|
||||||
".onnx": "onnx",
|
".onnx": "onnx",
|
||||||
@ -83,10 +83,10 @@ export abstract class Model {
|
|||||||
return res.blob();
|
return res.blob();
|
||||||
}).then(blob => blob.stream()).then(async stream => {
|
}).then(blob => blob.stream()).then(async stream => {
|
||||||
const cacheFilename = path.join(cacheDir, Date.now().toString());
|
const cacheFilename = path.join(cacheDir, Date.now().toString());
|
||||||
|
const hash = await new Promise<string>((resolve, reject) => {
|
||||||
let fsStream!: ReturnType<typeof fs.createWriteStream>;
|
let fsStream!: ReturnType<typeof fs.createWriteStream>;
|
||||||
let hashStream!: ReturnType<typeof crypto.createHash>;
|
let hashStream!: ReturnType<typeof crypto.createHash>;
|
||||||
let hash!: string;
|
stream.pipeTo(new WritableStream({
|
||||||
await stream.pipeTo(new WritableStream({
|
|
||||||
start(controller) {
|
start(controller) {
|
||||||
fsStream = fs.createWriteStream(cacheFilename);
|
fsStream = fs.createWriteStream(cacheFilename);
|
||||||
hashStream = crypto.createHash("md5");
|
hashStream = crypto.createHash("md5");
|
||||||
@ -98,18 +98,25 @@ export abstract class Model {
|
|||||||
close() {
|
close() {
|
||||||
fsStream.end();
|
fsStream.end();
|
||||||
hashStream.end();
|
hashStream.end();
|
||||||
hash = hashStream.digest("hex")
|
resolve(hashStream.digest("hex"));
|
||||||
},
|
},
|
||||||
abort() { }
|
abort() { }
|
||||||
}));
|
})).catch(reject);
|
||||||
|
})
|
||||||
return { filename: cacheFilename, hash };
|
return { filename: cacheFilename, hash };
|
||||||
});
|
});
|
||||||
//重命名
|
//重命名
|
||||||
const filename = `${res.hash}.${saveType}`;
|
const filename = `${res.hash}.${saveType}`;
|
||||||
fs.promises.rename(res.filename, path.join(cacheDir, filename));
|
fs.promises.rename(res.filename, path.join(cacheDir, filename));
|
||||||
//保存缓存
|
//保存缓存
|
||||||
|
if (!cache) {
|
||||||
cache = { url, filename };
|
cache = { url, filename };
|
||||||
cacheJsonData.push(cache);
|
cacheJsonData.push(cache);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
cache.filename = filename;
|
||||||
|
cache.url = url;
|
||||||
|
}
|
||||||
fs.promises.writeFile(cacheJsonFile, JSON.stringify(cacheJsonData, null, 4));
|
fs.promises.writeFile(cacheJsonFile, JSON.stringify(cacheJsonData, null, 4));
|
||||||
}
|
}
|
||||||
//返回模型数据
|
//返回模型数据
|
||||||
|
Reference in New Issue
Block a user