Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add save to ipfs function #310

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"compounds": [
{
"name": "Main + renderer",
"configurations": [
"Main",
"Renderer"
],
"stopAll": true
}
],
"configurations": [
{
"type": "pwa-chrome",
"name": "Renderer",
"port": 9222,
"request": "attach",
"name": "Attach to Chrome",
"port": 9222, // must run Chrome with a flag: google-chrome --remote-debugging-port=9222
"urlFilter": "http://127.0.0.1:1999/*",
"type": "pwa-chrome",
"webRoot": "${workspaceFolder}"
},
{
"name": "Main",
"type": "pwa-node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron",
"windows": {
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron.cmd"
},
"args": [
".",
"--remote-debugging-port=9222"
],
"outputCapture": "std",
"console": "integratedTerminal"
}
]
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ See [Control Directives](https://rtlcss.com/learn/usage-guide/control-directives
This is basically ready for release, but as of yet unreleased.

- Install dependencies with `npm i`
- Start the electron app with `npm run electron:start`
- Start the electron app with `ELECTRON_ENABLE_LOGGING=true npm run electron:start`

[electron-debug][] is included, so you can use <kbd>F5</kbd>/<kbd>Ctrl+R</kbd> to reload and <kbd>F12</kbd>/<kbd>Ctrl+Shift+I</kbd> to open the devtools.

Expand Down
186 changes: 93 additions & 93 deletions lib/FileSaver.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
factory();
global.FileSaver = mod.exports;
}
})(this, function () {
}(this, function () {
"use strict";

/*
Expand All @@ -29,7 +29,7 @@
function bom(blob, opts) {
if (typeof opts === 'undefined') opts = {
autoBom: false
};else if (typeof opts !== 'object') {
}; else if (typeof opts !== 'object') {
console.warn('Deprecated: Expected third argument to be a object');
opts = {
autoBom: !opts
Expand Down Expand Up @@ -69,7 +69,7 @@

try {
xhr.send();
} catch (e) {}
} catch (e) { }

return xhr.status >= 200 && xhr.status <= 299;
} // `a.click()` doesn't work for all browsers (#465)
Expand All @@ -90,99 +90,99 @@

var isMacOSWebView = /Macintosh/.test(navigator.userAgent) && /AppleWebKit/.test(navigator.userAgent) && !/Safari/.test(navigator.userAgent);
var saveAs = _global.saveAs || ( // probably in some web worker
typeof window !== 'object' || window !== _global ? function saveAs() {}
/* noop */
// Use download attribute first if possible (#193 Lumia mobile) unless this is a macOS WebView
: 'download' in HTMLAnchorElement.prototype && !isMacOSWebView ? function saveAs(blob, name, opts) {
var URL = _global.URL || _global.webkitURL;
var a = document.createElement('a');
name = name || blob.name || 'download';
a.download = name;
a.rel = 'noopener'; // tabnabbing
// TODO: detect chrome extensions & packaged apps
// a.target = '_blank'

if (typeof blob === 'string') {
// Support regular links
a.href = blob;

if (a.origin !== location.origin) {
corsEnabled(a.href) ? download(blob, name, opts) : click(a, a.target = '_blank');
} else {
click(a);
}
} else {
// Support blobs
a.href = URL.createObjectURL(blob);
setTimeout(function () {
URL.revokeObjectURL(a.href);
}, 4E4); // 40s

setTimeout(function () {
click(a);
}, 0);
}
} // Use msSaveOrOpenBlob as a second approach
: 'msSaveOrOpenBlob' in navigator ? function saveAs(blob, name, opts) {
name = name || blob.name || 'download';

if (typeof blob === 'string') {
if (corsEnabled(blob)) {
download(blob, name, opts);
} else {
typeof window !== 'object' || window !== _global ? function saveAs() { }
/* noop */
// Use download attribute first if possible (#193 Lumia mobile) unless this is a macOS WebView
: 'download' in HTMLAnchorElement.prototype && !isMacOSWebView ? function saveAs(blob, name, opts) {
var URL = _global.URL || _global.webkitURL;
var a = document.createElement('a');
a.href = blob;
a.target = '_blank';
setTimeout(function () {
click(a);
});
}
} else {
navigator.msSaveOrOpenBlob(bom(blob, opts), name);
}
} // Fallback to using FileReader and a popup
: function saveAs(blob, name, opts, popup) {
// Open a popup immediately do go around popup blocker
// Mostly only available on user interaction and the fileReader is async so...
popup = popup || open('', '_blank');

if (popup) {
popup.document.title = popup.document.body.innerText = 'downloading...';
}

if (typeof blob === 'string') return download(blob, name, opts);
var force = blob.type === 'application/octet-stream';

var isSafari = /constructor/i.test(_global.HTMLElement) || _global.safari;

var isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent);

if ((isChromeIOS || force && isSafari || isMacOSWebView) && typeof FileReader !== 'undefined') {
// Safari doesn't allow downloading of blob URLs
var reader = new FileReader();

reader.onloadend = function () {
var url = reader.result;
url = isChromeIOS ? url : url.replace(/^data:[^;]*;/, 'data:attachment/file;');
if (popup) popup.location.href = url;else location = url;
popup = null; // reverse-tabnabbing #460
};

reader.readAsDataURL(blob);
} else {
var URL = _global.URL || _global.webkitURL;
var url = URL.createObjectURL(blob);
if (popup) popup.location = url;else location.href = url;
popup = null; // reverse-tabnabbing #460

setTimeout(function () {
URL.revokeObjectURL(url);
}, 4E4); // 40s
}
});
name = name || blob.name || 'download';
a.download = name;
a.rel = 'noopener'; // tabnabbing
// TODO: detect chrome extensions & packaged apps
// a.target = '_blank'

if (typeof blob === 'string') {
// Support regular links
a.href = blob;

if (a.origin !== location.origin) {
corsEnabled(a.href) ? download(blob, name, opts) : click(a, a.target = '_blank');
} else {
click(a);
}
} else {
// Support blobs
a.href = URL.createObjectURL(blob);
setTimeout(function () {
URL.revokeObjectURL(a.href);
}, 4E4); // 40s

setTimeout(function () {
click(a);
}, 0);
}
} // Use msSaveOrOpenBlob as a second approach
: 'msSaveOrOpenBlob' in navigator ? function saveAs(blob, name, opts) {
name = name || blob.name || 'download';

if (typeof blob === 'string') {
if (corsEnabled(blob)) {
download(blob, name, opts);
} else {
var a = document.createElement('a');
a.href = blob;
a.target = '_blank';
setTimeout(function () {
click(a);
});
}
} else {
navigator.msSaveOrOpenBlob(bom(blob, opts), name);
}
} // Fallback to using FileReader and a popup
: function saveAs(blob, name, opts, popup) {
// Open a popup immediately do go around popup blocker
// Mostly only available on user interaction and the fileReader is async so...
popup = popup || open('', '_blank');

if (popup) {
popup.document.title = popup.document.body.innerText = 'downloading...';
}

if (typeof blob === 'string') return download(blob, name, opts);
var force = blob.type === 'application/octet-stream';

var isSafari = /constructor/i.test(_global.HTMLElement) || _global.safari;

var isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent);

if ((isChromeIOS || force && isSafari || isMacOSWebView) && typeof FileReader !== 'undefined') {
// Safari doesn't allow downloading of blob URLs
var reader = new FileReader();

reader.onloadend = function () {
var url = reader.result;
url = isChromeIOS ? url : url.replace(/^data:[^;]*;/, 'data:attachment/file;');
if (popup) popup.location.href = url; else location = url;
popup = null; // reverse-tabnabbing #460
};

reader.readAsDataURL(blob);
} else {
var URL = _global.URL || _global.webkitURL;
var url = URL.createObjectURL(blob);
if (popup) popup.location = url; else location.href = url;
popup = null; // reverse-tabnabbing #460

setTimeout(function () {
URL.revokeObjectURL(url);
}, 4E4); // 40s
}
});
_global.saveAs = saveAs.saveAs = saveAs;

