web/src/component/FileSelector.vue

100 lines
3.3 KiB
Vue
Raw Normal View History

2021-05-24 14:19:37 +00:00
<template>
<el-upload
:auto-upload="false"
:on-change="addFile"
:show-file-list="false"
action=""
drag
multiple>
<i class="el-icon-upload"/>
<div class="el-upload__text">将文件拖到此处<em>点击选择</em></div>
2021-05-24 19:06:28 +00:00
<div slot="tip" class="el-upload__tip">
<div>
仅在浏览器内对文件进行解锁无需消耗流量
<el-tooltip effect="dark" placement="top-start">
<div slot="content">
算法在源代码中已经提供所有运算都发生在本地
</div>
<i class="el-icon-info" style="font-size: 12px"/>
</el-tooltip>
</div>
<div>
工作模式: {{ parallel ? "多线程 Worker" : "单线程 Queue" }}
<el-tooltip effect="dark" placement="top-start">
<div slot="content">
将此工具部署在HTTPS环境下可以启用Web Worker特性<br/>
从而更快的利用并行处理完成解锁
</div>
<i class="el-icon-info" style="font-size: 12px"/>
</el-tooltip>
</div>
</div>
2021-05-24 14:19:37 +00:00
<transition name="el-fade-in"><!--todo: add delay to animation-->
<el-progress
v-show="progress_show" :format="progress_string" :percentage="progress_value"
:stroke-width="16" :text-inside="true"
style="margin: 16px 6px 0 6px"
></el-progress>
</transition>
</el-upload>
</template>
<script>
import {spawn, Worker, Pool} from "threads"
import {CommonDecrypt} from "@/decrypt/common.ts";
2021-05-24 15:48:52 +00:00
import {DecryptQueue} from "@/utils/utils";
2021-05-24 14:19:37 +00:00
export default {
name: "FileSelector",
data() {
return {
task_all: 0,
task_finished: 0,
2021-05-24 19:06:28 +00:00
queue: new DecryptQueue(), // for http or file protocol
parallel: false
2021-05-24 14:19:37 +00:00
}
},
computed: {
progress_value() {
return this.task_all ? this.task_finished / this.task_all * 100 : 0
},
progress_show() {
return this.task_all !== this.task_finished
}
},
mounted() {
2021-05-24 15:48:52 +00:00
if (window.Worker && process.env.NODE_ENV === 'production') {
2021-05-24 14:19:37 +00:00
console.log("Using Worker Pool")
this.queue = Pool(
2021-05-24 15:48:52 +00:00
() => spawn(new Worker('@/utils/worker.ts')),
2021-05-24 14:19:37 +00:00
navigator.hardwareConcurrency || 1
)
2021-05-24 19:06:28 +00:00
this.parallel = true
2021-05-24 14:19:37 +00:00
} else {
console.log("Using Queue in Main Thread")
}
},
methods: {
progress_string() {
return `${this.task_finished} / ${this.task_all}`
},
async addFile(file) {
this.task_all++
this.queue.queue(async (dec = CommonDecrypt) => {
console.log("start handling", file.name)
try {
this.$emit("success", await dec(file));
} catch (e) {
console.error(e)
2021-05-24 15:48:52 +00:00
this.$emit("error", e, file.name)
2021-05-24 14:19:37 +00:00
} finally {
this.task_finished++
}
})
},
}
}
</script>