220 lines
7.5 KiB
JavaScript
220 lines
7.5 KiB
JavaScript
import JSZip from "jszip";
|
||
import { saveAs } from "file-saver";
|
||
import Vue from 'vue';
|
||
import {
|
||
requestPackageAndAnonymizImage,
|
||
} from "@/api/load.js";
|
||
import streamSaver from "streamsaver";
|
||
import "streamsaver/examples/zip-stream.js"
|
||
let flag = {};
|
||
export const resetFlag = () => {
|
||
flag = {}
|
||
}
|
||
export const downloadImage = async (id, id2, IsDicom = true) => {
|
||
if (flag[`${id2}_${IsDicom}`]) return Vue.prototype.$message.warning(Vue.prototype.$t('trials:upload:tip:uploading'));
|
||
flag[`${id2}_${IsDicom}`] = true
|
||
try {
|
||
let params = {
|
||
TrialId: id,
|
||
SubjectVisitId: id2,
|
||
IsDicom: IsDicom
|
||
}
|
||
let res = await requestPackageAndAnonymizImage(params);
|
||
if (res.IsSuccess) {
|
||
if (!res.Result) {
|
||
flag[`${id2}_${IsDicom}`] = false;
|
||
Vue.prototype.$message.warning(Vue.prototype.$t("trials:upload:message:not"))
|
||
return 1;
|
||
}
|
||
Vue.prototype.$message.success(Vue.prototype.$t("trials:upload:message:startUpload"));
|
||
let href = Vue.prototype.OSSclientConfig.basePath + res.Result;
|
||
download(href, res.OtherInfo.FileName, { id2, IsDicom })
|
||
return 2;
|
||
// let a = document.createElement("a");
|
||
// // let fileName =
|
||
// // res.Result.split("/")[res.Result.split("/").length - 1];
|
||
// a.download = res.OtherInfo.FileName;
|
||
// a.href = href;
|
||
// a.click();
|
||
// URL.revokeObjectURL(href);
|
||
// let timer = setTimeout(() => {
|
||
// a = null;
|
||
// href = null;
|
||
// timer = null;
|
||
// }, 500)
|
||
// return 2;
|
||
} else {
|
||
flag[`${id2}_${IsDicom}`] = false;
|
||
return false;
|
||
}
|
||
} catch (err) {
|
||
flag[`${id2}_${IsDicom}`] = false;
|
||
console.log(err);
|
||
}
|
||
};
|
||
const setfolder = async (item) => {
|
||
const zip = new JSZip(); // 创建实例对象
|
||
let patientIds = item.PatientList.map(i => i.PatientIdStr);
|
||
let zipName = `${item.SubjectCode}_${patientIds.join(',')}`;
|
||
let zipObj = {};
|
||
const promises = [];
|
||
for (let visit of item.VisitList) {
|
||
if (!zipObj[`${visit.VisitName}`]) {
|
||
zipObj[`${visit.VisitName}`] = zip.folder(visit.VisitName);
|
||
}
|
||
for (let study of visit.StudyList) {
|
||
let date = study.StudyTime.split(" ")[0];
|
||
for (let series of study.SeriesList) {
|
||
if (
|
||
!zipObj[
|
||
`${visit.VisitName}${series.Modality}`
|
||
]
|
||
) {
|
||
zipObj[
|
||
`${visit.VisitName}${series.Modality}`
|
||
] = zipObj[`${visit.VisitName}`].folder(
|
||
`${date}_${series.Modality}`
|
||
);
|
||
}
|
||
for (let instance of series.InstancePathList) {
|
||
let obj = {
|
||
subjectCode: item.SubjectCode,
|
||
visitName: visit.VisitName,
|
||
date: study.StudyTime.split(" ")[0],
|
||
modality: series.Modality,
|
||
instancePath: instance.Path,
|
||
dicomName: instance.Path.split("/Dicom/")[1],
|
||
};
|
||
const promise = handleBatchDown(
|
||
obj,
|
||
zipObj[
|
||
`${visit.VisitName}${series.Modality}`
|
||
],
|
||
);
|
||
promises.push(promise);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
// 生成 zip 文件
|
||
Promise.all(promises)
|
||
.then(() => {
|
||
// 生成zip 文件
|
||
zip
|
||
.generateAsync({
|
||
type: "blob",
|
||
compression: "DEFLATE", // STORE: 默认不压缩, DEFLATE:需要压缩
|
||
compressionOptions: {
|
||
level: 9, // 压缩等级 1~9 1 压缩速度最快, 9 最优压缩方式
|
||
},
|
||
})
|
||
.then((res) => {
|
||
saveAs(res, zipName + ".zip"); // 使用FileSaver.saveAs保存文件,文件名可自定义
|
||
flag[`${id2}_${IsDicom}`] = false;
|
||
zipObj = null;
|
||
});
|
||
})
|
||
.catch((reason) => { flag[`${id2}_${IsDicom}`] = false; });
|
||
};
|
||
const handleBatchDown = async (item, zip) => {
|
||
return new Promise((resolve) => {
|
||
console.log("同步下载打包开始时间:" + new Date());
|
||
// 创建压缩文件输出流
|
||
const zipFileOutputStream = streamSaver.createWriteStream(zipName);
|
||
// 创建下载文件流
|
||
const fileIterator = files.values();
|
||
const readableZipStream = new window.ZIP({
|
||
async pull(ctrl) {
|
||
const fileInfo = fileIterator.next();
|
||
if (fileInfo.done) {//迭代终止
|
||
ctrl.close();
|
||
} else {
|
||
const { name, url } = fileInfo.value;
|
||
return fetch(url).then(res => {
|
||
ctrl.enqueue({
|
||
name,
|
||
stream: () => res.body
|
||
})
|
||
})
|
||
}
|
||
}
|
||
});
|
||
if (window.WritableStream && readableZipStream.pipeTo) {
|
||
// 开始下载
|
||
readableZipStream
|
||
.pipeTo(zipFileOutputStream)
|
||
.then(() => { console.log("同步下载打包结束时间:" + new Date()); resolve(true) })
|
||
} else {
|
||
resolve(false);
|
||
}
|
||
})
|
||
|
||
};
|
||
const getFileData = (fileUrl) => {
|
||
return new Promise((resolve, reject) => {
|
||
axios(fileUrl, {
|
||
method: "GET",
|
||
responseType: "blob", // 返回的数据会被强制转为blob类型 ,转换成arraybuffer 也行
|
||
})
|
||
.then((res) => {
|
||
resolve(res);
|
||
})
|
||
.catch((error) => {
|
||
reject(error);
|
||
});
|
||
});
|
||
};
|
||
export const fileDownload = (content, filename) => {
|
||
const eleLink = document.createElement("a");
|
||
eleLink.download = filename;
|
||
eleLink.style.display = "none";
|
||
const blob = new Blob([content]);
|
||
eleLink.href = URL.createObjectURL(blob);
|
||
document.body.appendChild(eleLink);
|
||
eleLink.click();
|
||
document.body.removeChild(eleLink);
|
||
};
|
||
let download = async (downloadUrl, downloadFileName, res) => {
|
||
const blob = await getBlob(downloadUrl);
|
||
flag[`${res.id2}_${res.IsDicom}`] = false;
|
||
saveAsB(blob, downloadFileName);
|
||
}
|
||
|
||
let getBlob = (url) => {
|
||
return new Promise((resolve, reject) => {
|
||
const xhr = new XMLHttpRequest();
|
||
|
||
xhr.open('GET', url, true);
|
||
xhr.responseType = 'blob';
|
||
xhr.onload = () => {
|
||
if (xhr.status === 200) {
|
||
resolve(xhr.response);
|
||
} else {
|
||
reject(new Error(`Request failed with status ${xhr.status}`));
|
||
}
|
||
};
|
||
xhr.onerror = () => {
|
||
reject(new Error('Request failed'));
|
||
};
|
||
|
||
xhr.send();
|
||
});
|
||
}
|
||
|
||
let saveAsB = (blob, filename) => {
|
||
const link = document.createElement('a');
|
||
const body = document.body;
|
||
|
||
link.href = window.URL.createObjectURL(blob);
|
||
link.download = filename;
|
||
|
||
// hide the link
|
||
link.style.display = 'none';
|
||
body.appendChild(link);
|
||
|
||
link.click();
|
||
body.removeChild(link);
|
||
|
||
window.URL.revokeObjectURL(link.href);
|
||
}
|