From d016eb8ca16456cda1a1e2f12a8c8637a71f51ff Mon Sep 17 00:00:00 2001 From: Eddie Wang Date: Fri, 20 Dec 2019 09:00:45 -0800 Subject: [PATCH] sync up tree --- packages/siaviewnode-server/package.json | 1 + packages/siaviewnode-server/src/main.ts | 103 ++++++++++++++--------- yarn.lock | 12 +++ 3 files changed, 75 insertions(+), 41 deletions(-) diff --git a/packages/siaviewnode-server/package.json b/packages/siaviewnode-server/package.json index 6cf75978..c1cf997d 100644 --- a/packages/siaviewnode-server/package.json +++ b/packages/siaviewnode-server/package.json @@ -17,6 +17,7 @@ "express-fileupload": "1.1.6", "express-session": "1.17.0", "ramda": "0.26.1", + "shortid": "2.2.15", "typescript": "^3.5.2" }, "devDependencies": { diff --git a/packages/siaviewnode-server/src/main.ts b/packages/siaviewnode-server/src/main.ts index e557edf4..7aae72ca 100644 --- a/packages/siaviewnode-server/src/main.ts +++ b/packages/siaviewnode-server/src/main.ts @@ -1,24 +1,29 @@ -import * as express from "express" -import * as fileUpload from "express-fileupload" -import * as R from "ramda" -import * as cors from "cors" -import axios from "axios" +import * as express from 'express' +import * as shortid from 'shortid' +import * as fileUpload from 'express-fileupload' +import * as R from 'ramda' +import * as cors from 'cors' +import axios from 'axios' -const SIAD_ENDPOINT = "http://localhost:9980" +const SIAD_ENDPOINT = 'http://localhost:9980' // simple siad connection with static strings const siad = axios.create({ baseURL: SIAD_ENDPOINT, headers: { - "User-Agent": "Sia-Agent", - "Access-Control-Allow-Origin": "*" + 'User-Agent': 'Sia-Agent', + 'Access-Control-Allow-Origin': '*', }, auth: { - username: "", - password: "d05bb024715aea0bb734ce057acbae27" - } + username: '', + password: 'd05bb024715aea0bb734ce057acbae27', + }, }) +// Ramda shared utility functions +const selectFile = R.path(['files', 'file']) +const pName = R.prop('name') + declare var __DEV__: boolean export class Server { @@ -43,7 +48,7 @@ export class Server { } if (__DEV__) { - console.log("> in development") + console.log('> in development') } console.log(`> listening on port ${this.port}`) @@ -54,56 +59,72 @@ export class Server { private setRoutes = (): void => { this.app.use( cors({ - origin: "http://localhost:*", - credentials: true - }) + origin: 'http://localhost:*', + credentials: true, + }), ) this.app.use( fileUpload({ - limits: { fileSize: 10 * 1024 * 1024 } - }) + limits: { fileSize: 10 * 1024 * 1024 }, + }), ) - this.app.post("/siafile", this.postSiaFile) - this.app.get("/siafile/download", this.downloadSiaFile) + // siafile + this.app.post('/siafile', this.postSiaFile) + // linkfile + this.app.post('/linkfile', this.handleLinkUpload) } - private async downloadSiaFile(req, res) {} + private async handleLinkUpload( + req: express.Request, + res: express.Response, + ): Promise { + const fileToUpload: any = selectFile(req) + console.log('file:', fileToUpload) + const filename = pName(fileToUpload) + console.log('filename:', filename) + const uid = shortid.generate() + console.log('uid:', uid) + try { + // TODO: add uuid so we don't collide + const { data } = await siad.post(`/renter/linkfile/linkfiles/${uid}`, fileToUpload.data, { + params: { + name: filename, + }, + }) + console.log('data is ', data) + return res.send(data) + } catch (err) { + console.log('err', err) + return res.sendStatus(500) + } + } private async postSiaFile( req: express.Request & any, - res: express.Response + res: express.Response, ): Promise { - const selectFile = R.path(["files", "file"]) try { const file: any = selectFile(req) - const selectContentLength = R.path(["headers", "Content-Length"]) + const selectContentLength = R.path(['headers', 'Content-Length']) const cl = selectContentLength(req) - console.log("cl is", cl) + console.log('cl is', cl) - console.log("file is", file) + console.log('file is', file) - const { data: stream, headers } = await siad.post( - "/renter/stream", - file.data, - { - responseType: "stream" - } - ) - const contentLength = headers["Content-Length"] + const { data: stream, headers } = await siad.post('/renter/stream', file.data, { + responseType: 'stream', + }) + const contentLength = headers['Content-Length'] - const pName = R.prop("name") - const splitFilename = R.compose(R.head, R.split(".sia")) + const splitFilename = R.compose(R.head, R.split('.sia')) const fileName = R.compose(splitFilename, pName)(file) - res.set( - "Content-Disposition", - `attachment; filename="${fileName}"; filename*="${fileName}"` - ) - res.set("Content-Length", contentLength) + res.set('Content-Disposition', `attachment; filename="${fileName}"; filename*="${fileName}"`) + res.set('Content-Length', contentLength) stream.pipe(res) } catch (e) { - console.log("postSiaFile err:", e) + console.log('postSiaFile err:', e) return res.json({ error: e.message }) } } diff --git a/yarn.lock b/yarn.lock index 3f006b64..48da2a72 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6451,6 +6451,11 @@ nan@^2.12.1: resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.0.tgz#7818f722027b2459a86f0295d434d1fc2336c52c" integrity sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg== +nanoid@^2.1.0: + version "2.1.8" + resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-2.1.8.tgz#2dbb0224231b246e3b4c819de7bfea6384dabf08" + integrity sha512-g1z+n5s26w0TGKh7gjn7HCqurNKMZWzH08elXzh/gM/csQHd/UqDV6uxMghQYg9IvqRPm1QpeMk50YMofHvEjQ== + nanomatch@^1.2.9: version "1.2.13" resolved "https://registry.yarnpkg.com/nanomatch/-/nanomatch-1.2.13.tgz#b87a8aa4fc0de8fe6be88895b38983ff265bd119" @@ -8543,6 +8548,13 @@ shell-quote@^1.6.1: resolved "https://registry.yarnpkg.com/shell-quote/-/shell-quote-1.7.2.tgz#67a7d02c76c9da24f99d20808fcaded0e0e04be2" integrity sha512-mRz/m/JVscCrkMyPqHc/bczi3OQHkLTqXHEFu0zDhK/qfv3UcOA4SVmRCLmos4bhjr9ekVQubj/R7waKapmiQg== +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== + dependencies: + nanoid "^2.1.0" + signal-exit@^3.0.0, signal-exit@^3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"