mirror of
https://github.com/zfile-dev/zfile.git
synced 2025-04-19 05:34:52 +00:00
109 lines
3.3 KiB
Java
109 lines
3.3 KiB
Java
package im.zhaojun.local.service;
|
|
|
|
import im.zhaojun.common.model.StorageConfig;
|
|
import im.zhaojun.common.model.SystemConfig;
|
|
import im.zhaojun.common.model.constant.SystemConfigConstant;
|
|
import im.zhaojun.common.model.dto.FileItemDTO;
|
|
import im.zhaojun.common.model.enums.FileTypeEnum;
|
|
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
|
import im.zhaojun.common.repository.SystemConfigRepository;
|
|
import im.zhaojun.common.service.FileService;
|
|
import im.zhaojun.common.service.StorageConfigService;
|
|
import im.zhaojun.common.util.StringUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import javax.annotation.Resource;
|
|
import java.io.File;
|
|
import java.net.Inet4Address;
|
|
import java.net.InetAddress;
|
|
import java.util.ArrayList;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
@Service
|
|
public class LocalServiceImpl implements FileService {
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(LocalServiceImpl.class);
|
|
|
|
private static final String FILE_PATH_KEY = "filePath";
|
|
|
|
@Resource
|
|
private StorageConfigService storageConfigService;
|
|
|
|
@Resource
|
|
private SystemConfigRepository systemConfigRepository;
|
|
|
|
private String filePath;
|
|
|
|
private boolean isInitialized;
|
|
|
|
@Override
|
|
public void init() {
|
|
try {
|
|
Map<String, StorageConfig> stringStorageConfigMap =
|
|
storageConfigService.selectStorageConfigMapByKey(StorageTypeEnum.LOCAL);
|
|
filePath = stringStorageConfigMap.get(FILE_PATH_KEY).getValue();
|
|
isInitialized = true;
|
|
} catch (Exception e) {
|
|
log.debug(StorageTypeEnum.LOCAL.getDescription() + "初始化异常, 已跳过");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public List<FileItemDTO> fileList(String path) throws Exception {
|
|
List<FileItemDTO> fileItemList = new ArrayList<>();
|
|
|
|
String fullPath = StringUtils.concatPath(filePath, path);
|
|
|
|
File file = new File(fullPath);
|
|
File[] files = file.listFiles();
|
|
|
|
if (files == null) {
|
|
return fileItemList;
|
|
}
|
|
for (File f : files) {
|
|
FileItemDTO fileItemDTO = new FileItemDTO();
|
|
fileItemDTO.setType(f.isDirectory() ? FileTypeEnum.FOLDER : FileTypeEnum.FILE);
|
|
fileItemDTO.setTime(new Date(f.lastModified()));
|
|
fileItemDTO.setSize(f.length());
|
|
fileItemDTO.setName(f.getName());
|
|
fileItemDTO.setPath(path);
|
|
if (f.isFile()) {
|
|
fileItemDTO.setUrl(getDownloadUrl(StringUtils.concatUrl(path, f.getName())));
|
|
}
|
|
fileItemList.add(fileItemDTO);
|
|
}
|
|
|
|
return fileItemList;
|
|
}
|
|
|
|
@Override
|
|
public String getDownloadUrl(String path) throws Exception {
|
|
SystemConfig usernameConfig = systemConfigRepository.findByKey(SystemConfigConstant.DOMAIN);
|
|
return StringUtils.removeDuplicateSeparator(usernameConfig.getValue() + "/file/" + path);
|
|
}
|
|
|
|
public String getFilePath() {
|
|
return filePath;
|
|
}
|
|
|
|
public void setFilePath(String filePath) {
|
|
this.filePath = filePath;
|
|
}
|
|
|
|
@Override
|
|
public StorageTypeEnum getStorageTypeEnum() {
|
|
return StorageTypeEnum.LOCAL;
|
|
}
|
|
|
|
|
|
@Override
|
|
public boolean getIsInitialized() {
|
|
return isInitialized;
|
|
}
|
|
|
|
} |