Giter Site home page Giter Site logo

Comments (7)

tomdesair avatar tomdesair commented on May 14, 2024 1

Hi @softboy99,

The tus protocol itself makes abstraction of files: "the specification does not have the principle of a disk-based file, allowing you to upload arbitrary data using tus" (https://tus.io/protocols/resumable-upload.html#how-can-i-get-the-file-name-or-file-type-for-an-upload). This means the tus Java server implementation cannot rely on the fact that an upload is always a "file". It could also be for example a video stream.

In addition, you should always consider the upload directory as a temporary directory. You should not use it as the permanent destination of the uploaded bytes.

However, the Uppy file uploader does provide the filename as metadata. The tus Java server implementation supports this through the getFileName() method. But note that this method only gives a meaningful value if you upload files with Uppy (or any other uploader that passes the filename as metadata). So if you are uploading files with Uppy, you could copy the uploaded bytes to their final destination in a new file with the correct name.

For example we could extend the Dropwizard example to

if(!uploadInfo.isUploadInProgress()) {
    InputStream uploadedBytes = tusFileUploadService.getUploadedBytes(url, ownerKey);

    Path newFile = Paths.get("my/target/directory", uploadInfo.getFileName());

    java.nio.file.Files.copy(
        uploadedBytes, 
        newFile, 
        StandardCopyOption.REPLACE_EXISTING);

    //Since we're done with processing the upload, we can safely remove it now
    tusFileUploadService.deleteUpload(url, ownerKey);
}

Does this answer your questions?

from tus-java-server.

softboy99 avatar softboy99 commented on May 14, 2024

Hi
do you mean the meta data be request header or body set by the client?

from tus-java-server.

softboy99 avatar softboy99 commented on May 14, 2024

Hi,
My uploadInfo alwasy be null. I'm using it in spring boot rest controller. after tusFileUploadService.process call. Can you help me? Thanks a lot

from tus-java-server.

softboy99 avatar softboy99 commented on May 14, 2024

Hi, The following is my code:
`try {
tusFileUploadService.process(servletRequest, servletResponse);
String hLocation = servletResponse.getHeader(HttpHeader.LOCATION);
if(hLocation!=null){
UploadInfo ui = tusFileUploadService.getUploadInfo(hLocation);
if(ui!=null && !ui.isUploadInProgress()) {
InputStream uploadedBytes = tusFileUploadService.getUploadedBytes(hLocation);
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String[] sDateArr = sdf.format(d).split("-");
String fileName = servletRequest.getHeader("fileName");
Path newFile = Paths.get(tusDataPath,sDateArr[0],sDateArr[1],sDateArr[2], fileName);
java.nio.file.Files.copy(uploadedBytes, newFile, StandardCopyOption.REPLACE_EXISTING);
//Since we're done with processing the upload, we can safely remove it now
tusFileUploadService.deleteUpload(hLocation);
String entityName = servletRequest.getHeader("entityName");
String fieldName = servletRequest.getHeader("fieldName");
String keyName = servletRequest.getHeader("keyName");
String keyValue = servletRequest.getHeader("keyValue");
String uploadType = servletRequest.getHeader("uploadType");
String deviceNo = servletRequest.getHeader("deviceNo");
long operId = Long.parseLong(servletRequest.getHeader("operId"));
servletResponse.setHeader("Location",newFile.toString());
//fileUploadService.updateFileLocation(uploadType,entityName,fieldName,newFile.toString(),keyName,keyValue,deviceNo,operId);
}
}

    } catch (IOException | TusException e) {
        e.printStackTrace();
    }`

if(ui!=null && !ui.isUploadInProgress()) never be executed

from tus-java-server.

softboy99 avatar softboy99 commented on May 14, 2024

Hi,
@tomdesair , could you please help me? Thanks a lot!

from tus-java-server.

tomdesair avatar tomdesair commented on May 14, 2024

Hi @softboy99,

You need to provide any metadata about the upload (like mime-type, filename, ...) in the Upload-Metadata HTTP header. Make sure to read this: https://tus.io/protocols/resumable-upload.html#upload-metadata

For example if you provide the header Upload-Metadata: filename d29ybGRfZG9taW5hdGlvbiBwbGFuLnBkZg==,mimetype YXBwbGljYXRpb24vcGRm when creating the upload, the library will parse the metadata in that header. The UploadInfo.getMetadata() method will then return a Map containing entries ("filename", "world_domination plan.pdf") and ("mimetype", "application/pdf"). Method UploadInfo.getFileName() will look for the filename entry and will thus return world_domination plan.pdf in this example.

Also see this unit test.

Does this help?

from tus-java-server.

tomdesair avatar tomdesair commented on May 14, 2024

You can also take a look at this example from another developer:

from tus-java-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.