Skip to content

Commit

Permalink
删除某版本下某文件 link #36
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonczc committed Nov 3, 2021
1 parent e75e43d commit fc861b3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
33 changes: 33 additions & 0 deletions server/src/controller/PluginsController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,39 @@ class PluginsController(
r.created()
}

@ApiOperation("删除一个文件")
@DeleteMapping("/{id}/{version}/{filename}")
@ApiResponses(
ApiResponse(code = 404, message = "Plugin not found", response = ApiResp::class),
ApiResponse(code = 403, message = "Plugin is not owned by you", response = ApiResp::class),
ApiResponse(code = 201, message = "Uploaded", response = ApiResp::class),
)
fun (@receiver:ApiIgnore ServerWebExchange).delFile(
@ApiParam("插件 ID", example = PluginDesc.ID_EXAMPLE)
@PathVariable
id: String,

@ApiParam("插件版本号")
@PathVariable
version: String,

@ApiParam("文件名")
@PathVariable
filename: String,
): Mono<ApiResp<Void?>> = mono {
val user = loginUserOrReject
val plugin = desc.get(id) ?: return@mono r.notFound(null)

plugin.checkOwnedBy(user)
plugin.checkAvailable()

if (!storage.hasVersion(plugin.pluginId, version)) return@mono r.notFound(message = "Version not found")
val file = storage.get(plugin.pluginId, version, filename)
if (!file.exists()) return@mono r.conflict(null, message = "File doesn't exist")
storage.delete(id, version, filename)
r.ok()
}

@ApiOperation("创建版本")
@PutMapping("/{id}/{version}")
@ApiResponses(
Expand Down
6 changes: 6 additions & 0 deletions server/src/services/PluginStorageService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ class PluginStorageService {
return dir.deleteRecursively()
}

fun delete(pid: String, version: String, fileName:String): Boolean {
val file = resolveFile(pid, version,fileName)
if (!file.exists()) return false
return file.delete()
}

private fun InputStream.copyToBuffered(out: OutputStream, buffer: ByteArray): Long {
var bytesCopied: Long = 0
var bytes = read(buffer)
Expand Down

0 comments on commit fc861b3

Please sign in to comment.