content: /*
   * Copyright 2021-2022 Alibaba Group Holding Ltd.
   *
   *    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.aliyun.fastmodel.ide.open.service.impl;
  
  import java.io.IOException;
  import java.net.MalformedURLException;
  import java.nio.file.Files;
  import java.nio.file.Path;
  import java.nio.file.Paths;
  import java.nio.file.StandardCopyOption;
  import java.util.stream.Stream;
  
  import javax.annotation.PostConstruct;
  
  import com.aliyun.fastmodel.ide.open.service.StorageService;
  import org.springframework.core.io.Resource;
  import org.springframework.core.io.UrlResource;
  import org.springframework.stereotype.Component;
  import org.springframework.util.FileSystemUtils;
  import org.springframework.web.multipart.MultipartFile;
  
  /**
   * 文件存储服务
   *
   * @author panguanjing
   * @date 2021/12/22
   */
  @Component
  public class FileStorageServiceImpl implements StorageService {
      private final Path root = Paths.get(System.getProperty("java.io.tmpdir"));
  
      @Override
      @PostConstruct
      public void init() {
          try {
              if (root.toFile().exists()) {
                  return;
              }
              Files.createDirectory(root);
          } catch (IOException e) {
              throw new RuntimeException("Could not initialize folder for upload!", e);
          }
      }
  
      @Override
      public Resource save(MultipartFile file) {
          try {
              Path resolve = root.resolve(file.getOriginalFilename());
              Files.copy(file.getInputStream(), resolve, StandardCopyOption.REPLACE_EXISTING);
              Resource resource = new UrlResource(resolve.toUri());
              return resource;
          } catch (Exception e) {
              throw new RuntimeException("Could not store the file. Error: " + file.getOriginalFilename(), e);
          }
      }
  
      @Override
      public Resource load(String filename) {
          try {
              Path file = root.resolve(filename);
              Resource resource = new UrlResource(file.toUri());
  
              if (resource.exists() || resource.isReadable()) {
                  return resource;
              } else {
                  throw new RuntimeException("Could not read the file!");
              }
          } catch (MalformedURLException e) {
              throw new RuntimeException("Error: " + e.getMessage());
          }
      }
  
      @Override
      public void deleteAll() {
          FileSystemUtils.deleteRecursively(root.toFile());
      }
  
      @Override
      public Stream<Path> loadAll() {
          try {
              return Files.walk(root, 1).filter(path -> !path.equals(root)).map(root::relativize);
          } catch (IOException e) {
              throw new RuntimeException("Could not load the files!");
          }
      }
  }
  