package org.innovateuk.ifs.filestorage.messaging;

import org.innovateuk.ifs.IfsConstants;
import org.innovateuk.ifs.api.filestorage.util.FileHashing;
import org.innovateuk.ifs.api.filestorage.v1.upload.FileUploadRequest;
import org.innovateuk.ifs.filestorage.storage.StorageService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;

import java.io.IOException;
import java.util.UUID;

import static org.innovateuk.ifs.starters.messaging.cfg.CommonQueues.FILE_UPLOAD_SERVICE_UPLOAD;

public class UploadMessageListener {

    @Autowired
    private StorageService storageService;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Scheduled(initialDelay = 1000L, fixedDelay = 20L)
    public void testinit() throws IOException {
        Resource resource = new ClassPathResource("test.jpg");
        byte[] payload = resource.getInputStream().readAllBytes();
        FileUploadRequest fileUploadRequest =  FileUploadRequest.builder()
                .fileId(UUID.randomUUID().toString())
                .fileName("test.jpg")
                .systemId(IfsConstants.IFS_SYSTEM_USER)
                .userId(IfsConstants.IFS_SYSTEM_USER)
                .mimeType(MediaType.IMAGE_JPEG_VALUE)
                .fileSizeBytes(payload.length)
                .md5Checksum(FileHashing.fileHash64(payload))
                .payload(payload).build();
        amqpTemplate.convertAndSend(FILE_UPLOAD_SERVICE_UPLOAD, fileUploadRequest);
    }

    /**
     * Listen the upload queue. The caller will have a UUID to reference to the stored result at a later date.
     * @param fileUploadRequest the file wrapped request to store.
     */
    @RabbitListener(queues = {FILE_UPLOAD_SERVICE_UPLOAD})
    public void fileUpload(FileUploadRequest fileUploadRequest) {
        storageService.fileUpload(fileUploadRequest);
    }

}

@@ -48,108 +48,8 @@
 g;%0A%0A
-import org.innovateuk.ifs.IfsConstants;%0Aimport org.innovateuk.ifs.api.filestorage.util.FileHashing;%0A
 impo
@@ -181,59 +181,8 @@
 ce;%0A
-import org.springframework.amqp.core.AmqpTemplate;%0A
 impo
@@ -310,1318 +310,202 @@
 ed;%0A
-import org.springframework.core.io.ClassPathResource;%0Aimport org.springframework.core.io.Resource;%0Aimport org.springframework.http.MediaType;%0Aimport org.springframework.scheduling.annotation.Scheduled;%0A%0Aimport java.io.IOException;%0Aimport java.util.UUID;%0A%0Aimport static org.innovateuk.ifs.starters.messaging.cfg.CommonQueues.FILE_UPLOAD_SERVICE_UPLOAD;%0A%0Apublic class UploadMessageListener %7B%0A%0A    @Autowired%0A    private StorageService storageService;%0A%0A    @Autowired%0A    private AmqpTemplate amqpTemplate;%0A%0A    @Scheduled(initialDelay = 1000L, fixedDelay = 20L)%0A    public void testinit() throws IOException %7B%0A        Resource resource = new ClassPathResource(%22test.jpg%22);%0A        byte%5B%5D payload = resource.getInputStream().readAllBytes();%0A        FileUploadRequest fileUploadRequest =  FileUploadRequest.builder()%0A                .fileId(UUID.randomUUID().toString())%0A                .fileName(%22test.jpg%22)%0A                .systemId(IfsConstants.IFS_SYSTEM_USER)%0A                .userId(IfsConstants.IFS_SYSTEM_USER)%0A                .mimeType(MediaType.IMAGE_JPEG_VALUE)%0A                .fileSizeBytes(payload.length)%0A                .md5Checksum(FileHashing.fileHash64(payload))%0A                .payload(payload).build();%0A        amqpTemplate.convertAndSend(FILE_UPLOAD_SERVICE_UPLOAD, fileUploadRequest);%0A    %7D
+%0Aimport static org.innovateuk.ifs.starters.messaging.cfg.CommonQueues.FILE_UPLOAD_SERVICE_UPLOAD;%0A%0Apublic class UploadMessageListener %7B%0A%0A    @Autowired%0A    private StorageService storageService;
 %0A%0A  

remove test code