Comments (7)
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.
Hi
do you mean the meta data be request header or body set by the client?
from tus-java-server.
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.
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.
Hi,
@tomdesair , could you please help me? Thanks a lot!
from tus-java-server.
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.
You can also take a look at this example from another developer:
- https://github.com/ralscha/blog2019/blob/51374dd/uploadtus/client/src/app/home/home.page.ts#L83
- https://github.com/ralscha/blog2019/blob/51374dd/uploadtus/server/src/main/java/ch/rasc/upload/UploadController.java#L66
from tus-java-server.
Related Issues (20)
- [Bug]uploadInfo.isUploadInProgress() always be true even the uploading has finished HOT 6
- A base URI other than '/upload' causes a null pointer exception when calling getUploadInfo HOT 1
- Avoid file system locks in DiskStorageService
- Possible to add a body in the response ?
- Method to add URL sent to Tus client?
- Cannot resume upload after server unexpected shutdown
- Error when upload file to tus on eureka gateway HOT 1
- Java TUS Server returns Location without url hostname, which breaks flutter
- Exception if cleanup called before any request handled
- Undertow throws exception when TUS tries to respond with 409 or some other error status HOT 1
- Unexpected single newline character in chunk size, response head already sent HOT 2
- Providing our own error message
- Release latest changes HOT 1
- file feature download chinese filename error HOT 1
- java17 support HOT 4
- Response as JSON
- Is there any configuration required to use this in the spring boot server? HOT 1
- file getUploadedBytes UploadNotFoundException.
- Javadoc for this lib
- [Question] Is there any plan to support creation-with-upload extension?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tus-java-server.