file api moved to network module
This commit is contained in:
parent
9d73412a2d
commit
9dbfd1ac61
|
|
@ -22,7 +22,7 @@
|
|||
|
||||
<script setup>
|
||||
import { computed, onUnmounted, reactive, onMounted, ref } from "vue";
|
||||
import { PostData } from "components/webguicomp//network";
|
||||
import { GetBlockObject, PutBlockObject, PostData } from "components/webguicomp//network";
|
||||
import { useQuasar, Dialog } from 'quasar';
|
||||
const $q = useQuasar();
|
||||
|
||||
|
|
@ -57,115 +57,18 @@ async function SaveFile(data, fn) {
|
|||
a.remove();
|
||||
}
|
||||
|
||||
function ReceiveChunk(cur, total, chunk) {
|
||||
let foper;
|
||||
if (total == 1) foper = 3;
|
||||
else if (cur == 0) foper = 1;
|
||||
else if (cur == (total - 1)) foper = 2;
|
||||
else foper = 0;
|
||||
let data = {
|
||||
raw_data: {
|
||||
opertype: 1,
|
||||
operphase: foper,
|
||||
mem_object: selected.value[0].name,
|
||||
size: BLOCK_SIZE,
|
||||
dat: ''
|
||||
}
|
||||
};
|
||||
return new Promise((resolve, reject) => {
|
||||
console.log(`Receive chunk ${cur} from ${total}`)
|
||||
PostData(data, 1, 0, () => resolve(data))
|
||||
})
|
||||
}
|
||||
|
||||
function base64ToArrayBuffer(base64) {
|
||||
var binaryString = atob(base64);
|
||||
var bytes = new Uint8Array(binaryString.length);
|
||||
for (var i = 0; i < binaryString.length; i++) {
|
||||
bytes[i] = binaryString.charCodeAt(i);
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
async function ReveiveFileChunks(buf) {
|
||||
let chunksnum = Math.floor(selected.value[0].size / BLOCK_SIZE);
|
||||
if (selected.value[0].size % BLOCK_SIZE)
|
||||
chunksnum++;
|
||||
console.log(`Found ${chunksnum} blocks in file ${selected.value[0].name}`)
|
||||
let i;
|
||||
let chunk, resp;
|
||||
const dialog = Dialog.create({ message: 'Downloaded 0%', progress: true, persistent: true, ok: false, style: 'border: none; box-shadow: none;' })
|
||||
for (i = 0; i < chunksnum; i++) {
|
||||
resp = await ReceiveChunk(i, chunksnum, chunk);
|
||||
let decoded = base64ToArrayBuffer(resp.raw_data.dat);
|
||||
for (let k = 0; k < decoded.byteLength; k++)
|
||||
buf[i * BLOCK_SIZE + k] = decoded[k];
|
||||
dialog.update({ message: `Downloaded ${Math.floor(i * 100 / chunksnum)}%` })
|
||||
}
|
||||
dialog.hide();
|
||||
}
|
||||
|
||||
async function DownloadFile() {
|
||||
if (selected.value[0]) {
|
||||
const buf = new Uint8Array(selected.value[0].size);
|
||||
await ReveiveFileChunks(buf);
|
||||
//await ReveiveFileChunks(buf);
|
||||
await GetBlockObject(selected.value[0].name, selected.value[0].size, buf);
|
||||
SaveFile(buf, selected.value[0].name);
|
||||
}
|
||||
}
|
||||
|
||||
function ToBase64(bytes) {
|
||||
var binary = '';
|
||||
var len = bytes.byteLength;
|
||||
for (var i = 0; i < len; i++) {
|
||||
binary += String.fromCharCode(bytes[i]);
|
||||
}
|
||||
return window.btoa(binary);
|
||||
}
|
||||
|
||||
function SendChunk(cur, total, reader) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let arr;
|
||||
if (cur == (total - 1))
|
||||
arr = new Uint8Array(reader.result, cur * BLOCK_SIZE);
|
||||
else
|
||||
arr = new Uint8Array(reader.result, cur * BLOCK_SIZE, BLOCK_SIZE);
|
||||
let encode = ToBase64(arr);
|
||||
let length = encode.length;
|
||||
|
||||
let foper;
|
||||
if (total == 1) foper = 3;
|
||||
else if (cur == 0) foper = 1;
|
||||
else if (cur == (total - 1)) foper = 2;
|
||||
else foper = 0;
|
||||
|
||||
let data = {
|
||||
raw_data: {
|
||||
opertype: 3,
|
||||
operphase: foper,
|
||||
mem_object: file.value.name,
|
||||
size: length,
|
||||
dat: encode
|
||||
}
|
||||
};
|
||||
|
||||
console.log(`Send chunk ${cur} from ${total} length ${arr.byteLength}`)
|
||||
PostData(data, 1, 0, () => resolve())
|
||||
})
|
||||
}
|
||||
|
||||
async function SendFileChunks(reader) {
|
||||
let chunksnum = Math.floor(reader.result.byteLength / BLOCK_SIZE);
|
||||
if (reader.result.byteLength % BLOCK_SIZE)
|
||||
chunksnum++;
|
||||
console.log(`Found ${chunksnum} blocks in file`)
|
||||
let i;
|
||||
const dialog = Dialog.create({ message: 'Uploaded 0%', progress: true, persistent: true, ok: false, style: 'border: none; box-shadow: none;' })
|
||||
for (i = 0; i < chunksnum; i++) {
|
||||
await SendChunk(i, chunksnum, reader);
|
||||
dialog.update({ message: `Uploaded ${Math.floor(i * 100 / chunksnum)}%` })
|
||||
}
|
||||
dialog.update({ message: 'Done.Refreshing file list...' });
|
||||
PostData(data, 2, 0, () => { dialog.hide() });
|
||||
async function SendFile(buf) {
|
||||
await PutBlockObject(file.value.name, file.value.size, buf);
|
||||
PostData(data, 2, 0, null);
|
||||
}
|
||||
|
||||
function UploadFile() {
|
||||
|
|
@ -174,7 +77,7 @@ function UploadFile() {
|
|||
console.log(`File "${file.value.name}" with size ${file.value.size} byte`);
|
||||
if (reader.result.byteLength == 0)
|
||||
return;
|
||||
SendFileChunks(reader);
|
||||
SendFile(reader.result);
|
||||
}
|
||||
reader.readAsArrayBuffer(file.value);
|
||||
}
|
||||
|
|
|
|||
107
network.js
107
network.js
|
|
@ -1,11 +1,114 @@
|
|||
import { api } from "boot/axios";
|
||||
import { sha256 } from "js-sha256";
|
||||
import { Notify } from "quasar";
|
||||
import { Notify, Dialog } from "quasar";
|
||||
|
||||
const API_URL = "/api";
|
||||
const SHA256_HMAC_KEY = "mykey";
|
||||
let MessIdCounter = 1;
|
||||
|
||||
const BLOCK_SIZE = 5120;
|
||||
function ReceiveChunk(cur, total, name) {
|
||||
let foper;
|
||||
if (total == 1) foper = 3;
|
||||
else if (cur == 0) foper = 1;
|
||||
else if (cur == (total - 1)) foper = 2;
|
||||
else foper = 0;
|
||||
let data = {
|
||||
raw_data: {
|
||||
opertype: 1,
|
||||
operphase: foper,
|
||||
mem_object: name,
|
||||
size: BLOCK_SIZE,
|
||||
dat: ''
|
||||
}
|
||||
};
|
||||
return new Promise((resolve, reject) => {
|
||||
console.log(`Receive chunk ${cur} from ${total}`)
|
||||
PostData(data, 1, 0, () => resolve(data))
|
||||
})
|
||||
}
|
||||
|
||||
function base64ToArrayBuffer(base64) {
|
||||
var binaryString = atob(base64);
|
||||
var bytes = new Uint8Array(binaryString.length);
|
||||
for (var i = 0; i < binaryString.length; i++) {
|
||||
bytes[i] = binaryString.charCodeAt(i);
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
async function GetBlockObject(name, size, buf) {
|
||||
let partsnum = Math.floor(size / BLOCK_SIZE);
|
||||
if (size % BLOCK_SIZE)
|
||||
partsnum++;
|
||||
console.log(`Found ${partsnum} blocks in file ${name}`)
|
||||
let i;
|
||||
let resp;
|
||||
const dialog = Dialog.create({ message: 'Downloaded 0%', progress: true, persistent: true, ok: false, style: 'border: none; box-shadow: none;' })
|
||||
for (i = 0; i < partsnum; i++) {
|
||||
resp = await ReceiveChunk(i, partsnum, name);
|
||||
let decoded = base64ToArrayBuffer(resp.raw_data.dat);
|
||||
for (let k = 0; k < decoded.byteLength; k++)
|
||||
buf[i * BLOCK_SIZE + k] = decoded[k];
|
||||
dialog.update({ message: `Downloaded ${Math.floor(i * 100 / partsnum)}%` })
|
||||
}
|
||||
dialog.hide();
|
||||
}
|
||||
|
||||
function ToBase64(bytes) {
|
||||
var binary = '';
|
||||
var len = bytes.byteLength;
|
||||
for (var i = 0; i < len; i++) {
|
||||
binary += String.fromCharCode(bytes[i]);
|
||||
}
|
||||
return window.btoa(binary);
|
||||
}
|
||||
|
||||
function SendChunk(cur, total, name, buf) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let arr;
|
||||
if (cur == (total - 1))
|
||||
arr = new Uint8Array(buf, cur * BLOCK_SIZE);
|
||||
else
|
||||
arr = new Uint8Array(buf, cur * BLOCK_SIZE, BLOCK_SIZE);
|
||||
let encode = ToBase64(arr);
|
||||
let length = encode.length;
|
||||
|
||||
let foper;
|
||||
if (total == 1) foper = 3;
|
||||
else if (cur == 0) foper = 1;
|
||||
else if (cur == (total - 1)) foper = 2;
|
||||
else foper = 0;
|
||||
|
||||
let data = {
|
||||
raw_data: {
|
||||
opertype: 3,
|
||||
operphase: foper,
|
||||
mem_object: name,
|
||||
size: length,
|
||||
dat: encode
|
||||
}
|
||||
};
|
||||
|
||||
console.log(`Send chunk ${cur} from ${total} length ${arr.byteLength}`)
|
||||
PostData(data, 1, 0, () => resolve())
|
||||
})
|
||||
}
|
||||
|
||||
async function PutBlockObject(name, size, buf) {
|
||||
let partsnum = Math.floor(size / BLOCK_SIZE);
|
||||
if (size % BLOCK_SIZE)
|
||||
partsnum++;
|
||||
console.log(`Found ${partsnum} blocks in file`)
|
||||
let i;
|
||||
const dialog = Dialog.create({ message: 'Uploaded 0%', progress: true, persistent: true, ok: false, style: 'border: none; box-shadow: none;' })
|
||||
for (i = 0; i < partsnum; i++) {
|
||||
await SendChunk(i, partsnum, name, buf);
|
||||
dialog.update({ message: `Uploaded ${Math.floor(i * 100 / partsnum)}%` })
|
||||
}
|
||||
dialog.hide();
|
||||
}
|
||||
|
||||
function PostDataControlled(varlist, messtype, applytype, onfinished, enable) {
|
||||
if (!enable) return;
|
||||
var pld = {};
|
||||
|
|
@ -44,7 +147,7 @@ function PostDataControlled(varlist, messtype, applytype, onfinished, enable) {
|
|||
function PostData(varlist, messtype, applytype, onfinished) {
|
||||
PostDataControlled(varlist, messtype, applytype, onfinished, true);
|
||||
}
|
||||
export { PostData, PostDataControlled };
|
||||
export { PostData, PostDataControlled, GetBlockObject, PutBlockObject };
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user