/*
 * Copyright 2020 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.netflix.spinnaker.front50.controllers;

import static java.lang.String.format;

import com.google.common.hash.Hashing;
import com.google.common.io.ByteStreams;
import com.netflix.spinnaker.front50.plugins.PluginBinaryStorageService;
import com.netflix.spinnaker.kork.exceptions.SystemException;
import java.io.InputStream;
import java.util.Optional;
import lombok.SneakyThrows;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/pluginBinaries")
public class PluginBinaryController {

  private final Optional<PluginBinaryStorageService> pluginBinaryStorageService;

  public PluginBinaryController(Optional<PluginBinaryStorageService> pluginBinaryStorageService) {
    this.pluginBinaryStorageService = pluginBinaryStorageService;
  }

  @SneakyThrows
  @PostMapping("/{id}/{version}")
  @ResponseStatus(HttpStatus.CREATED)
  void upload(
      @PathVariable String id,
      @PathVariable String version,
      @RequestParam("sha512sum") String sha512sum,
      InputStream body) {
    byte[] bytes = ByteStreams.toByteArray(body);
    verifyChecksum(bytes, sha512sum);
    storageService().store(getKey(id, version), bytes);
  }

  @GetMapping("/{id}/{version}")
  ResponseEntity<byte[]> getBinary(@PathVariable String id, @PathVariable String version) {
    return ResponseEntity.ok()
        .header("Content-Type", "application/octet-stream")
        .body(storageService().load(getKey(id, version)));
  }

  private static String getKey(String id, String version) {
    return format("%s/%s.zip", id, version);
  }

  private void verifyChecksum(byte[] body, String sha512sum) {
    String sha = Hashing.sha512().hashBytes(body).toString();
    if (!sha.equals(sha512sum)) {
      throw new SystemException("Plugin binary checksum does not match expected checksum value")
          .setRetryable(true);
    }
  }

  private PluginBinaryStorageService storageService() {
    return pluginBinaryStorageService.orElseThrow(
        () ->
            new IllegalArgumentException(
                "Plugin binary storage service is yet not available for your persistence backend"));
  }
}

@@ -723,49 +723,8 @@
 ng;%0A
-import com.google.common.io.ByteStreams;%0A
 impo
@@ -858,36 +858,8 @@
 on;%0A
-import java.io.InputStream;%0A
 impo
@@ -1050,16 +1050,72 @@
 ation.*;
+%0Aimport org.springframework.web.multipart.MultipartFile;
 %0A%0A@RestC
@@ -1681,75 +1681,87 @@
     
-InputStream body) %7B%0A    byte%5B%5D bytes = ByteStreams.toByteArray(body
+@RequestParam(%22plugin%22) MultipartFile body) %7B%0A    byte%5B%5D bytes = body.getBytes(
 );%0A 

Move back to multipart uploads (#724)