package com.railway.system.service.impl;

import com.railway.common.config.ServerConfig;
import com.railway.common.constant.Constants;
import com.railway.common.utils.file.FileUploadUtils;
import com.railway.system.service.ISysFileService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * 本地文件存储
 *
 * @author railway
 */
@Primary
@Service
public class LocalSysFileServiceImpl extends AbstractFileService implements ISysFileService {

  /**
   * 上传文件存储在本地的根路径
   */
  @Value("${railway.filePath}")
  private String localFilePath;

  private final ServerConfig serverConfig;

  public LocalSysFileServiceImpl(ServerConfig serverConfig) {
    this.serverConfig = serverConfig;
  }

  /**
   * 本地文件上传接口
   *
   * @param file 上传的文件
   * @return 访问地址
   * @throws Exception 文件上传异常
   */
  @Override
  public String uploadFile(MultipartFile file) throws Exception {
    String name = FileUploadUtils.upload(localFilePath, file);
    return serverConfig.getUrl() + Constants.RESOURCE_PREFIX + name;
  }
}
