From 76ce36a7e10029da9ac7f45deef7844a0eaf91bb Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Mon, 17 Feb 2020 14:27:47 +0100 Subject: [PATCH] rewrite api endpoint --- packages/sia-skynet/package.json | 3 +- .../src/components/HomeUpload/HomeUpload.js | 60 +++++++++---------- packages/siaviewnode-server/src/main.ts | 23 +++---- yarn.lock | 2 +- 4 files changed, 43 insertions(+), 45 deletions(-) diff --git a/packages/sia-skynet/package.json b/packages/sia-skynet/package.json index 0b499858..61d875bf 100644 --- a/packages/sia-skynet/package.json +++ b/packages/sia-skynet/package.json @@ -21,7 +21,8 @@ "react-reveal": "^1.2.2", "react-scripts": "^3.4.0", "react-syntax-highlighter": "^12.2.1", - "react-visibility-sensor": "^5.1.1" + "react-visibility-sensor": "^5.1.1", + "shortid": "^2.2.15" }, "scripts": { "predeploy": "npm run build", diff --git a/packages/sia-skynet/src/components/HomeUpload/HomeUpload.js b/packages/sia-skynet/src/components/HomeUpload/HomeUpload.js index 80f88644..bccad101 100644 --- a/packages/sia-skynet/src/components/HomeUpload/HomeUpload.js +++ b/packages/sia-skynet/src/components/HomeUpload/HomeUpload.js @@ -2,7 +2,7 @@ import React, { Component } from 'react' import classNames from 'classnames' import Dropzone from 'react-dropzone' import Reveal from 'react-reveal/Reveal' - +import shortid from 'shortid'; import { Button, UploadFile } from '../' import { Deco3, Deco4, Deco5, Folder, DownArrow } from '../../svg' import './HomeUpload.scss' @@ -13,47 +13,41 @@ export default class HomeUpload extends Component { handleDrop = async acceptedFiles => { this.setState({ files: [ - ...acceptedFiles.map(file => { - return { file, status: 'uploading' } - }), + ...acceptedFiles.map(file => ({ file, status: 'uploading' })), ...this.state.files, ], }) + const onComplete = (file, status, skylink) => { + this.setState(state => { + const index = state.files.findIndex(f => f.file === file) + + return { + files: [ + ...state.files.slice(0, index), + { + ...state.files[index], + status, + url: `https://siasky.net/${skylink}`, + }, + ...state.files.slice(index + 1), + ], + } + }) + } + acceptedFiles.forEach(async file => { - const url = `/api/skyfile?filename=${file.name}` - const fd = new FormData() - fd.append('file', file) - - const onComplete = (status, skylink) => { - this.setState(state => { - const index = state.files.findIndex(f => f.file === file) - - return { - files: [ - ...state.files.slice(0, index), - { - ...state.files[index], - status, - url: `https://siasky.net/${skylink}`, - }, - ...state.files.slice(index + 1), - ], - } - }) - } - try { - const response = await fetch(url, { - method: 'POST', - body: fd, - mode: 'cors', - }) + const fd = new FormData() + fd.append('file', file) + + const uuid = shortid.generate(); + const response = await fetch(`/skynet/skyfile/${uuid}`, { method: 'POST', body: fd }) const { skylink } = await response.json() - onComplete('complete', skylink) + onComplete(file, 'complete', skylink) } catch (error) { - onComplete('error') + onComplete(file, 'error') } }) } diff --git a/packages/siaviewnode-server/src/main.ts b/packages/siaviewnode-server/src/main.ts index 1394210a..3cfb2680 100644 --- a/packages/siaviewnode-server/src/main.ts +++ b/packages/siaviewnode-server/src/main.ts @@ -6,7 +6,6 @@ import requestId from "express-request-id" import fs from "fs" import morgan from 'morgan' import { homedir } from "os" -import shortid from "shortid" import { Logger } from "winston" import logger from "./logger" @@ -26,7 +25,7 @@ const siad = axios.create({ } }) -export class Server { +class Server { public app: express.Express constructor(private logger: Logger) { @@ -78,7 +77,7 @@ export class Server { } private configureRoutes() { - this.app.post("/skyfile", this.handleSkyfilePOST.bind(this)) + this.app.post("/skynet/skyfile/:uuid", this.handleSkyfilePOST.bind(this)) this.app.get( "/stats", this.handleStatsGET.bind(this) @@ -151,16 +150,20 @@ export class Server { res.status(400).send({ error: "Missing file" }) } - const uid = shortid.generate() - this.logger.info(`POST skyfile w/name ${file.name} and uid ${uid}`) + if( !req.params.uuid ) { + res.status(400).send({ error: "Missing uuid" }) + } + + this.logger.info(`POST skyfile w/name ${file.name} and uid ${req.params.uuid}`) try { const { data } = await siad.post( - `/skynet/skyfile/${uid}`, + `/skynet/skyfile/${req.params.uuid}`, file.data, - { - maxContentLength: MAX_UPLOAD_FILESIZE, - params: { filename: file.name } + { + params: { + filename: file.name + } } ) return res.send(data) @@ -172,4 +175,4 @@ export class Server { } } -module.exports = new Server(logger).app +export default new Server(logger).app diff --git a/yarn.lock b/yarn.lock index 43136d51..60475f46 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10914,7 +10914,7 @@ shellwords@^0.1.1: resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.1.tgz#d6b9181c1a48d397324c84871efbcfc73fc0654b" integrity sha512-vFwSUfQvqybiICwZY5+DAWIPLKsWO31Q91JSKl3UYv+K5c2QRPzn0qzec6QPu1Qc9eHYItiP3NdJqNVqetYAww== -shortid@2.2.15: +shortid@2.2.15, shortid@^2.2.15: version "2.2.15" resolved "https://registry.yarnpkg.com/shortid/-/shortid-2.2.15.tgz#2b902eaa93a69b11120373cd42a1f1fe4437c122" integrity sha512-5EaCy2mx2Jgc/Fdn9uuDuNIIfWBpzY4XIlhoqtXF6qsf+/+SGZ+FxDdX/ZsMZiWupIWNqAEmiNY4RC+LSmCeOw==