tomdesair / tus-java-server

Library to receive tus v1.0.0 file uploads in a Java server environment
MIT License
131 stars 62 forks source link

[Bug]uploadInfo.isUploadInProgress() always be true even the uploading has finished #25

Open softboy99 opened 5 years ago

softboy99 commented 5 years ago

@Controller @RequestMapping("/upload") public class FileUploadController { @Value("${tus.server.data.directory}") protected String tusDataPath;

@Autowired
private TusFileUploadService tusFileUploadService;

@Autowired
private FileUploadService fileUploadService;

//@CrossOrigin(origins = "http://127.0.0.1:8080",exposedHeaders = {"Location","Upload-Offset","Upload-Length"})
@RequestMapping(value = {"", "/**"}, method = {RequestMethod.POST, RequestMethod.PATCH, RequestMethod.HEAD,
        RequestMethod.DELETE, RequestMethod.OPTIONS, RequestMethod.GET})
public void processUpload(final HttpServletRequest servletRequest, final HttpServletResponse servletResponse)  {
   // tusFileUploadService.withUploadURI(servletRequest.getContextPath() + "/upload");
    try {
        tusFileUploadService.process(servletRequest, servletResponse);
        String hLocation = servletResponse.getHeader(HttpHeader.LOCATION);
        if(hLocation!=null){
            while(true){
                UploadInfo ui = tusFileUploadService.getUploadInfo(hLocation);
                if(ui!=null && !ui.isUploadInProgress()) {
                    InputStream uploadedBytes = tusFileUploadService.getUploadedBytes(hLocation);
                    ///////////////////////////// others code here
                    break;
                }
            }
        }

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

}

ksvraja commented 5 years ago

I am not sure, what you are trying to do here..

You can look at the DiskStorageService implementation.

softboy99 commented 5 years ago

what i want is just to move the upload to another disk location when the upload completed. so i need to check ui.isUploadInProgress()

ksvraja commented 5 years ago

you should extend the DiskStorageService class and override the update method.

public void update(UploadInfo info) throws IOException, UploadNotFoundException {
        super.update(info);
        if (!info.isUploadInProgress()) {
            logger.debug("upload completed ");
// Do your stuff here.. 
        }
    }

and make sure while initializing TusFileUploadService set the storageService as your extended class/object -- withUploadStorageService()

tomdesair commented 5 years ago

Hi,

The library assumes that the upload is part of some web form. This means that FileUploadController will indeed only see "in progress" uploads as it will process the upload when the user is filling in the rest of the form.

After the user submits the form, a FormSubmissionController will then see all the completed uploads. You can take a look at the Dropwizard example here. I'll try to also extend my Spring Boot example with a form submission flow.

However, this indeed does not cover use cases where the file upload might happen without any web form (e.g. pure file transfers). I'm thinking about adding some kind of TusEvent callback system that would allow users of the library to add custom callbacks/code when certain type of Tus events happen like UPLOAD_CREATED, UPLOAD_COMPLETED, UPLOAD_FAILED, UPLOAD_CHUNK_PROCESSED, UPLOAD_DELETED...

For your use case, you could then add your own TusEventCallback implementation that listens to the UPLOAD_COMPLETED event. It can then do some custom processing on every completed upload like moving the file.

I'll see if I can work something out for this in the code in the coming days/weeks.

tomdesair commented 5 years ago

You can also take a look at this example: https://github.com/ralscha/blog2019/blob/51374dd/uploadtus/server/src/main/java/ch/rasc/upload/UploadController.java#L50

This developer seems to be doing something similar as you without the need for a separate form controller.

ksvraja commented 5 years ago

Hi,

The library assumes that the upload is part of some web form. This means that FileUploadController will indeed only see "in progress" uploads as it will process the upload when the user is filling in the rest of the form.

After the user submits the form, a FormSubmissionController will then see all the completed uploads. You can take a look at the Dropwizard example here. I'll try to also extend my Spring Boot example with a form submission flow.

However, this indeed does not cover use cases where the file upload might happen without any web form (e.g. pure file transfers). I'm thinking about adding some kind of TusEvent callback system that would allow users of the library to add custom callbacks/code when certain type of Tus events happen like UPLOAD_CREATED, UPLOAD_COMPLETED, UPLOAD_FAILED, UPLOAD_CHUNK_PROCESSED, UPLOAD_DELETED...

For your use case, you could then add your own TusEventCallback implementation that listens to the UPLOAD_COMPLETED event. It can then do some custom processing on every completed upload like moving the file.

I'll see if I can work something out for this in the code in the coming days/weeks.

Hi Tom, We would welcome this functionality in the standard library. In our current project, we capture these events by extending few methods on the DiskStorageService class and do some custom processing as we needed.