create a worker for upload and downloading with the api (not working since it's operating in a different context)
parent
1532213602
commit
a6c6aff864
@ -0,0 +1,200 @@ |
||||
import * as api from "ng-sdk-js"; |
||||
import { default as ng } from "../api"; |
||||
|
||||
//console.log("loaded worker");
|
||||
|
||||
// Accepts messages with data:
|
||||
// - command: `get_blob` | `upload`
|
||||
// - request_id: A request id defined by the caller that is used on `postMessage` together with the `result` of the command or `progress`
|
||||
// - params: an object of parameters to pass to the function.
|
||||
//
|
||||
// May send progress messages with {request_id, progress}, {request_id, result}, or {request_id, error}.
|
||||
|
||||
onmessage = async (e) => { |
||||
const { data } = e; |
||||
let result; |
||||
switch (data.command) { |
||||
case "download": |
||||
get_blob(data.request_id, data.params); |
||||
break; |
||||
case "upload": |
||||
prepare_upload(data.request_id, data.params) |
||||
break; |
||||
} |
||||
}; |
||||
|
||||
postMessage({ loaded: true }); |
||||
|
||||
async function get_blob(request_id, { session_id, ref }) { |
||||
if (!ref) return false; |
||||
|
||||
try { |
||||
let nuri = { |
||||
target: "PrivateStore", |
||||
entire_store: false, |
||||
access: [{ Key: ref.reference.key }], |
||||
locator: [], |
||||
object: ref.reference.id, |
||||
}; |
||||
|
||||
let file_request = { |
||||
V0: { |
||||
command: "FileGet", |
||||
nuri, |
||||
session_id: session_id, |
||||
}, |
||||
}; |
||||
|
||||
let final_blob; |
||||
let content_type; |
||||
console.debug("downloading with ng.app_request_stream", ref); |
||||
|
||||
let unsub = await ng.app_request_stream(file_request, async (blob) => { |
||||
console.log("GOT APP RESPONSE", blob); |
||||
if (blob.V0.FileMeta) { |
||||
content_type = blob.V0.FileMeta.content_type; |
||||
final_blob = new Blob([], { type: content_type }); |
||||
} else if (blob.V0.FileBinary) { |
||||
if (blob.V0.FileBinary.byteLength > 0) { |
||||
final_blob = new Blob([final_blob, blob.V0.FileBinary], { |
||||
type: content_type, |
||||
}); |
||||
} |
||||
} else if (blob.V0 == "EndOfStream") { |
||||
var blobUrl = URL.createObjectURL(final_blob); |
||||
postMessage({ request_id, result: blobUrl }); |
||||
|
||||
return blobUrl; |
||||
} |
||||
}); |
||||
} catch (e) { |
||||
console.error(e); |
||||
postMessage({ request_id, error: e }); |
||||
} |
||||
} |
||||
|
||||
// Upload
|
||||
|
||||
function upload_file(request_id, { session_id, upload_id, nuri, file, success: on_success }) { |
||||
let chunkSize = 1_048_564; |
||||
let fileSize = file.size; |
||||
let offset = 0; |
||||
let readBlock = null; |
||||
postMessage({ request_id, progress: { total: fileSize, current: offset } }); |
||||
|
||||
console.log("in upload_file"); |
||||
|
||||
let onLoadHandler = async function(event) { |
||||
let result = event.target.result; |
||||
|
||||
console.log("onLoadHandler", result); |
||||
|
||||
if (event.target.error == null) { |
||||
offset += result.byteLength; |
||||
postMessage({ request_id, progress: { total: fileSize, current: offset } }); |
||||
|
||||
// console.log("chunk", result);
|
||||
|
||||
let res = await ng.upload_chunk( |
||||
session_id, |
||||
upload_id, |
||||
result, |
||||
nuri |
||||
); |
||||
//console.log("chunk upload res", res);
|
||||
// if (onChunkRead) {
|
||||
// onChunkRead(result);
|
||||
// }
|
||||
} else { |
||||
// if (onChunkError) {
|
||||
// onChunkError(event.target.error);
|
||||
// }
|
||||
return; |
||||
} |
||||
|
||||
// If finished:
|
||||
if (offset >= fileSize) { |
||||
//console.log("file uploaded");
|
||||
let res = await ng.upload_chunk( |
||||
session_id, |
||||
upload_id, |
||||
[], |
||||
nuri |
||||
); |
||||
|
||||
postMessage({ request_id, progress: { total: fileSize, current: fileSize } }); |
||||
on_success(res); |
||||
|
||||
return; |
||||
} |
||||
|
||||
readBlock(offset, chunkSize, file); |
||||
}; |
||||
|
||||
readBlock = function(offset, length, file) { |
||||
let fileReader = new FileReader(); |
||||
let blob = file.slice(offset, length + offset); |
||||
fileReader.onload = onLoadHandler; |
||||
fileReader.readAsArrayBuffer(blob); |
||||
}; |
||||
|
||||
readBlock(offset, chunkSize, file); |
||||
return; |
||||
} |
||||
|
||||
const prepare_upload = async (request_id, { session_id, file }) => { |
||||
if (!file) return; |
||||
//console.log(file);
|
||||
|
||||
let nuri = { |
||||
target: "PrivateStore", |
||||
entire_store: false, |
||||
access: [], |
||||
locator: [], |
||||
}; |
||||
|
||||
let start_request = { |
||||
V0: { |
||||
command: "FilePut", |
||||
nuri, |
||||
payload: { |
||||
V0: { |
||||
RandomAccessFilePut: file.type, |
||||
}, |
||||
}, |
||||
session_id: session_id, |
||||
}, |
||||
}; |
||||
|
||||
console.log("prepare_upload", file); |
||||
|
||||
let start_res = await ng.app_request(start_request); |
||||
let upload_id = start_res.V0.FileUploading; |
||||
|
||||
console.log("prepare_upload, start_res", start_res); |
||||
|
||||
upload_file(request_id, { |
||||
session_id, upload_id, nuri, file, success: async (reference) => { |
||||
if (reference) { |
||||
let request = { |
||||
V0: { |
||||
command: "FilePut", |
||||
nuri, |
||||
payload: { |
||||
V0: { |
||||
AddFile: { |
||||
filename: file.name, |
||||
object: reference.V0.FileUploaded, |
||||
}, |
||||
}, |
||||
}, |
||||
session_id: session_id, |
||||
}, |
||||
}; |
||||
|
||||
await ng.app_request(request); |
||||
postMessage({ request_id, result: "Upload finished" }); |
||||
} |
||||
} |
||||
}); |
||||
}; |
@ -1,5 +1,5 @@ |
||||
import * as api from "ng-sdk-js"; |
||||
import { default as ng } from "./api"; |
||||
import { default as ng } from "../api"; |
||||
|
||||
//console.log("loaded worker");
|
||||
|
Loading…
Reference in new issue