rewrite api endpoint

This commit is contained in:
Karol Wypchlo 2020-02-17 14:27:47 +01:00
parent 2326b23c10
commit 76ce36a7e1
4 changed files with 43 additions and 45 deletions

View File

@ -21,7 +21,8 @@
"react-reveal": "^1.2.2", "react-reveal": "^1.2.2",
"react-scripts": "^3.4.0", "react-scripts": "^3.4.0",
"react-syntax-highlighter": "^12.2.1", "react-syntax-highlighter": "^12.2.1",
"react-visibility-sensor": "^5.1.1" "react-visibility-sensor": "^5.1.1",
"shortid": "^2.2.15"
}, },
"scripts": { "scripts": {
"predeploy": "npm run build", "predeploy": "npm run build",

View File

@ -2,7 +2,7 @@ import React, { Component } from 'react'
import classNames from 'classnames' import classNames from 'classnames'
import Dropzone from 'react-dropzone' import Dropzone from 'react-dropzone'
import Reveal from 'react-reveal/Reveal' import Reveal from 'react-reveal/Reveal'
import shortid from 'shortid';
import { Button, UploadFile } from '../' import { Button, UploadFile } from '../'
import { Deco3, Deco4, Deco5, Folder, DownArrow } from '../../svg' import { Deco3, Deco4, Deco5, Folder, DownArrow } from '../../svg'
import './HomeUpload.scss' import './HomeUpload.scss'
@ -13,47 +13,41 @@ export default class HomeUpload extends Component {
handleDrop = async acceptedFiles => { handleDrop = async acceptedFiles => {
this.setState({ this.setState({
files: [ files: [
...acceptedFiles.map(file => { ...acceptedFiles.map(file => ({ file, status: 'uploading' })),
return { file, status: 'uploading' }
}),
...this.state.files, ...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 => { 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 { try {
const response = await fetch(url, { const fd = new FormData()
method: 'POST', fd.append('file', file)
body: fd,
mode: 'cors', const uuid = shortid.generate();
}) const response = await fetch(`/skynet/skyfile/${uuid}`, { method: 'POST', body: fd })
const { skylink } = await response.json() const { skylink } = await response.json()
onComplete('complete', skylink) onComplete(file, 'complete', skylink)
} catch (error) { } catch (error) {
onComplete('error') onComplete(file, 'error')
} }
}) })
} }

View File

@ -6,7 +6,6 @@ import requestId from "express-request-id"
import fs from "fs" import fs from "fs"
import morgan from 'morgan' import morgan from 'morgan'
import { homedir } from "os" import { homedir } from "os"
import shortid from "shortid"
import { Logger } from "winston" import { Logger } from "winston"
import logger from "./logger" import logger from "./logger"
@ -26,7 +25,7 @@ const siad = axios.create({
} }
}) })
export class Server { class Server {
public app: express.Express public app: express.Express
constructor(private logger: Logger) { constructor(private logger: Logger) {
@ -78,7 +77,7 @@ export class Server {
} }
private configureRoutes() { private configureRoutes() {
this.app.post("/skyfile", this.handleSkyfilePOST.bind(this)) this.app.post("/skynet/skyfile/:uuid", this.handleSkyfilePOST.bind(this))
this.app.get( this.app.get(
"/stats", this.handleStatsGET.bind(this) "/stats", this.handleStatsGET.bind(this)
@ -151,16 +150,20 @@ export class Server {
res.status(400).send({ error: "Missing file" }) res.status(400).send({ error: "Missing file" })
} }
const uid = shortid.generate() if( !req.params.uuid ) {
this.logger.info(`POST skyfile w/name ${file.name} and uid ${uid}`) res.status(400).send({ error: "Missing uuid" })
}
this.logger.info(`POST skyfile w/name ${file.name} and uid ${req.params.uuid}`)
try { try {
const { data } = await siad.post( const { data } = await siad.post(
`/skynet/skyfile/${uid}`, `/skynet/skyfile/${req.params.uuid}`,
file.data, file.data,
{ {
maxContentLength: MAX_UPLOAD_FILESIZE, params: {
params: { filename: file.name } filename: file.name
}
} }
) )
return res.send(data) return res.send(data)
@ -172,4 +175,4 @@ export class Server {
} }
} }
module.exports = new Server(logger).app export default new Server(logger).app

View File

@ -10914,7 +10914,7 @@ shellwords@^0.1.1:
resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.1.tgz#d6b9181c1a48d397324c84871efbcfc73fc0654b" resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.1.tgz#d6b9181c1a48d397324c84871efbcfc73fc0654b"
integrity sha512-vFwSUfQvqybiICwZY5+DAWIPLKsWO31Q91JSKl3UYv+K5c2QRPzn0qzec6QPu1Qc9eHYItiP3NdJqNVqetYAww== integrity sha512-vFwSUfQvqybiICwZY5+DAWIPLKsWO31Q91JSKl3UYv+K5c2QRPzn0qzec6QPu1Qc9eHYItiP3NdJqNVqetYAww==
shortid@2.2.15: shortid@2.2.15, shortid@^2.2.15:
version "2.2.15" version "2.2.15"
resolved "https://registry.yarnpkg.com/shortid/-/shortid-2.2.15.tgz#2b902eaa93a69b11120373cd42a1f1fe4437c122" resolved "https://registry.yarnpkg.com/shortid/-/shortid-2.2.15.tgz#2b902eaa93a69b11120373cd42a1f1fe4437c122"
integrity sha512-5EaCy2mx2Jgc/Fdn9uuDuNIIfWBpzY4XIlhoqtXF6qsf+/+SGZ+FxDdX/ZsMZiWupIWNqAEmiNY4RC+LSmCeOw== integrity sha512-5EaCy2mx2Jgc/Fdn9uuDuNIIfWBpzY4XIlhoqtXF6qsf+/+SGZ+FxDdX/ZsMZiWupIWNqAEmiNY4RC+LSmCeOw==