Skip to content

Commit

Permalink
feat: add file rename endpoint and folder rename endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
AkagiYui committed May 27, 2024
1 parent 4715b00 commit adca15b
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.akagiyui.drive.model.response.UserFileResponse
import com.akagiyui.drive.service.*
import jakarta.servlet.http.HttpServletResponse
import jakarta.validation.constraints.Min
import jakarta.validation.constraints.NotBlank
import jakarta.validation.constraints.Size
import org.springframework.beans.factory.DisposableBean
import org.springframework.core.io.InputStreamResource
Expand Down Expand Up @@ -321,6 +322,7 @@ class FileController(
* 移动文件
* @param id 用户文件ID
* @param folderId 目标文件夹ID
* @param user 用户
*/
@PutMapping("/{id}/move")
@RequirePermission
Expand All @@ -331,4 +333,20 @@ class FileController(
) {
userFileService.moveFile(user.id, id, folderId)
}

/**
* 重命名文件
* @param id 用户文件ID
* @param name 新文件名
* @param user 用户
*/
@PutMapping("/{id}/name")
@RequirePermission
fun renameFile(
@PathVariable id: String,
@RequestParam("name") @Validated @NotBlank name: String,
@CurrentUser user: User,
) {
userFileService.rename(user.id, id, name)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,21 @@ class FolderController(private val folderService: FolderService) {
) {
folderService.moveFolder(user.id, folderId, parentId)
}

/**
* 重命名文件夹
*
* @param folderId 文件夹ID
* @param name 新文件夹名
* @param user 用户
*/
@PutMapping("/{folderId}/name")
@RequirePermission
fun renameFolder(
@PathVariable folderId: String,
@RequestParam("name") name: String,
@CurrentUser user: User,
) {
folderService.rename(user.id, folderId, name)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ interface FolderRepository : JpaRepository<Folder, String> {
*/
fun findByUserIdAndParentId(userId: String, parentId: String?): List<Folder>

/**
* 根据用户ID和文件夹ID获取文件夹
*
* @param userId 用户ID
* @param id 文件夹ID
* @return 文件夹
*/
fun findByUserIdAndId(userId: String, id: String): Folder?

/**
* 根据文件夹名、用户ID、父文件夹ID判断文件夹是否存在
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,12 @@ interface FolderService {
* 移动文件夹
*/
fun moveFolder(userId: String, folderId: String, parentId: String?)

/**
* 重命名文件夹
* @param userId 用户ID
* @param folderId 文件夹ID
* @param newName 新文件夹名
*/
fun rename(userId: String, folderId: String, newName: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,9 @@ interface UserFileService {
* 秒传文件
*/
fun mirrorFile(user: User, request: MirrorFileRequest): UserFile

/**
* 重命名文件
*/
fun rename(userId: String, userFileId: String, newName: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ class FolderServiceImpl @Autowired constructor(
log.debug("delete folder: $folderId")
}

override fun rename(userId: String, folderId: String, newName: String) {
val folder = folderRepository.findByUserIdAndId(userId, folderId)
?: throw CustomException(ResponseEnum.NOT_FOUND)
folder.name = newName
folderRepository.save(folder)
}

override fun moveFolder(userId: String, folderId: String, parentId: String?) {
val folder = folderRepository.findById(folderId).orElseThrow { CustomException(ResponseEnum.NOT_FOUND) }
if (folder.user.id != userId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ class UserFileServiceImpl(
return userFileRepository.save(userFile)
}

override fun rename(userId: String, userFileId: String, newName: String) {
val userFile = getUserFileById(userId, userFileId)
userFile.name = newName
userFileRepository.save(userFile)
}

override fun getFiles(userId: String, folderId: String?): List<UserFile> {
return userFileRepository.findByUserIdAndFolderId(userId, folderId)
}
Expand Down

0 comments on commit adca15b

Please sign in to comment.