3

I have two entities photo and video, basically i need a controller that can upload both picture and video. an eg scenario is i execute the controller it opens my file and if i select a video it is handled as an mp4 with the video entity and if i select a picture it uses the photo entity and handles it as an image. Both entities have Multipartfile attribute to denote image and video.

Baically i have seen this link that has an answer for uploading a video How to Implement HTTP byte-range requests in Spring MVC

Another example would be in social apps we use one click to upload either a photo or a video

This is what i have currently

@RequestMapping(value = "/Upload", method = RequestMethod.POST)
public String  FileUpload(HttpServletRequest request,
  @RequestParam("fileUpload")  MultipartFile[] fileUpload) throws    Exception {


}

I would like to use the MultipartFileSender from the above link but not sure how handle it with two different enities one for video and photo

currently i have this

  @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String Post(@Nullable Photo photo, @Nullable Video video,
                       HttpServletRequest request, HttpServletResponse response) {

     String ext1 = FilenameUtils.getExtension("/path/to/file/foo.txt");
     if(ext1.matches("png")) {

       MultipartFile bookImage = photo.getImage();
       try {
           byte[] bytes = bookImage.getBytes();
           String name = photo.getId() + ".png";
           BufferedOutputStream stream =
               new BufferedOutputStream(
                   new FileOutputStream(new File("src/main/resources/static/image/book/" + name)));
           stream.write(bytes);
           stream.close();

           photoRepository.save(photo);
       } catch (Exception e) {
           e.printStackTrace();
       }

      }  else {
         /*

          */
          MultipartFile videoFile = video.getVideo();
         /**
          * not sure how to  continue about this
the  class bellow MultipartFileSender can be found here https://stackoverflow.com/questions/28427339/how-to-implement-http-byte-range-requests-in-spring-mvc  i am using that because i need a byte range request for the video upload 
          */
         MultipartFileSender.fromFile(File( ))
                              .with(request)
             .with(response)
             .serveResource();
   }
      return null;
 }
valik
  • 2,014
  • 4
  • 24
  • 55
  • 1
    Have you thought about the answer below ? – Zeusox Nov 06 '18 at 16:11
  • 2
    yes i have thank you for writing , i was thinking how to implement it i will post a code and may be you can see and add some ideas to it , but i also want to use the idea from the link i added to the question regarding mp4 files only im not sure how to use them – valik Nov 06 '18 at 17:25
  • 2
    That should not be an issue at all. You can implement it along what I have specified in my answer...If you think my answer is relevant, upvote it or something so others see its usefulness. Again, I can go along with you to help you implement this... – Zeusox Nov 06 '18 at 17:36
  • @IssmeilEL. i have updated my question with what i have currently as the controller may be you can add some insights to it thanks – valik Nov 06 '18 at 17:55
  • So what is the problem with your Controller now ? I mean I need to know what is going on when you try to fire it ? – Zeusox Nov 06 '18 at 18:00
  • nothing yet i am still struggling with the algorithm and how to use the class MultipartFileSender from the link @IssmeilEL. – valik Nov 06 '18 at 18:09
  • Have you checked this out: https://gist.github.com/davinkevin/b97e39d7ce89198774b4 ?? – Zeusox Nov 06 '18 at 18:16
  • this code uses the same class as the one in the other link ? – valik Nov 06 '18 at 18:21
  • i noticed also that in that link you sent , first the got the item from db by id then added a video to it ? but i want to post a video like an instagram app, does it make sense to first save the entity before adding video to it ? – valik Nov 06 '18 at 18:23
  • Are you trying to add the video in a local directory and then take its name and store it in the DB ? – Zeusox Nov 06 '18 at 18:34
  • yes video or pictures should be stored in a file could be system file but other properties like "tags" "comments" likes and name should be stored in the db – valik Nov 06 '18 at 19:33
  • Ok going back to your previous question if it makes sense to save the entity before adding the video to it. I would say you just have to get the file name with its extension, store in the DB and immediately after that store your video in what directory you prefer. This way you would have your video or image in your directory with its name reference in the database so you can retrieve it whenever you want... – Zeusox Nov 06 '18 at 19:36

1 Answers1

1

There are different ways to do this. I have done something similar, but I kinda used some tricks to get to a result similar to yours.

1- create 2 separate array list with all possible video and image extensions

1 - Create a method that would get your media's file extension

2 - create a method that would compare the file extension you get against your lists of arrays that includes all possible video and image extensions.

This way you will separate between what is video and image and handle them differently. Hope this helps !

Zeusox
  • 7,708
  • 10
  • 31
  • 60