From aea4c876b86d01c3ffee29774b92edb47f891d96 Mon Sep 17 00:00:00 2001 From: Phillip Date: Fri, 20 Jan 2023 15:07:17 +0200 Subject: [PATCH] able to save zip file to qdn --- qortal-ui-plugins/package.json | 7 +- .../plugins/core/components/ChatPage.js | 94 ++++++++++++++++++- .../plugins/utils/publish-image.js | 11 ++- 3 files changed, 106 insertions(+), 6 deletions(-) diff --git a/qortal-ui-plugins/package.json b/qortal-ui-plugins/package.json index 7e6583d1..5100b14e 100644 --- a/qortal-ui-plugins/package.json +++ b/qortal-ui-plugins/package.json @@ -21,15 +21,17 @@ "@material/mwc-list": "0.27.0", "@material/mwc-select": "0.27.0", "@tiptap/core": "2.0.0-beta.209", + "@tiptap/extension-highlight": "2.0.0-beta.209", "@tiptap/extension-image": "2.0.0-beta.209", "@tiptap/extension-placeholder": "2.0.0-beta.209", "@tiptap/extension-underline": "2.0.0-beta.209", - "@tiptap/extension-highlight": "2.0.0-beta.209", "@tiptap/html": "2.0.0-beta.209", "@tiptap/starter-kit": "2.0.0-beta.209", + "@zip.js/zip.js": "2.6.62", "asmcrypto.js": "2.3.2", "compressorjs": "1.1.1", "emoji-picker-js": "https://github.com/Qortal/emoji-picker-js", + "localforage": "1.10.0", "prosemirror-commands": "1.5.0", "prosemirror-dropcursor": "1.6.1", "prosemirror-gapcursor": "1.3.1", @@ -40,7 +42,6 @@ "prosemirror-state": "1.4.2", "prosemirror-transform": "1.7.0", "prosemirror-view": "1.29.1", - "localforage": "1.10.0", "short-unique-id": "4.4.4" }, "devDependencies": { @@ -88,4 +89,4 @@ "engines": { "node": ">=16.17.1" } -} \ No newline at end of file +} diff --git a/qortal-ui-plugins/plugins/core/components/ChatPage.js b/qortal-ui-plugins/plugins/core/components/ChatPage.js index 1274da7e..0ade923f 100644 --- a/qortal-ui-plugins/plugins/core/components/ChatPage.js +++ b/qortal-ui-plugins/plugins/core/components/ChatPage.js @@ -9,6 +9,8 @@ import Placeholder from '@tiptap/extension-placeholder' import Highlight from '@tiptap/extension-highlight' import {unsafeHTML} from 'lit/directives/unsafe-html.js'; import { Editor, Extension } from '@tiptap/core' +import * as zip from "@zip.js/zip.js"; +import { saveAs } from 'file-saver'; // import localForage from "localforage"; registerTranslateConfig({ @@ -103,7 +105,8 @@ class ChatPage extends LitElement { openUserInfo: { type: Boolean }, selectedHead: { type: Object }, userName: { type: String }, - goToRepliedMessage: {attribute: false} + goToRepliedMessage: {attribute: false}, + gifsToBeAdded: { type: Array} } } @@ -891,6 +894,7 @@ class ChatPage extends LitElement { this.currentEditor = '_chatEditorDOM' this.initialChat = this.initialChat.bind(this) this.isEnabledChatEnter = true + this.gifsToBeAdded = [] } _toggle(value) { @@ -923,6 +927,66 @@ class ChatPage extends LitElement { localStorage.setItem('isEnabledChatEnter', !this.isEnabledChatEnter ) this.isEnabledChatEnter = !this.isEnabledChatEnter } + + addGifs(gifs){ + console.log('gifs', gifs) + this.gifsToBeAdded = [...this.gifsToBeAdded, ...gifs] + console.log('this.gifsToBeAdded', this.gifsToBeAdded) + } + + async uploadGifCollection(){ + try { + function blobToBase64(blob) { + return new Promise((resolve, _) => { + const reader = new FileReader(); + reader.onloadend = () => resolve(reader.result); + reader.readAsDataURL(blob); + }); + } + const zipFileWriter = new zip.BlobWriter("application/zip"); +// Creates a TextReader object storing the text of the entry to add in the zip +// (i.e. "Hello world!"). +const helloWorldReader = new zip.TextReader("Hello world!"); + +// Creates a ZipWriter object writing data via `zipFileWriter`, adds the entry +// "hello.txt" containing the text "Hello world!" via `helloWorldReader`, and +// closes the writer. +const file = this.gifsToBeAdded[0] +const file2 = this.gifsToBeAdded[1] +const zipWriter = new zip.ZipWriter(zipFileWriter, { bufferedWrite: true }); +await zipWriter.add(file.name, new zip.BlobReader(file)); +await zipWriter.add(file2.name, new zip.BlobReader(file2)); + +await zipWriter.close(); +const zipFileBlob = await zipFileWriter.getData() +const blobTobase = await blobToBase64(zipFileBlob) +console.log({blobTobase}) +const userName = await this.getName(this.selectedAddress.address); + if (!userName) { + parentEpml.request('showSnackBar', get("chatpage.cchange27")); + this.isLoading = false; + return; + } + const id = this.uid(); + const identifier = `gif_${id}`; +await publishData({ + registeredName: userName, + file : blobTobase.split(',')[1], + service: 'GIF_REPOSITORY', + identifier: identifier, + parentEpml, + metaData: undefined, + uploadType: 'zip', + selectedAddress: this.selectedAddress, + worker: this.webWorkerImage, + isBase64: true + }) +saveAs(zipFileBlob, 'zipfile'); +console.log({zipFileBlob}) + } catch (error) { + console.log(error) + } + } render() { return html` @@ -1110,6 +1174,34 @@ class ChatPage extends LitElement { + + { + // this.removeImage(); + }} + style=${true ? "visibility:visible;z-index:50" : "visibility: hidden;z-index:-100"}> +
+
+ + + + +
+
+

${translate("chatpage.cchange41")}


diff --git a/qortal-ui-plugins/plugins/utils/publish-image.js b/qortal-ui-plugins/plugins/utils/publish-image.js index 05594d1c..5835aa33 100644 --- a/qortal-ui-plugins/plugins/utils/publish-image.js +++ b/qortal-ui-plugins/plugins/utils/publish-image.js @@ -16,7 +16,8 @@ export const publishData = async ({ parentEpml, uploadType, selectedAddress, - worker + worker, + isBase64 }) => { const validateName = async (receiverName) => { let nameRes = await parentEpml.request("apiCall", { @@ -115,11 +116,17 @@ export const publishData = async ({ } // Base64 encode the file to work around compatibility issues between javascript and java byte arrays + if(isBase64){ + postBody = file + } + if(!isBase64){ let fileBuffer = new Uint8Array(await file.arrayBuffer()) postBody = Buffer.from(fileBuffer).toString("base64") + } + } - + console.log({postBody}) let uploadDataUrl = `/arbitrary/${service}/${registeredName}${urlSuffix}?apiKey=${getApiKey()}` if (identifier != null && identifier.trim().length > 0) {