下载方式变更
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
af882fede8
commit
b913f8e073
|
|
@ -66,7 +66,6 @@ async function ossGenerateSTS() {
|
||||||
objectItem[objectItem.length - 1] = new Date().getTime() + '_' + objectItem[objectItem.length - 1]
|
objectItem[objectItem.length - 1] = new Date().getTime() + '_' + objectItem[objectItem.length - 1]
|
||||||
objectName = objectItem.join('/')
|
objectName = objectItem.join('/')
|
||||||
}
|
}
|
||||||
objectName = objectName.slice(1)
|
|
||||||
const reader = new FileReader();
|
const reader = new FileReader();
|
||||||
reader.onload = (ex) => {
|
reader.onload = (ex) => {
|
||||||
const bufferStream = new stream.PassThrough()
|
const bufferStream = new stream.PassThrough()
|
||||||
|
|
@ -79,7 +78,7 @@ async function ossGenerateSTS() {
|
||||||
} else {
|
} else {
|
||||||
resolve({
|
resolve({
|
||||||
name: objectName,
|
name: objectName,
|
||||||
url: Vue.prototype.OSSclientConfig.viewEndpoint + '/' + decodeUtf8(objectName)
|
url: Vue.prototype.OSSclientConfig.viewEndpoint + decodeUtf8(objectName)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,79 @@
|
||||||
import streamSaver from "streamsaver";
|
import streamSaver from "streamsaver";
|
||||||
import "streamsaver/examples/zip-stream.js";
|
import "streamsaver/examples/zip-stream.js";
|
||||||
|
import JSZip from 'jszip';
|
||||||
|
import FileSaver from 'file-saver';
|
||||||
|
import axios from 'axios'
|
||||||
// import store from '@/store'
|
// import store from '@/store'
|
||||||
streamSaver.mitm = `${window.location.origin}/mitm.html?version=2.0.0`
|
streamSaver.mitm = `${window.location.origin}/mitm.html?version=2.0.0`
|
||||||
|
function getFile(url) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
axios.get(url, {
|
||||||
|
responseType: 'blob',
|
||||||
|
}).then(res => {
|
||||||
|
resolve(res.data)
|
||||||
|
}).catch(err => {
|
||||||
|
reject(err)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
function addFilesRecursively(zip, structure) {
|
||||||
|
Object.keys(structure).forEach((key) => {
|
||||||
|
if (typeof structure[key] === "object" && !structure[key].size) {
|
||||||
|
// 创建子文件夹
|
||||||
|
const subFolder = zip.folder(key);
|
||||||
|
addFilesRecursively(subFolder, structure[key]);
|
||||||
|
} else {
|
||||||
|
// 添加文件
|
||||||
|
zip.file(key, structure[key], { binary: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
function createFloders(obj, arr, r) {
|
||||||
|
let i = arr.shift()
|
||||||
|
if (arr.length <= 0) {
|
||||||
|
obj[i] = r
|
||||||
|
} else {
|
||||||
|
if (!obj[i]) {
|
||||||
|
obj[i] = {}
|
||||||
|
}
|
||||||
|
createFloders(obj[i], arr, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function zipFilesJsZip(zipName, files, obj = {}) {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
try {
|
||||||
|
console.log("同步下载打包开始时间:" + new Date());
|
||||||
|
files = formatFiles(files)
|
||||||
|
const zip = new JSZip();
|
||||||
|
let all = []
|
||||||
|
files.forEach(file => {
|
||||||
|
let res = getFile(file.url).then(r => {
|
||||||
|
const arr = file.name.split('/')
|
||||||
|
createFloders(obj, arr, r)
|
||||||
|
// zip.file(fileName, r, { binary: true });
|
||||||
|
})
|
||||||
|
all.push(res)
|
||||||
|
})
|
||||||
|
Promise.all(all).then(() => {
|
||||||
|
addFilesRecursively(zip, obj)
|
||||||
|
zip.generateAsync({
|
||||||
|
type: "blob",
|
||||||
|
compression: "DEFLATE", // STORE:默认不压缩 DEFLATE:需要压缩
|
||||||
|
compressionOptions: {
|
||||||
|
level: 9 // 压缩等级1~9 1压缩速度最快,9最优压缩方式
|
||||||
|
}
|
||||||
|
}).then((res) => {
|
||||||
|
FileSaver.saveAs(res, zipName, () => {
|
||||||
|
resolve(true)
|
||||||
|
console.log("同步下载打包结束时间:" + new Date());
|
||||||
|
}) // 利用file-saver保存文件
|
||||||
|
})
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
// 下载文件并压缩
|
// 下载文件并压缩
|
||||||
function zipFiles(zipName, files) {
|
function zipFiles(zipName, files) {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
|
|
@ -100,7 +172,7 @@ function decodeUtf8(bytes) {
|
||||||
return str2.join("/") + '/' + name;
|
return str2.join("/") + '/' + name;
|
||||||
}
|
}
|
||||||
export async function downLoadFile(file, name, type = 'file') {
|
export async function downLoadFile(file, name, type = 'file') {
|
||||||
if (type === 'zip') return await zipFiles(name, file);
|
if (type === 'zip') return await zipFilesJsZip(name, file, {});
|
||||||
return await updateFile(file, name)
|
return await updateFile(file, name)
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue