sync up tree

This commit is contained in:
Eddie Wang 2019-12-20 09:00:45 -08:00
parent f9d3d8b7f3
commit d016eb8ca1
No known key found for this signature in database
GPG Key ID: 595553ADC5949F24
3 changed files with 75 additions and 41 deletions

View File

@ -17,6 +17,7 @@
"express-fileupload": "1.1.6", "express-fileupload": "1.1.6",
"express-session": "1.17.0", "express-session": "1.17.0",
"ramda": "0.26.1", "ramda": "0.26.1",
"shortid": "2.2.15",
"typescript": "^3.5.2" "typescript": "^3.5.2"
}, },
"devDependencies": { "devDependencies": {

View File

@ -1,24 +1,29 @@
import * as express from "express" import * as express from 'express'
import * as fileUpload from "express-fileupload" import * as shortid from 'shortid'
import * as R from "ramda" import * as fileUpload from 'express-fileupload'
import * as cors from "cors" import * as R from 'ramda'
import axios from "axios" 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 // simple siad connection with static strings
const siad = axios.create({ const siad = axios.create({
baseURL: SIAD_ENDPOINT, baseURL: SIAD_ENDPOINT,
headers: { headers: {
"User-Agent": "Sia-Agent", 'User-Agent': 'Sia-Agent',
"Access-Control-Allow-Origin": "*" 'Access-Control-Allow-Origin': '*',
}, },
auth: { auth: {
username: "", username: '',
password: "d05bb024715aea0bb734ce057acbae27" password: 'd05bb024715aea0bb734ce057acbae27',
} },
}) })
// Ramda shared utility functions
const selectFile = R.path(['files', 'file'])
const pName = R.prop('name')
declare var __DEV__: boolean declare var __DEV__: boolean
export class Server { export class Server {
@ -43,7 +48,7 @@ export class Server {
} }
if (__DEV__) { if (__DEV__) {
console.log("> in development") console.log('> in development')
} }
console.log(`> listening on port ${this.port}`) console.log(`> listening on port ${this.port}`)
@ -54,56 +59,72 @@ export class Server {
private setRoutes = (): void => { private setRoutes = (): void => {
this.app.use( this.app.use(
cors({ cors({
origin: "http://localhost:*", origin: 'http://localhost:*',
credentials: true credentials: true,
}) }),
) )
this.app.use( this.app.use(
fileUpload({ fileUpload({
limits: { fileSize: 10 * 1024 * 1024 } limits: { fileSize: 10 * 1024 * 1024 },
}) }),
) )
this.app.post("/siafile", this.postSiaFile) // siafile
this.app.get("/siafile/download", this.downloadSiaFile) 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<express.Response> {
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( private async postSiaFile(
req: express.Request & any, req: express.Request & any,
res: express.Response res: express.Response,
): Promise<express.Response> { ): Promise<express.Response> {
const selectFile = R.path(["files", "file"])
try { try {
const file: any = selectFile(req) const file: any = selectFile(req)
const selectContentLength = R.path(["headers", "Content-Length"]) const selectContentLength = R.path(['headers', 'Content-Length'])
const cl = selectContentLength(req) 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( const { data: stream, headers } = await siad.post('/renter/stream', file.data, {
"/renter/stream", responseType: 'stream',
file.data, })
{ const contentLength = headers['Content-Length']
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) const fileName = R.compose(splitFilename, pName)(file)
res.set( res.set('Content-Disposition', `attachment; filename="${fileName}"; filename*="${fileName}"`)
"Content-Disposition", res.set('Content-Length', contentLength)
`attachment; filename="${fileName}"; filename*="${fileName}"`
)
res.set("Content-Length", contentLength)
stream.pipe(res) stream.pipe(res)
} catch (e) { } catch (e) {
console.log("postSiaFile err:", e) console.log('postSiaFile err:', e)
return res.json({ error: e.message }) return res.json({ error: e.message })
} }
} }

View File

@ -6451,6 +6451,11 @@ nan@^2.12.1:
resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.0.tgz#7818f722027b2459a86f0295d434d1fc2336c52c" resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.0.tgz#7818f722027b2459a86f0295d434d1fc2336c52c"
integrity sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg== 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: nanomatch@^1.2.9:
version "1.2.13" version "1.2.13"
resolved "https://registry.yarnpkg.com/nanomatch/-/nanomatch-1.2.13.tgz#b87a8aa4fc0de8fe6be88895b38983ff265bd119" 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" resolved "https://registry.yarnpkg.com/shell-quote/-/shell-quote-1.7.2.tgz#67a7d02c76c9da24f99d20808fcaded0e0e04be2"
integrity sha512-mRz/m/JVscCrkMyPqHc/bczi3OQHkLTqXHEFu0zDhK/qfv3UcOA4SVmRCLmos4bhjr9ekVQubj/R7waKapmiQg== 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: signal-exit@^3.0.0, signal-exit@^3.0.2:
version "3.0.2" version "3.0.2"
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d" resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"