com.sippnex.fileblade.controllers.BrowseController Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fileblade-server Show documentation
Show all versions of fileblade-server Show documentation
Server-side Spring library for fileblade. A web filemanager
package com.sippnex.fileblade.controllers;
import com.sippnex.fileblade.dtos.DirectoryDto;
import com.sippnex.fileblade.entities.Directory;
import com.sippnex.fileblade.entities.FilebladeItem;
import com.sippnex.fileblade.services.FilebladeItemService;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("fileblade/browse")
public class BrowseController {
final FilebladeItemService filebladeItemService;
public BrowseController(FilebladeItemService filebladeItemService) {
this.filebladeItemService = filebladeItemService;
}
@PostMapping("create-directory")
public void createDirectory(@RequestBody DirectoryDto directoryDto) {
Directory directory = new Directory();
directory.setName(directoryDto.getName());
if(directoryDto.getParentDirectoryId() != null && directoryDto.getParentDirectoryId() > 0) {
Optional optionalParentDirectory = filebladeItemService.findById(directoryDto.getParentDirectoryId());
if(!optionalParentDirectory.isPresent() && optionalParentDirectory.get() instanceof Directory) {
System.out.println("Directory-Creation failed: parent-directory with id " + directoryDto.getParentDirectoryId() + " not found");
return;
}
directory.setParentDirectory((Directory)optionalParentDirectory.get());
}
filebladeItemService.save(directory);
}
@GetMapping("show-directory")
public DirectoryDto showDirectory(@RequestParam Optional directoryId, HttpServletRequest request) {
if (directoryId.isPresent() && directoryId.get() > 0) {
Optional optionalDirectory = filebladeItemService.findById(directoryId.get());
if(optionalDirectory.isPresent() && optionalDirectory.get() instanceof Directory) {
return DirectoryDto.parse((Directory)optionalDirectory.get(), "http://" + request.getServerName() + ":" + request.getServerPort());
} else {
// TODO: handle not found error
return null;
}
} else {
Directory rootDirectory = new Directory("");
List rootItems = filebladeItemService.findRootItems();
rootDirectory.setItems(rootItems);
return DirectoryDto.parse(rootDirectory, "http://" + request.getServerName() + ":" + request.getServerPort());
}
}
@GetMapping("move-item")
public void moveFile(@RequestParam Long itemId, @RequestParam Long destinationParentDirectoryId) {
// Try to get item entity
Optional optionalItem = filebladeItemService.findById(itemId);
if(!optionalItem.isPresent()) {
System.out.println("Item-Move failed: item with id " + itemId + " not found");
return;
}
FilebladeItem item = optionalItem.get();
if(destinationParentDirectoryId > 0) {
// Try to get destination parent directory entity
Optional optionalDestinationParentDirectory = filebladeItemService.findById(destinationParentDirectoryId);
if(!optionalDestinationParentDirectory.isPresent() || !(optionalDestinationParentDirectory.get() instanceof Directory)) {
System.out.println("File-Move failed: destination-parent-directory with id " + destinationParentDirectoryId + " not found");
return;
}
Directory destinationParentDirectory = (Directory)optionalDestinationParentDirectory.get();
// Move file to the destination parent directory
item.setParentDirectory(destinationParentDirectory);
} else {
// Move file to root directory (set parent null)
item.setParentDirectory(null);
}
filebladeItemService.save(item);
}
@GetMapping("rename-item")
public void renameDirectory(@RequestParam Long itemId, @RequestParam String name) {
// Try to get item entity
Optional optionalItem = filebladeItemService.findById(itemId);
if(!optionalItem.isPresent()) {
System.out.println("Item-Rename failed: item with id " + itemId + " not found");
return;
}
FilebladeItem item = optionalItem.get();
// Rename item
item.setName(name);
filebladeItemService.save(item);
}
@GetMapping("delete-item")
public void deleteDirectory(@RequestParam Long itemId) {
// Delete item
filebladeItemService.deleteById(itemId);
}
}