Skip to content

Commit 7ccabf4

Browse files
committed
Merge branch 'chunk_upload' of https://github.com/jenken827/openlist into pr/jenken827/1141
2 parents e68f04e + e365417 commit 7ccabf4

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

server/handles/fsup.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func FsPreup(c *gin.Context) {
237237
common.ErrorResp(c, fmt.Errorf("invalid request body: %w", err), 400)
238238
return
239239
}
240-
240+
241241
// 基本参数验证
242242
if req.Name == "" {
243243
common.ErrorResp(c, fmt.Errorf("file name is required"), 400)
@@ -247,9 +247,15 @@ func FsPreup(c *gin.Context) {
247247
common.ErrorResp(c, fmt.Errorf("file size must be greater than 0"), 400)
248248
return
249249
}
250-
250+
251251
storage := c.Request.Context().Value(conf.StorageKey).(driver.Driver)
252252
path := c.Request.Context().Value(conf.PathKey).(string)
253+
if !req.Overwrite {
254+
if res, _ := fs.Get(c.Request.Context(), path, &fs.GetArgs{NoLog: true}); res != nil {
255+
common.ErrorStrResp(c, "file exists", 403)
256+
return
257+
}
258+
}
253259

254260
res, err := fs.Preup(c.Request.Context(), storage, path, req)
255261
if err != nil {
@@ -261,50 +267,56 @@ func FsPreup(c *gin.Context) {
261267

262268
// FsUpSlice 流式上传分片 - 使用PUT方法进行流式上传,避免表单上传的内存占用
263269
func FsUpSlice(c *gin.Context) {
270+
defer func() {
271+
if n, _ := io.ReadFull(c.Request.Body, []byte{0}); n == 1 {
272+
_, _ = utils.CopyWithBuffer(io.Discard, c.Request.Body)
273+
}
274+
_ = c.Request.Body.Close()
275+
}()
264276
// 从HTTP头获取参数
265277
taskID := c.GetHeader("X-Task-ID")
266278
if taskID == "" {
267279
common.ErrorResp(c, fmt.Errorf("X-Task-ID header is required"), 400)
268280
return
269281
}
270-
282+
271283
sliceNumStr := c.GetHeader("X-Slice-Num")
272284
if sliceNumStr == "" {
273285
common.ErrorResp(c, fmt.Errorf("X-Slice-Num header is required"), 400)
274286
return
275287
}
276-
288+
277289
sliceNum, err := strconv.ParseUint(sliceNumStr, 10, 32)
278290
if err != nil {
279291
common.ErrorResp(c, fmt.Errorf("invalid X-Slice-Num: %w", err), 400)
280292
return
281293
}
282-
294+
283295
sliceHash := c.GetHeader("X-Slice-Hash")
284-
296+
285297
// 构建请求对象
286298
req := &reqres.UploadSliceReq{
287299
TaskID: taskID,
288300
SliceHash: sliceHash,
289301
SliceNum: uint(sliceNum),
290302
}
291-
303+
292304
// 获取请求体作为流
293305
reader := c.Request.Body
294306
if reader == nil {
295307
common.ErrorResp(c, fmt.Errorf("request body is required"), 400)
296308
return
297309
}
298-
310+
299311
storage := c.Request.Context().Value(conf.StorageKey).(driver.Driver)
300-
312+
301313
// 调用流式上传分片函数
302314
err = fs.UploadSlice(c.Request.Context(), storage, req, reader)
303315
if err != nil {
304316
common.ErrorResp(c, fmt.Errorf("upload slice failed: %w", err), 500)
305317
return
306318
}
307-
319+
308320
common.SuccessResp(c)
309321
}
310322

@@ -316,12 +328,12 @@ func FsUpSliceComplete(c *gin.Context) {
316328
common.ErrorResp(c, fmt.Errorf("invalid request body: %w", err), 400)
317329
return
318330
}
319-
331+
320332
if req.TaskID == "" {
321333
common.ErrorResp(c, fmt.Errorf("task_id is required"), 400)
322334
return
323335
}
324-
336+
325337
storage := c.Request.Context().Value(conf.StorageKey).(driver.Driver)
326338
rsp, err := fs.SliceUpComplete(c.Request.Context(), storage, req.TaskID)
327339
if err != nil {

0 commit comments

Comments
 (0)