2017-01-20 01:58:46 -05:00
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package models
import (
2021-09-23 10:45:36 -05:00
"context"
2017-01-20 01:58:46 -05:00
"fmt"
2021-11-16 13:18:25 -05:00
"net/url"
2017-01-20 01:58:46 -05:00
"path"
2021-09-19 06:49:59 -05:00
"code.gitea.io/gitea/models/db"
2021-11-09 14:57:58 -05:00
"code.gitea.io/gitea/models/unit"
2017-01-20 01:58:46 -05:00
"code.gitea.io/gitea/modules/setting"
2020-08-17 23:23:45 -05:00
"code.gitea.io/gitea/modules/storage"
2019-08-15 09:46:21 -05:00
"code.gitea.io/gitea/modules/timeutil"
2018-03-05 20:22:16 -05:00
2019-10-17 04:26:49 -05:00
"xorm.io/xorm"
2017-01-20 01:58:46 -05:00
)
// Attachment represent a attachment of issue/comment/release.
type Attachment struct {
2017-04-19 21:31:31 -05:00
ID int64 ` xorm:"pk autoincr" `
UUID string ` xorm:"uuid UNIQUE" `
2021-09-08 10:19:30 -05:00
RepoID int64 ` xorm:"INDEX" ` // this should not be zero
IssueID int64 ` xorm:"INDEX" ` // maybe zero when creating
ReleaseID int64 ` xorm:"INDEX" ` // maybe zero when creating
2019-04-02 14:25:05 -05:00
UploaderID int64 ` xorm:"INDEX DEFAULT 0" ` // Notice: will be zero before this column added
2017-04-19 21:31:31 -05:00
CommentID int64
Name string
2019-08-15 09:46:21 -05:00
DownloadCount int64 ` xorm:"DEFAULT 0" `
Size int64 ` xorm:"DEFAULT 0" `
CreatedUnix timeutil . TimeStamp ` xorm:"created" `
2017-01-20 01:58:46 -05:00
}
2021-09-19 06:49:59 -05:00
func init ( ) {
db . RegisterModel ( new ( Attachment ) )
}
2017-04-19 21:31:31 -05:00
// IncreaseDownloadCount is update download count + 1
func ( a * Attachment ) IncreaseDownloadCount ( ) error {
// Update download count.
2021-09-23 10:45:36 -05:00
if _ , err := db . GetEngine ( db . DefaultContext ) . Exec ( "UPDATE `attachment` SET download_count=download_count+1 WHERE id=?" , a . ID ) ; err != nil {
2017-04-19 21:31:31 -05:00
return fmt . Errorf ( "increase attachment count: %v" , err )
}
return nil
}
2020-08-17 23:23:45 -05:00
// AttachmentRelativePath returns the relative path
func AttachmentRelativePath ( uuid string ) string {
return path . Join ( uuid [ 0 : 1 ] , uuid [ 1 : 2 ] , uuid )
2017-01-20 01:58:46 -05:00
}
2020-08-17 23:23:45 -05:00
// RelativePath returns the relative path of the attachment
func ( a * Attachment ) RelativePath ( ) string {
return AttachmentRelativePath ( a . UUID )
2017-01-20 01:58:46 -05:00
}
2018-03-05 20:22:16 -05:00
// DownloadURL returns the download url of the attached file
func ( a * Attachment ) DownloadURL ( ) string {
2021-11-16 13:18:25 -05:00
return setting . AppURL + "attachments/" + url . PathEscape ( a . UUID )
2018-03-05 20:22:16 -05:00
}
2020-01-04 18:20:08 -05:00
// LinkedRepository returns the linked repo if any
2021-11-09 14:57:58 -05:00
func ( a * Attachment ) LinkedRepository ( ) ( * Repository , unit . Type , error ) {
2020-01-04 18:20:08 -05:00
if a . IssueID != 0 {
iss , err := GetIssueByID ( a . IssueID )
if err != nil {
2021-11-09 14:57:58 -05:00
return nil , unit . TypeIssues , err
2020-01-04 18:20:08 -05:00
}
repo , err := GetRepositoryByID ( iss . RepoID )
2021-11-09 14:57:58 -05:00
unitType := unit . TypeIssues
2020-02-18 19:36:19 -05:00
if iss . IsPull {
2021-11-09 14:57:58 -05:00
unitType = unit . TypePullRequests
2020-02-18 19:36:19 -05:00
}
return repo , unitType , err
2020-01-04 18:20:08 -05:00
} else if a . ReleaseID != 0 {
rel , err := GetReleaseByID ( a . ReleaseID )
if err != nil {
2021-11-09 14:57:58 -05:00
return nil , unit . TypeReleases , err
2020-01-04 18:20:08 -05:00
}
repo , err := GetRepositoryByID ( rel . RepoID )
2021-11-09 14:57:58 -05:00
return repo , unit . TypeReleases , err
2020-01-04 18:20:08 -05:00
}
return nil , - 1 , nil
}
2018-03-05 20:22:16 -05:00
// GetAttachmentByID returns attachment by given id
func GetAttachmentByID ( id int64 ) ( * Attachment , error ) {
2021-09-23 10:45:36 -05:00
return getAttachmentByID ( db . GetEngine ( db . DefaultContext ) , id )
2018-03-05 20:22:16 -05:00
}
2021-09-19 06:49:59 -05:00
func getAttachmentByID ( e db . Engine , id int64 ) ( * Attachment , error ) {
2020-06-17 12:50:11 -05:00
attach := & Attachment { }
if has , err := e . ID ( id ) . Get ( attach ) ; err != nil {
2018-03-05 20:22:16 -05:00
return nil , err
} else if ! has {
return nil , ErrAttachmentNotExist { ID : id , UUID : "" }
}
return attach , nil
}
2021-09-19 06:49:59 -05:00
func getAttachmentByUUID ( e db . Engine , uuid string ) ( * Attachment , error ) {
2020-06-17 12:50:11 -05:00
attach := & Attachment { }
has , err := e . Where ( "uuid=?" , uuid ) . Get ( attach )
2017-01-20 01:58:46 -05:00
if err != nil {
return nil , err
} else if ! has {
return nil , ErrAttachmentNotExist { 0 , uuid }
}
return attach , nil
}
2019-12-10 19:01:52 -05:00
// GetAttachmentsByUUIDs returns attachment by given UUID list.
2021-09-23 10:45:36 -05:00
func GetAttachmentsByUUIDs ( ctx context . Context , uuids [ ] string ) ( [ ] * Attachment , error ) {
return getAttachmentsByUUIDs ( db . GetEngine ( ctx ) , uuids )
2019-12-10 19:01:52 -05:00
}
2021-09-19 06:49:59 -05:00
func getAttachmentsByUUIDs ( e db . Engine , uuids [ ] string ) ( [ ] * Attachment , error ) {
2017-01-20 01:58:46 -05:00
if len ( uuids ) == 0 {
return [ ] * Attachment { } , nil
}
// Silently drop invalid uuids.
attachments := make ( [ ] * Attachment , 0 , len ( uuids ) )
return attachments , e . In ( "uuid" , uuids ) . Find ( & attachments )
}
// GetAttachmentByUUID returns attachment by given UUID.
func GetAttachmentByUUID ( uuid string ) ( * Attachment , error ) {
2021-09-23 10:45:36 -05:00
return getAttachmentByUUID ( db . GetEngine ( db . DefaultContext ) , uuid )
2017-01-20 01:58:46 -05:00
}
2021-09-06 09:46:20 -05:00
// ExistAttachmentsByUUID returns true if attachment is exist by given UUID
func ExistAttachmentsByUUID ( uuid string ) ( bool , error ) {
2021-09-23 10:45:36 -05:00
return db . GetEngine ( db . DefaultContext ) . Where ( "`uuid`=?" , uuid ) . Exist ( new ( Attachment ) )
2021-09-06 09:46:20 -05:00
}
2019-01-06 17:37:30 -05:00
// GetAttachmentByReleaseIDFileName returns attachment by given releaseId and fileName.
func GetAttachmentByReleaseIDFileName ( releaseID int64 , fileName string ) ( * Attachment , error ) {
2021-09-23 10:45:36 -05:00
return getAttachmentByReleaseIDFileName ( db . GetEngine ( db . DefaultContext ) , releaseID , fileName )
2019-01-06 17:37:30 -05:00
}
2021-09-19 06:49:59 -05:00
func getAttachmentsByIssueID ( e db . Engine , issueID int64 ) ( [ ] * Attachment , error ) {
2017-01-20 01:58:46 -05:00
attachments := make ( [ ] * Attachment , 0 , 10 )
return attachments , e . Where ( "issue_id = ? AND comment_id = 0" , issueID ) . Find ( & attachments )
}
// GetAttachmentsByIssueID returns all attachments of an issue.
func GetAttachmentsByIssueID ( issueID int64 ) ( [ ] * Attachment , error ) {
2021-09-23 10:45:36 -05:00
return getAttachmentsByIssueID ( db . GetEngine ( db . DefaultContext ) , issueID )
2017-01-20 01:58:46 -05:00
}
// GetAttachmentsByCommentID returns all attachments if comment by given ID.
func GetAttachmentsByCommentID ( commentID int64 ) ( [ ] * Attachment , error ) {
2021-09-23 10:45:36 -05:00
return getAttachmentsByCommentID ( db . GetEngine ( db . DefaultContext ) , commentID )
2017-10-01 11:52:35 -05:00
}
2021-09-19 06:49:59 -05:00
func getAttachmentsByCommentID ( e db . Engine , commentID int64 ) ( [ ] * Attachment , error ) {
2017-01-20 01:58:46 -05:00
attachments := make ( [ ] * Attachment , 0 , 10 )
2020-02-27 18:10:27 -05:00
return attachments , e . Where ( "comment_id=?" , commentID ) . Find ( & attachments )
2017-01-20 01:58:46 -05:00
}
2019-01-06 17:37:30 -05:00
// getAttachmentByReleaseIDFileName return a file based on the the following infos:
2021-09-19 06:49:59 -05:00
func getAttachmentByReleaseIDFileName ( e db . Engine , releaseID int64 , fileName string ) ( * Attachment , error ) {
2019-01-06 17:37:30 -05:00
attach := & Attachment { ReleaseID : releaseID , Name : fileName }
has , err := e . Get ( attach )
if err != nil {
return nil , err
} else if ! has {
return nil , err
}
return attach , nil
}
2017-01-20 01:58:46 -05:00
// DeleteAttachment deletes the given attachment and optionally the associated file.
func DeleteAttachment ( a * Attachment , remove bool ) error {
2021-09-23 10:45:36 -05:00
_ , err := DeleteAttachments ( db . DefaultContext , [ ] * Attachment { a } , remove )
2017-01-20 01:58:46 -05:00
return err
}
// DeleteAttachments deletes the given attachments and optionally the associated files.
2021-09-23 10:45:36 -05:00
func DeleteAttachments ( ctx context . Context , attachments [ ] * Attachment , remove bool ) ( int , error ) {
2017-12-24 16:04:22 -05:00
if len ( attachments ) == 0 {
return 0 , nil
}
2021-03-14 13:52:12 -05:00
ids := make ( [ ] int64 , 0 , len ( attachments ) )
2017-12-24 16:04:22 -05:00
for _ , a := range attachments {
ids = append ( ids , a . ID )
}
2021-09-23 10:45:36 -05:00
cnt , err := db . GetEngine ( ctx ) . In ( "id" , ids ) . NoAutoCondition ( ) . Delete ( attachments [ 0 ] )
2017-12-24 16:04:22 -05:00
if err != nil {
return 0 , err
}
if remove {
for i , a := range attachments {
2020-08-17 23:23:45 -05:00
if err := storage . Attachments . Delete ( a . RelativePath ( ) ) ; err != nil {
2017-01-20 01:58:46 -05:00
return i , err
}
}
}
2017-12-24 16:04:22 -05:00
return int ( cnt ) , nil
2017-01-20 01:58:46 -05:00
}
// DeleteAttachmentsByIssue deletes all attachments associated with the given issue.
func DeleteAttachmentsByIssue ( issueID int64 , remove bool ) ( int , error ) {
attachments , err := GetAttachmentsByIssueID ( issueID )
if err != nil {
return 0 , err
}
2021-09-23 10:45:36 -05:00
return DeleteAttachments ( db . DefaultContext , attachments , remove )
2017-01-20 01:58:46 -05:00
}
// DeleteAttachmentsByComment deletes all attachments associated with the given comment.
func DeleteAttachmentsByComment ( commentID int64 , remove bool ) ( int , error ) {
attachments , err := GetAttachmentsByCommentID ( commentID )
if err != nil {
return 0 , err
}
2021-09-23 10:45:36 -05:00
return DeleteAttachments ( db . DefaultContext , attachments , remove )
2017-01-20 01:58:46 -05:00
}
2018-03-05 20:22:16 -05:00
// UpdateAttachment updates the given attachment in database
func UpdateAttachment ( atta * Attachment ) error {
2021-09-23 10:45:36 -05:00
return updateAttachment ( db . GetEngine ( db . DefaultContext ) , atta )
2018-03-05 20:22:16 -05:00
}
2021-03-22 11:09:51 -05:00
// UpdateAttachmentByUUID Updates attachment via uuid
2021-09-23 10:45:36 -05:00
func UpdateAttachmentByUUID ( ctx context . Context , attach * Attachment , cols ... string ) error {
2021-03-22 11:09:51 -05:00
if attach . UUID == "" {
2021-09-23 10:45:36 -05:00
return fmt . Errorf ( "attachment uuid should be not blank" )
2021-03-22 11:09:51 -05:00
}
2021-09-23 10:45:36 -05:00
_ , err := db . GetEngine ( ctx ) . Where ( "uuid=?" , attach . UUID ) . Cols ( cols ... ) . Update ( attach )
2021-03-22 11:09:51 -05:00
return err
}
2021-09-19 06:49:59 -05:00
func updateAttachment ( e db . Engine , atta * Attachment ) error {
2018-03-05 20:22:16 -05:00
var sess * xorm . Session
if atta . ID != 0 && atta . UUID == "" {
sess = e . ID ( atta . ID )
} else {
// Use uuid only if id is not set and uuid is set
sess = e . Where ( "uuid = ?" , atta . UUID )
}
_ , err := sess . Cols ( "name" , "issue_id" , "release_id" , "comment_id" , "download_count" ) . Update ( atta )
return err
}
2019-09-30 11:10:00 -05:00
// DeleteAttachmentsByRelease deletes all attachments associated with the given release.
func DeleteAttachmentsByRelease ( releaseID int64 ) error {
2021-09-23 10:45:36 -05:00
_ , err := db . GetEngine ( db . DefaultContext ) . Where ( "release_id = ?" , releaseID ) . Delete ( & Attachment { } )
2019-09-30 11:10:00 -05:00
return err
}
2020-08-17 23:23:45 -05:00
// IterateAttachment iterates attachments; it should not be used when Gitea is servicing users.
func IterateAttachment ( f func ( attach * Attachment ) error ) error {
var start int
const batchSize = 100
for {
2021-03-14 13:52:12 -05:00
attachments := make ( [ ] * Attachment , 0 , batchSize )
2021-09-23 10:45:36 -05:00
if err := db . GetEngine ( db . DefaultContext ) . Limit ( batchSize , start ) . Find ( & attachments ) ; err != nil {
2020-08-17 23:23:45 -05:00
return err
}
if len ( attachments ) == 0 {
return nil
}
start += len ( attachments )
for _ , attach := range attachments {
if err := f ( attach ) ; err != nil {
return err
}
}
}
}
2021-09-14 14:41:40 -05:00
// CountOrphanedAttachments returns the number of bad attachments
func CountOrphanedAttachments ( ) ( int64 , error ) {
2021-09-23 10:45:36 -05:00
return db . GetEngine ( db . DefaultContext ) . Where ( "(issue_id > 0 and issue_id not in (select id from issue)) or (release_id > 0 and release_id not in (select id from `release`))" ) .
2021-09-14 14:41:40 -05:00
Count ( new ( Attachment ) )
}
// DeleteOrphanedAttachments delete all bad attachments
func DeleteOrphanedAttachments ( ) error {
2021-09-23 10:45:36 -05:00
_ , err := db . GetEngine ( db . DefaultContext ) . Where ( "(issue_id > 0 and issue_id not in (select id from issue)) or (release_id > 0 and release_id not in (select id from `release`))" ) .
2021-09-14 14:41:40 -05:00
Delete ( new ( Attachment ) )
return err
}