package org.innovateuk.ifs.api.filestorage.util;

import com.google.common.io.ByteStreams;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.innovateuk.ifs.api.filestorage.v1.upload.FileUploadRequest;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;

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

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class FileUploadRequestBuilder {

    public static final String DEFAULT_SYSTEM_ID = "IFS";

    public static FileUploadRequest.FileUploadRequestBuilder fromResource(UUID uuid, Resource resource, MediaType mediaType, String userId) throws IOException {
        byte[] payload = ByteStreams.toByteArray(resource.getInputStream());
        //noinspection UnstableApiUsage
        return FileUploadRequest.builder()
                .fileId(uuid.toString())
                .fileName(resource.getFilename() == null ? uuid.toString() : resource.getFilename())
                .md5Checksum(FileHashing.fileHash64(payload))
                .mimeType(mediaType.toString())
                .payload(payload)
                .userId(userId)
                .fileSizeBytes(payload.length)
                .systemId(DEFAULT_SYSTEM_ID);
    }

    public static FileUploadRequest.FileUploadRequestBuilder fromResource(Resource resource, MediaType mediaType, String userId) throws IOException {
        return fromResource(UUID.randomUUID(), resource, mediaType, userId);
    }

    public static FileUploadRequest.FileUploadRequestBuilder fromResource(byte[] payload, String userId) throws IOException {
        Resource payloadResource = new ByteArrayResource(payload);
        return fromResource(UUID.randomUUID(), payloadResource,
                MediaTypeFactory.getMediaType(payloadResource).orElse(MediaType.APPLICATION_JSON), userId);
    }

}

@@ -144,16 +144,56 @@
 ructor;%0A
+import org.innovateuk.ifs.IfsConstants;%0A
 import o
@@ -594,67 +594,8 @@
  %7B%0A%0A
-    public static final String DEFAULT_SYSTEM_ID = %22IFS%22;%0A%0A
     
@@ -1306,23 +1306,32 @@
 mId(
-DEFAULT
+IfsConstants.IFS
 _SYSTEM_
 ID);
@@ -1326,18 +1326,20 @@
 _SYSTEM_
-ID
+USER
 );%0A    %7D

use constant as system user id