tus-java-server icon indicating copy to clipboard operation
tus-java-server copied to clipboard

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

Open softboy99 opened this issue 6 years ago • 6 comments

@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();
    }
}

}

softboy99 avatar Sep 05 '19 12:09 softboy99

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

You can look at the DiskStorageService implementation.

ksvraja avatar Sep 05 '19 13:09 ksvraja

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

softboy99 avatar Sep 06 '19 02:09 softboy99

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()

ksvraja avatar Sep 06 '19 03:09 ksvraja

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 avatar Sep 07 '19 11:09 tomdesair

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.

tomdesair avatar Sep 07 '19 14:09 tomdesair

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.

ksvraja avatar Sep 07 '19 14:09 ksvraja