if (typeof module !== 'undefined') {
module.exports = saveAs;
}
});
}));
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
"glob": "^7.1.6",
"lookpath": "^1.2.2",
"rtlcss": "^3.1.2",
"wallpaper": "^4.4.2"
"wallpaper": "^4.4.2",
"web3.storage": "latest"
},
"devDependencies": {
"@electron-forge/cli": "^6.0.0-beta.65",
Expand Down Expand Up @@ -110,4 +111,4 @@
"bugs": {
"url": "https://github.com/1j01/jspaint/issues"
}
}
}
4 changes: 4 additions & 0 deletions src/electron-injected.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ window.setRepresentedFilename = (filePath) => {
window.setDocumentEdited = (documentEdited) => {
ipcRenderer.send("set-document-edited", documentEdited);
};
window.uploadToIpfs = (fileName) => {
ipcRenderer.send("upload-to-ipfs", fileName);
};

function show_save_error_message(responseCode, error) {
if (responseCode === "ACCESS_DENIED") {
Expand Down Expand Up @@ -102,6 +105,7 @@ window.systemHooks.showSaveFileDialog = async ({ formats, defaultFileName, defau
newFileFormatID: format.mimeType,
newFileHandle: filePath,
newBlob: blob,
newFilePath: filePath,
});
};
window.systemHooks.showOpenFileDialog = async ({ formats, defaultPath }) => {
Expand Down
9 changes: 9 additions & 0 deletions src/electron-main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { app, shell, session, dialog, ipcMain, BrowserWindow } = require('electron');
const fs = require("fs");
const path = require("path");
const { Web3Storage, getFilesFromPath } = require("web3.storage");

app.enableSandbox();

Expand Down Expand Up @@ -158,6 +159,14 @@ const createWindow = () => {
ipcMain.on("set-document-edited", (event, isEdited) => {
mainWindow.setDocumentEdited(isEdited);
});
ipcMain.on("upload-to-ipfs", async (event, fileName) => {
const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkaWQ6ZXRocjoweDU4ZDc1ZjYzN2Y5NDc2YzVkQmU1OGIxNzEyN0Q1MGU0NDgxMzUzQjQiLCJpc3MiOiJ3ZWIzLXN0b3JhZ2UiLCJpYXQiOjE2NjE0MDU2Mzc2MDQsIm5hbWUiOiJ4aW5taW5zdSJ9.sb1ATMTwOtsquSn6kTWQylCRUZjVDWrGUq5o6sLHlis";
const storage = new Web3Storage({ token });

const files = await getFilesFromPath(fileName);
const cid = await storage.put(files);
console.log('Content added with CID:', cid);
});
ipcMain.handle("show-save-dialog", async (event, options) => {
const { filePath, canceled } = await dialog.showSaveDialog(mainWindow, {
title: options.title,
Expand Down
Loading