content: /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you 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
   *
   *    https://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 org.apache.bigtop.manager.server.controller;
  
  import org.apache.bigtop.manager.common.utils.ProjectPathUtils;
  import org.apache.bigtop.manager.server.enums.ApiExceptionEnum;
  import org.apache.bigtop.manager.server.exception.ApiException;
  import org.apache.bigtop.manager.server.utils.ResponseEntity;
  
  import org.springframework.web.bind.annotation.PostMapping;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestParam;
  import org.springframework.web.bind.annotation.RestController;
  import org.springframework.web.multipart.MultipartFile;
  
  import io.swagger.v3.oas.annotations.Operation;
  import io.swagger.v3.oas.annotations.tags.Tag;
  import lombok.extern.slf4j.Slf4j;
  
  import java.nio.file.Files;
  import java.nio.file.Path;
  import java.nio.file.Paths;
  import java.nio.file.StandardCopyOption;
  import java.time.LocalDateTime;
  import java.time.format.DateTimeFormatter;
  
  @Slf4j
  @Tag(name = "File Controller")
  @RestController
  @RequestMapping("/files")
  public class FileController {
  
      @Operation(summary = "upload key file", description = "Upload key file")
      @PostMapping("/upload-key")
      public ResponseEntity<String> uploadKeyFile(@RequestParam("file") MultipartFile file) {
          try {
              // Define the directory where the file will be saved
              String uploadDir = ProjectPathUtils.getKeyStorePath();
              Path uploadPath = Paths.get(uploadDir);
  
              // Create the directory if it does not exist
              if (!Files.exists(uploadPath)) {
                  Files.createDirectories(uploadPath);
              }
  
              // Generate a unique filename
              LocalDateTime now = LocalDateTime.now();
              DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
              String filename = now.format(formatter) + ".key";
  
              // Save the file to the desired location
              Path filePath = uploadPath.resolve(filename);
              Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
  
              return ResponseEntity.success(filename);
          } catch (Exception e) {
              log.error("Upload key file failed: ", e);
              throw new ApiException(ApiExceptionEnum.FILE_UPLOAD_FAILED);
          }
      }
  }
  