craft-nft

A standalone NFT implementation for real-world arts and crafts assets
Log | Files | Refs | README

commit 9a8ff3de39eb0aab56241505be577b227e31b5b4
parent 44363b424cf7112b78565110f590dc20a4b46bb2
Author: lash <dev@holbrook.no>
Date:   Thu, 22 Dec 2022 19:52:17 +0000

Upload attachments to content store

Diffstat:
Mjs/index.html | 11++++++++---
Mjs/manual_test_browser.js | 30+++++++++++++++++++++++-------
Mjs/src/wala.js | 7++++++-
3 files changed, 37 insertions(+), 11 deletions(-)

diff --git a/js/index.html b/js/index.html @@ -9,28 +9,33 @@ <script src="src/common.js"></script> <script src="src/wala.js"></script> <style type='text/css'> + #detail { display: none; - /*visibility: hidden;*/ } + #mint { display: none; - /*visibility: hidden;*/ } + a { text-decoration: underline; color: #0000ff; } + a:hover { cursor: pointer; } + label:after { content: '\a'; white-space: pre; } + dt { font-weight: 900; } + </style> </head> <body> @@ -60,7 +65,7 @@ dt { <label for="panel_amount">Batch size (0 or empty for unique work)</label> <input type="file" id="panel_thumb" name="thumb" onChange='fileChange();'; /> <label for="panel_thumb">Add image</label> - <ol id="panel_images_list"> + <ol id="panel_attachment_list"> </ol> <button id="panel_submit" onClick='return false;'>allocate</button> </form> diff --git a/js/manual_test_browser.js b/js/manual_test_browser.js @@ -64,6 +64,7 @@ async function generateAllocation() { name: undefined, description: undefined, amount: 0, + attachments: [], }; let amount = document.getElementById('panel_amount').value; @@ -82,6 +83,11 @@ async function generateAllocation() { sha_raw.update(s); const digest = sha_raw.getHash("HEX"); + const attachments = document.getElementById('panel_attachment_list').children; + for (let i = 0; i < attachments.length; i++) { + tokenData.attachments.push(attachments[i].innerHTML); + } + if (session.contentGateway !== undefined) { try { let r = await session.contentGateway.put(s); @@ -324,30 +330,40 @@ async function run(w3, generated_session) { } -async function renderFile(contents) { +async function renderFile(contents, filename) { //const sha_raw = new jsSHA("SHA-256", "BINARY", { encoding: "UTF8" }); - console.log('hexking', contents); const sha_raw = new jsSHA("SHA-256", "ARRAYBUFFER"); sha_raw.update(contents); const digest = sha_raw.getHash("HEX"); - console.debug('digest', digest); let li = document.createElement('li'); li.innerHTML = 'sha256:' + digest; - document.getElementById('panel_images_list').appendChild(li); + document.getElementById('panel_attachment_list').appendChild(li); + return digest; } -async function uploadFile(v) { +async function uploadFile(contents, filename) { + if (session.contentGateway !== undefined) { + try { + let r = await session.contentGateway.put(contents, filename); + //if (r != digest) { + // throw 'digest mismatch (' + r + ' != ' + digest + ')'; + //} + } catch(e) { + console.error('failed to upload token attachment data:', e); + } + } } + async function fileChange(e) { let fileButton = document.getElementById("panel_thumb") let file = fileButton.files[0]; if (file) { let f = new FileReader(); f.onloadend = async (r) => { - renderFile(r.target.result); - uploadFile(r.target.result); + renderFile(r.target.result, file.name); + uploadFile(r.target.result, file.name); }; f.readAsArrayBuffer(file); } diff --git a/js/src/wala.js b/js/src/wala.js @@ -2,9 +2,14 @@ function Wala(url) { this._url = url; } -Wala.prototype.put = async function(v) { +Wala.prototype.put = async function(v, filename, mimetype) { + let headers = {}; + if (filename !== undefined) { + headers['X-Filename'] = filename; + } let r = await fetch(this._url, { method: 'put', + headers: headers, body: v, }); if (!r.ok) {