add MediaFile from queue

This commit is contained in:
Thomas Peetz
2025-07-11 16:49:42 +02:00
parent c6fd80408b
commit 0d99f383fb
5 changed files with 69 additions and 5 deletions
+1 -1
View File
@@ -49,10 +49,10 @@ class AddLinkMessage(MessagingHandler):
message = Message(body=self.url, address=self.address, content_type="application/json", durable=True)
delivery = event.sender.send(message)
self.log.info(f"Delivery {delivery} sent")
event.connection.close()
def on_accepted(self, event: Event) -> None:
self.log.info(f"accepted Delivery: {event.delivery.remote_state}")
event.connection.close()
def on_rejected(self, event: Event) -> None:
@@ -1,15 +1,28 @@
package de.thpeetz.kontor.integration.routes;
import de.thpeetz.kontor.integration.services.AddLinkProcessor;
import de.thpeetz.kontor.media.services.MediaFileService;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class AddLinkFromQueue extends RouteBuilder {
@Autowired
private final MediaFileService mediaFileService;
@Autowired
public AddLinkFromQueue(MediaFileService mediaFileService) {
this.mediaFileService = mediaFileService;
}
@Override
public void configure() throws Exception {
from("jms:queue:add_link_file")
.routeId("read-queue-add-link_file")
.log("${body}")
.to("bean:addLinkService?method=fromQueue");
.process(new AddLinkProcessor(mediaFileService))
.to("jms:queue:update_title");
}
}
@@ -0,0 +1,45 @@
package de.thpeetz.kontor.integration.services;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.thpeetz.kontor.media.data.MediaFile;
import de.thpeetz.kontor.media.services.MediaFileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.HashMap;
import java.util.Map;
@Slf4j
public class AddLinkProcessor implements Processor {
private final MediaFileService mediaFileService;
public AddLinkProcessor(MediaFileService mediaFileService) {
this.mediaFileService = mediaFileService;
}
@Override
public void process(Exchange exchange) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
HashMap<String,String> myMap = objectMapper.readValue(exchange.getIn().getBody().toString(), new TypeReference<HashMap<String,String>>() {});
String url = myMap.get("url");
log.info("found url: {}", url);
MediaFile mediaFile = mediaFileService.findAllMediaFilesByUrl(url);
if (mediaFile == null) {
log.info("URL not found, create MediaFile");
mediaFile = new MediaFile();
mediaFile.setUrl(url);
mediaFile.setReview(true);
mediaFile.setShouldDownload(true);
MediaFile mediaFileResult = mediaFileService.saveMediaFile(mediaFile);
log.info("created MediaFile with {}", mediaFileResult.getId());
exchange.getMessage().getHeaders().put("mediafile_id", mediaFileResult.getId());
}
log.info("found MediaFile: {}", mediaFile);
Map<String, Object> map = exchange.getMessage().getHeaders();
log.info("Headers: {}", map);
}
}
@@ -19,6 +19,8 @@ public interface MediaFileRepository extends JpaRepository<MediaFile, String> {
List<MediaFile> findByReviewAndShouldDownload(Boolean review, Boolean shouldDownload);
MediaFile findByUrl(String url);
@Query("select m from MediaFile m " +
"where lower(m.url) like lower(concat('%', :searchTerm, '%')) or lower(m.title) like lower(concat('%', :searchTerm, '%')) " +
"AND m.review=:review AND m.shouldDownload=:download")
@@ -39,6 +39,10 @@ public class MediaFileService {
return results;
}
public MediaFile findAllMediaFilesByUrl(String url) {
return mediaFileRepository.findByUrl(url);
}
public List<MediaFile> findAllMediaFiles(SearchFilter searchFilter) {
if (searchFilter == null) {
return mediaFileRepository.findAll();
@@ -75,12 +79,12 @@ public class MediaFileService {
return mediaFileRepository.findAll();
}
public void saveMediaFile(MediaFile mediaFile) {
public MediaFile saveMediaFile(MediaFile mediaFile) {
if (mediaFile == null) {
log.warn("MediaFile is null. Are you sure you have connected your form to the application?");
return;
return null;
}
mediaFileRepository.save(mediaFile);
return mediaFileRepository.save(mediaFile);
}
public void deleteMediaFile(MediaFile mediaFile) {