|
| 1 | +package edu.harvard.dbmi.avillach.dataupload.upload.lambda; |
| 2 | + |
| 3 | +import edu.harvard.dbmi.avillach.dataupload.hpds.HPDSClient; |
| 4 | +import edu.harvard.dbmi.avillach.dataupload.hpds.hpdsartifactsdonotchange.Query; |
| 5 | +import edu.harvard.dbmi.avillach.dataupload.status.StatusService; |
| 6 | +import org.slf4j.Logger; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.stereotype.Service; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.OutputStream; |
| 13 | +import java.io.RandomAccessFile; |
| 14 | +import java.net.HttpURLConnection; |
| 15 | +import java.net.URL; |
| 16 | +import java.nio.ByteBuffer; |
| 17 | +import java.nio.channels.FileChannel; |
| 18 | +import java.nio.file.Files; |
| 19 | +import java.nio.file.Path; |
| 20 | +import java.util.Optional; |
| 21 | + |
| 22 | +@Service |
| 23 | +public class CumulusUploadService { |
| 24 | + private static final Logger log = LoggerFactory.getLogger(CumulusUploadService.class); |
| 25 | + private final StatusService statusService; |
| 26 | + private final POSTUrlFetcher urlFetcher; |
| 27 | + private final HPDSClient hpdsClient; |
| 28 | + private final Path sharingRoot; |
| 29 | + |
| 30 | + |
| 31 | + @Autowired |
| 32 | + public CumulusUploadService(StatusService statusService, POSTUrlFetcher urlFetcher, HPDSClient hpdsClient, Path sharingRoot) { |
| 33 | + this.statusService = statusService; |
| 34 | + this.urlFetcher = urlFetcher; |
| 35 | + this.hpdsClient = hpdsClient; |
| 36 | + this.sharingRoot = sharingRoot; |
| 37 | + } |
| 38 | + |
| 39 | + public boolean asyncUpload(Query query) { |
| 40 | + log.info("Async upload called"); |
| 41 | + Thread.ofVirtual().start(() -> upload(query)); |
| 42 | + return true; |
| 43 | + } |
| 44 | + private void upload(Query query) { |
| 45 | + log.info("Fetching upload URL"); |
| 46 | + Optional<String> uploadURL = urlFetcher.getPreSignedUploadURL(query.getPicSureId(), "patients.txt"); |
| 47 | + if (uploadURL.isEmpty()) { |
| 48 | + log.error("Could not get upload URL. Exiting"); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + boolean written = hpdsClient.writePatientData(query); |
| 53 | + if (!written) { |
| 54 | + log.warn("HPDS did not write data. Exiting"); |
| 55 | + return; |
| 56 | + } |
| 57 | + log.info("HPDS reported successfully writing {} data for {} to file.", "patients.txt", query.getPicSureId()); |
| 58 | + |
| 59 | + Path data = Path.of(sharingRoot.toString(), query.getPicSureId(), "patients.txt"); |
| 60 | + if (!Files.exists(data)) { |
| 61 | + log.info("HPDS lied; file {} DNE. Status set to error", data); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + log.info("File location verified. Uploading for {} to AWS", query.getPicSureId()); |
| 66 | + try { |
| 67 | + boolean uploaded = uploadFileToPresignedUrl(uploadURL.get(), data); |
| 68 | + } catch (IOException e) { |
| 69 | + log.error("Error uploading data: ", e); |
| 70 | + } |
| 71 | + log.info("Done uploading patients for query {}", query.getPicSureId()); |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + private boolean uploadFileToPresignedUrl(String presignedUrlString, Path filePath) throws IOException { |
| 76 | + |
| 77 | + URL presignedUrl = new URL(presignedUrlString); |
| 78 | + HttpURLConnection connection = (HttpURLConnection) presignedUrl.openConnection(); |
| 79 | + connection.setDoOutput(true); |
| 80 | + connection.setRequestMethod("PUT"); |
| 81 | + OutputStream out = connection.getOutputStream(); |
| 82 | + |
| 83 | + try (RandomAccessFile file = new RandomAccessFile(filePath.toString(), "r"); |
| 84 | + FileChannel inChannel = file.getChannel()) { |
| 85 | + ByteBuffer buffer = ByteBuffer.allocate(8192); //Buffer size is 8k |
| 86 | + |
| 87 | + while (inChannel.read(buffer) > 0) { |
| 88 | + buffer.flip(); |
| 89 | + for (int i = 0; i < buffer.limit(); i++) { |
| 90 | + out.write(buffer.get()); |
| 91 | + } |
| 92 | + buffer.clear(); |
| 93 | + } |
| 94 | + } catch (IOException e) { |
| 95 | + log.error(e.getMessage(), e); |
| 96 | + } |
| 97 | + |
| 98 | + out.close(); |
| 99 | + connection.getResponseCode(); |
| 100 | + log.info("HTTP response code is " + connection.getResponseCode()); |
| 101 | + return HttpURLConnection.HTTP_OK == connection.getResponseCode(); |
| 102 | + } |
| 103 | +} |
0 commit comments