@@ -6,17 +6,14 @@ package models
6
6
7
7
import (
8
8
"fmt"
9
- "io"
10
9
"io/ioutil"
11
- "mime/multipart"
12
10
"os"
13
11
"os/exec"
14
12
"path"
15
13
"path/filepath"
16
14
"time"
17
15
18
16
"github.com/Unknwon/com"
19
- gouuid "github.com/satori/go.uuid"
20
17
21
18
"code.gitea.io/git"
22
19
@@ -325,143 +322,6 @@ func (repo *Repository) DeleteRepoFile(doer *User, opts DeleteRepoFileOptions) (
325
322
return event , nil
326
323
}
327
324
328
- // ____ ___ .__ .___ ___________.___.__
329
- // | | \______ | | _________ __| _/ \_ _____/| | | ____ ______
330
- // | | /\____ \| | / _ \__ \ / __ | | __) | | | _/ __ \ / ___/
331
- // | | / | |_> > |_( <_> ) __ \_/ /_/ | | \ | | |_\ ___/ \___ \
332
- // |______/ | __/|____/\____(____ /\____ | \___ / |___|____/\___ >____ >
333
- // |__| \/ \/ \/ \/ \/
334
- //
335
-
336
- // Upload represent a uploaded file to a repo to be deleted when moved
337
- type Upload struct {
338
- ID int64 `xorm:"pk autoincr"`
339
- UUID string `xorm:"uuid UNIQUE"`
340
- Name string
341
- }
342
-
343
- // UploadLocalPath returns where uploads is stored in local file system based on given UUID.
344
- func UploadLocalPath (uuid string ) string {
345
- return path .Join (setting .Repository .Upload .TempPath , uuid [0 :1 ], uuid [1 :2 ], uuid )
346
- }
347
-
348
- // LocalPath returns where uploads are temporarily stored in local file system.
349
- func (upload * Upload ) LocalPath () string {
350
- return UploadLocalPath (upload .UUID )
351
- }
352
-
353
- // NewUpload creates a new upload object.
354
- func NewUpload (name string , buf []byte , file multipart.File ) (_ * Upload , err error ) {
355
- upload := & Upload {
356
- UUID : gouuid .NewV4 ().String (),
357
- Name : name ,
358
- }
359
-
360
- localPath := upload .LocalPath ()
361
- if err = os .MkdirAll (path .Dir (localPath ), os .ModePerm ); err != nil {
362
- return nil , fmt .Errorf ("MkdirAll: %v" , err )
363
- }
364
-
365
- fw , err := os .Create (localPath )
366
- if err != nil {
367
- return nil , fmt .Errorf ("Create: %v" , err )
368
- }
369
- defer fw .Close ()
370
-
371
- if _ , err = fw .Write (buf ); err != nil {
372
- return nil , fmt .Errorf ("Write: %v" , err )
373
- } else if _ , err = io .Copy (fw , file ); err != nil {
374
- return nil , fmt .Errorf ("Copy: %v" , err )
375
- }
376
-
377
- if _ , err := x .Insert (upload ); err != nil {
378
- return nil , err
379
- }
380
-
381
- return upload , nil
382
- }
383
-
384
- // GetUploadByUUID returns the Upload by UUID
385
- func GetUploadByUUID (uuid string ) (* Upload , error ) {
386
- upload := & Upload {UUID : uuid }
387
- has , err := x .Get (upload )
388
- if err != nil {
389
- return nil , err
390
- } else if ! has {
391
- return nil , ErrUploadNotExist {0 , uuid }
392
- }
393
- return upload , nil
394
- }
395
-
396
- // GetUploadsByUUIDs returns multiple uploads by UUIDS
397
- func GetUploadsByUUIDs (uuids []string ) ([]* Upload , error ) {
398
- if len (uuids ) == 0 {
399
- return []* Upload {}, nil
400
- }
401
-
402
- // Silently drop invalid uuids.
403
- uploads := make ([]* Upload , 0 , len (uuids ))
404
- return uploads , x .In ("uuid" , uuids ).Find (& uploads )
405
- }
406
-
407
- // DeleteUploads deletes multiple uploads
408
- func DeleteUploads (uploads ... * Upload ) (err error ) {
409
- if len (uploads ) == 0 {
410
- return nil
411
- }
412
-
413
- sess := x .NewSession ()
414
- defer sess .Close ()
415
- if err = sess .Begin (); err != nil {
416
- return err
417
- }
418
-
419
- ids := make ([]int64 , len (uploads ))
420
- for i := 0 ; i < len (uploads ); i ++ {
421
- ids [i ] = uploads [i ].ID
422
- }
423
- if _ , err = sess .
424
- In ("id" , ids ).
425
- Delete (new (Upload )); err != nil {
426
- return fmt .Errorf ("delete uploads: %v" , err )
427
- }
428
-
429
- for _ , upload := range uploads {
430
- localPath := upload .LocalPath ()
431
- if ! com .IsFile (localPath ) {
432
- continue
433
- }
434
-
435
- if err := os .Remove (localPath ); err != nil {
436
- return fmt .Errorf ("remove upload: %v" , err )
437
- }
438
- }
439
-
440
- return sess .Commit ()
441
- }
442
-
443
- // DeleteUpload delete a upload
444
- func DeleteUpload (u * Upload ) error {
445
- return DeleteUploads (u )
446
- }
447
-
448
- // DeleteUploadByUUID deletes a upload by UUID
449
- func DeleteUploadByUUID (uuid string ) error {
450
- upload , err := GetUploadByUUID (uuid )
451
- if err != nil {
452
- if IsErrUploadNotExist (err ) {
453
- return nil
454
- }
455
- return fmt .Errorf ("GetUploadByUUID: %v" , err )
456
- }
457
-
458
- if err := DeleteUpload (upload ); err != nil {
459
- return fmt .Errorf ("DeleteUpload: %v" , err )
460
- }
461
-
462
- return nil
463
- }
464
-
465
325
// UploadRepoFileOptions contains the uploaded repository file options
466
326
type UploadRepoFileOptions struct {
467
327
LastCommitID string
0 commit comments