tableFiledrops.go (1955B)
1 package database 2 3 import ( 4 "dkforest/pkg/config" 5 "dkforest/pkg/utils" 6 ucrypto "dkforest/pkg/utils/crypto" 7 "github.com/google/uuid" 8 "github.com/sirupsen/logrus" 9 "os" 10 "path/filepath" 11 "time" 12 ) 13 14 type Filedrop struct { 15 ID int64 16 UUID string 17 FileName string 18 OrigFileName string 19 FileSize int64 20 IV []byte 21 Password EncryptedString 22 CreatedAt time.Time 23 UpdatedAt *time.Time 24 } 25 26 func (d *DkfDB) GetFiledropByUUID(uuid string) (out Filedrop, err error) { 27 err = d.db.First(&out, "uuid = ?", uuid).Error 28 return 29 } 30 31 func (d *DkfDB) GetFiledropByFileName(fileName string) (out Filedrop, err error) { 32 err = d.db.First(&out, "file_name = ?", fileName).Error 33 return 34 } 35 36 func (d *DkfDB) GetFiledrops() (out []Filedrop, err error) { 37 err = d.db.Find(&out).Error 38 return 39 } 40 41 func (d *DkfDB) CreateFiledrop() (out Filedrop, err error) { 42 out.UUID = uuid.New().String() 43 out.FileName = utils.MD5([]byte(utils.GenerateToken32())) 44 err = d.db.Save(&out).Error 45 return 46 } 47 48 func (d *Filedrop) Exists() bool { 49 filePath1 := filepath.Join(config.Global.ProjectFiledropPath.Get(), d.FileName) 50 return utils.FileExists(filePath1) 51 } 52 53 func (d *Filedrop) GetContent() (*os.File, *ucrypto.StreamDecrypter, error) { 54 password := []byte(d.Password) 55 filePath1 := filepath.Join(config.Global.ProjectFiledropPath.Get(), d.FileName) 56 f, err := os.Open(filePath1) 57 if err != nil { 58 return nil, nil, err 59 } 60 decrypter, err := utils.DecryptStream(password, d.IV, f) 61 if err != nil { 62 f.Close() 63 return nil, nil, err 64 } 65 return f, decrypter, nil 66 } 67 68 func (d *Filedrop) Delete(db *DkfDB) error { 69 if d.FileName != "" { 70 if err := os.Remove(filepath.Join(config.Global.ProjectFiledropPath.Get(), d.FileName)); err != nil { 71 logrus.Error(err) 72 } 73 } 74 if err := db.db.Delete(&d).Error; err != nil { 75 return err 76 } 77 return nil 78 } 79 80 func (d *Filedrop) DoSave(db *DkfDB) { 81 if err := db.db.Save(d).Error; err != nil { 82 logrus.Error(err) 83 } 84 }