Giter VIP home page Giter VIP logo

Comments (14)

Ivan-Feofanov avatar Ivan-Feofanov commented on May 28, 2024 1

Okay, i find something like solution

const fs = require('fs')
const path = require('path')
const tus = require('tus-node-server')

const server = new tus.Server()

const storageFolder = path.join(process.cwd(), '/files')

server.datastore = new tus.FileStore({
    path: `/${storageFolder}`
})

const metadataStringToObject = (stringValue) => {
  const keyValuePairList = stringValue.split(',')

  return keyValuePairList.reduce((metadata, keyValuePair) => {
    let [key, base64Value] = keyValuePair.split(' ')
    metadata[key] = new Buffer(base64Value, "base64").toString("ascii")

    return metadata
  }, {})
}

server.on(tus.EVENTS.EVENT_UPLOAD_COMPLETE, (event) => {
  fs.rename(`${storageFolder}/${event.file.id}`, `${storageFolder}/${metadataStringToObject(event.file.upload_metadata).filename}`)
})

const host = '0.0.0.0'
const port = 7000
server.listen({ host, port }, () => {
    console.log(`[${new Date().toLocaleTimeString()}] tus server listening at http://${host}:${port}`)
})

It may have an easier way?

from tus-node-server.

rictorres avatar rictorres commented on May 28, 2024 1

@djsg i see what's wrong.

you're calling fs.rename() without providing a callback for it (fs.rename(oldPath, newPath, callback)), so you'd write it like:

server.on(tus.EVENTS.EVENT_UPLOAD_COMPLETE, (event) => {
  const oldPath = `${storageFolder}/${event.file.id}`
  const newPath = `${storageFolder}/${metadataStringToObject(event.file.upload_metadata).filename}`
  
  fs.rename(oldPath, newPath, (err) => {
    // handle error in here
    console.error(err)
  })
})

there's also the sync version of rename although i wouldn't recommend in this case.

ref: https://nodejs.org/dist/latest-v8.x/docs/api/fs.html#fs_fs_rename_oldpath_newpath_callback

from tus-node-server.

djsg avatar djsg commented on May 28, 2024 1

@rictorres
Thx for your help. Now I've added in the demo code to rename uploaded file to its original name instead of id.

from tus-node-server.

djsg avatar djsg commented on May 28, 2024 1

@rictorres
How can I pass oldPath and newPath into callback function?
The following code gives oldPath undefined error.

fs.rename(oldPath, newPath, (err, oldPath, newpath) => {
    // handle error in here
    if(err != null)
        console.error(err)
    else {
        console.log('file ' + oldpath)
        console.log('saved as ' + newpath)
    }
})

from tus-node-server.

Acconut avatar Acconut commented on May 28, 2024 1

You pass oldpath to console.log in your callback, but the variable is called oldPath. Notice the capitalization. In JavaScript variable names are case sensitive, so oldpath is not oldPath.

from tus-node-server.

FaizalDwi avatar FaizalDwi commented on May 28, 2024 1

@FaizalDwi I do not see any immediate error. What is the value of event.file.upload_metadata?

it solved already. i changed it into 'name', instead of 'filename'
const newPath = '${storageFolder}/${metadataStringToObject(event.file.upload_metadata).name}'

from tus-node-server.

bhstahl avatar bhstahl commented on May 28, 2024

Hey @Ivan-Feofanov, correct. The easiest way could be to require the original file name to be passed as upload metadata.

from tus-node-server.

djsg avatar djsg commented on May 28, 2024

Hi,
tried your code, but got "let not defined reference error".
sorry, new to JavaScript.

from tus-node-server.

rictorres avatar rictorres commented on May 28, 2024

@djsg which version of node are you using?
let is a keyword like var

from tus-node-server.

djsg avatar djsg commented on May 28, 2024

It's version 4.2.6.
I updated my node version to 10.1.0.
Now got this error:

fs.js:148
throw new ERR_INVALID_CALLBACK();
^

TypeError [ERR_INVALID_CALLBACK]: Callback must be a function
at makeCallback (fs.js:148:11)
at Object.fs.rename (fs.js:694:14)
at FileStore.server.on (/path2 tus .../tus_node_server/tus_server_filename.js:25:6)

from tus-node-server.

djsg avatar djsg commented on May 28, 2024

@Acconut Thanks a lot.
Pardon me for my carelessness.

from tus-node-server.

Acconut avatar Acconut commented on May 28, 2024

No worries, you're welcome :)

from tus-node-server.

FaizalDwi avatar FaizalDwi commented on May 28, 2024

hi, i tried those codes and i still get my filename "undefined" instead of the original name after uploaded. i use Uppy tus as front-end.
this is my code from server.js

const path = require('path')
const tus = require('tus-node-server')

const server = new tus.Server()

const storageFolder = path.join(process.cwd(), '/uploads')

server.datastore = new tus.FileStore({
    path: `/${storageFolder}`
})

const metadataStringToObject = (stringValue) => {
  const keyValuePairList = stringValue.split(',')

  return keyValuePairList.reduce((metadata, keyValuePair) => {
    let [key, base64Value] = keyValuePair.split(' ')
    metadata[key] = new Buffer.from(base64Value, "base64").toString("ascii")

    return metadata
  }, {})
}

server.on(tus.EVENTS.EVENT_UPLOAD_COMPLETE, (event) => {
  const oldPath = `${storageFolder}/${event.file.id}`
  const newPath = `${storageFolder}/${metadataStringToObject(event.file.upload_metadata).filename}`
  
  fs.rename(oldPath, newPath, (err) => {
    // handle error in here
    if(err != null)
        console.error(err)
    else {
        console.log('file ' + oldPath)
        console.log('saved as ' + newPath)
    }
  })
})

const host = '127.0.0.1'
const port = 1080
server.listen({ host, port }, () => {
    console.log(`[${new Date().toLocaleTimeString()}] tus server running at http://${host}:${port}`)
})

did i make mistake from that code?

from tus-node-server.

Acconut avatar Acconut commented on May 28, 2024

@FaizalDwi I do not see any immediate error. What is the value of event.file.upload_metadata?

from tus-node-server.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.