package lan.dk.podcastserver.controller.api; import lan.dk.podcastserver.business.ItemBusiness; import lan.dk.podcastserver.entity.Item; import lan.dk.podcastserver.exception.PodcastNotFoundException; import lan.dk.podcastserver.manager.ItemDownloadManager; import lan.dk.podcastserver.utils.facade.PageRequestFacade; import lan.dk.podcastserver.utils.multipart.MultipartFileSender; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URISyntaxException; import java.text.ParseException; /** * Created by kevin on 26/12/2013. * See full code here : https://github.com/davinkevin/Podcast-Server/blob/d927d9b8cb9ea1268af74316cd20b7192ca92da7/src/main/java/lan/dk/podcastserver/controller/api/ItemController.java */ @RestController @RequestMapping("/api/podcast/{idPodcast}/items") public class ItemController { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); @Resource private ItemBusiness itemBusiness; @Autowired protected ItemDownloadManager itemDownloadManager; @RequestMapping(value="{id:[\\d]+}/download{ext}", method = RequestMethod.GET) public void getEpisodeFile(@PathVariable Integer id, HttpServletRequest request, HttpServletResponse response) throws Exception { logger.debug("Download du fichier d'item {}", id); Item item = itemBusiness.findOne(id); if (item.isDownloaded()) { logger.debug("Récupération en local de l'item {} au chemin {}", id, item.getLocalUri()); MultipartFileSender.fromPath(item.getLocalPath()) .with(request) .with(response) .serveResource(); } else { response.sendRedirect(item.getUrl()); } } }