mirror of https://github.com/halo-dev/halo
feat: completed static storage api.
parent
0d622d35d1
commit
2b75762b76
|
@ -1,16 +1,18 @@
|
||||||
package run.halo.app.controller.admin.api;
|
package run.halo.app.controller.admin.api;
|
||||||
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import run.halo.app.model.support.StaticFile;
|
import run.halo.app.model.support.StaticFile;
|
||||||
import run.halo.app.service.StaticStorageService;
|
import run.halo.app.service.StaticStorageService;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author ryan0up
|
* Static storage controller.
|
||||||
* @date 2019/12/6
|
*
|
||||||
|
* @author ryanwang
|
||||||
|
* @date 2019-12-06
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/admin/statics")
|
@RequestMapping("/api/admin/statics")
|
||||||
|
@ -23,7 +25,28 @@ public class StaticStorageController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
|
@ApiOperation("List static files.")
|
||||||
public List<StaticFile> list() {
|
public List<StaticFile> list() {
|
||||||
return staticStorageService.listStaticFolder();
|
return staticStorageService.listStaticFolder();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@DeleteMapping
|
||||||
|
@ApiOperation("Delete file by relative path")
|
||||||
|
public void deletePermanently(@RequestParam("path") String path) {
|
||||||
|
staticStorageService.delete(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
@ApiOperation("Create folder")
|
||||||
|
public void createFolder(String basePath,
|
||||||
|
@RequestParam("folderName") String folderName) {
|
||||||
|
staticStorageService.createFolder(basePath, folderName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("upload")
|
||||||
|
@ApiOperation("Upload static file")
|
||||||
|
public void upload(String basePath,
|
||||||
|
@RequestPart("file") MultipartFile file) {
|
||||||
|
staticStorageService.update(basePath, file);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package run.halo.app.service;
|
package run.halo.app.service;
|
||||||
|
|
||||||
|
import org.springframework.lang.NonNull;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import run.halo.app.model.support.StaticFile;
|
import run.halo.app.model.support.StaticFile;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -23,4 +25,27 @@ public interface StaticStorageService {
|
||||||
* @return List<StaticFile>
|
* @return List<StaticFile>
|
||||||
*/
|
*/
|
||||||
List<StaticFile> listStaticFolder();
|
List<StaticFile> listStaticFolder();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete file or folder by relative path
|
||||||
|
*
|
||||||
|
* @param relativePath relative path
|
||||||
|
*/
|
||||||
|
void delete(@NonNull String relativePath);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create folder.
|
||||||
|
*
|
||||||
|
* @param basePath base path
|
||||||
|
* @param folderName folder name must not be null
|
||||||
|
*/
|
||||||
|
void createFolder(String basePath, @NonNull String folderName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update static file.
|
||||||
|
*
|
||||||
|
* @param basePath base path
|
||||||
|
* @param file file must not be null.
|
||||||
|
*/
|
||||||
|
void update(String basePath, @NonNull MultipartFile file);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,10 +5,13 @@ import org.springframework.lang.NonNull;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import run.halo.app.config.properties.HaloProperties;
|
import run.halo.app.config.properties.HaloProperties;
|
||||||
|
import run.halo.app.exception.FileOperationException;
|
||||||
import run.halo.app.exception.ServiceException;
|
import run.halo.app.exception.ServiceException;
|
||||||
import run.halo.app.model.support.StaticFile;
|
import run.halo.app.model.support.StaticFile;
|
||||||
import run.halo.app.service.StaticStorageService;
|
import run.halo.app.service.StaticStorageService;
|
||||||
|
import run.halo.app.utils.FileUtils;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
@ -38,7 +41,6 @@ public class StaticStorageServiceImpl implements StaticStorageService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<StaticFile> listStaticFolder() {
|
public List<StaticFile> listStaticFolder() {
|
||||||
System.out.println(staticDir);
|
|
||||||
return listStaticFileTree(staticDir);
|
return listStaticFileTree(staticDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,4 +80,68 @@ public class StaticStorageServiceImpl implements StaticStorageService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void delete(String relativePath) {
|
||||||
|
Assert.notNull(relativePath, "Relative path must not be null");
|
||||||
|
|
||||||
|
Path path = Paths.get(staticDir.toString(), relativePath);
|
||||||
|
System.out.println(path.toString());
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (path.toFile().isDirectory()) {
|
||||||
|
FileUtils.deleteFolder(path);
|
||||||
|
} else {
|
||||||
|
Files.deleteIfExists(path);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new FileOperationException("文件 " + relativePath + " 删除失败", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void createFolder(String basePath, String folderName) {
|
||||||
|
Assert.notNull(folderName, "Folder name path must not be null");
|
||||||
|
|
||||||
|
Path path;
|
||||||
|
|
||||||
|
if (StringUtils.isEmpty(basePath)) {
|
||||||
|
path = Paths.get(staticDir.toString(), folderName);
|
||||||
|
} else {
|
||||||
|
path = Paths.get(staticDir.toString(), basePath, folderName);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (path.toFile().exists()) {
|
||||||
|
throw new FileOperationException("目录 " + path.toString() + " 已存在").setErrorData(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
FileUtils.createIfAbsent(path);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new FileOperationException("目录 " + path.toString() + " 创建失败", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update(String basePath, MultipartFile file) {
|
||||||
|
Assert.notNull(file, "Multipart file must not be null");
|
||||||
|
|
||||||
|
Path uploadPath;
|
||||||
|
|
||||||
|
if (StringUtils.isEmpty(basePath)) {
|
||||||
|
uploadPath = Paths.get(staticDir.toString(), file.getOriginalFilename());
|
||||||
|
} else {
|
||||||
|
uploadPath = Paths.get(staticDir.toString(), basePath, file.getOriginalFilename());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (uploadPath.toFile().exists()) {
|
||||||
|
throw new FileOperationException("文件 " + file.getOriginalFilename() + " 已存在").setErrorData(uploadPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Files.createFile(uploadPath);
|
||||||
|
file.transferTo(uploadPath);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new ServiceException("上传文件失败").setErrorData(uploadPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue