delete.go (2477B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package media 19 20 import ( 21 "context" 22 "errors" 23 "fmt" 24 "strings" 25 26 "codeberg.org/gruf/go-store/v2/storage" 27 "github.com/superseriousbusiness/gotosocial/internal/db" 28 "github.com/superseriousbusiness/gotosocial/internal/gtserror" 29 ) 30 31 // Delete deletes the media attachment with the given ID, including all files pertaining to that attachment. 32 func (p *Processor) Delete(ctx context.Context, mediaAttachmentID string) gtserror.WithCode { 33 attachment, err := p.state.DB.GetAttachmentByID(ctx, mediaAttachmentID) 34 if err != nil { 35 if err == db.ErrNoEntries { 36 // attachment already gone 37 return nil 38 } 39 // actual error 40 return gtserror.NewErrorInternalError(err) 41 } 42 43 errs := []string{} 44 45 // delete the thumbnail from storage 46 if attachment.Thumbnail.Path != "" { 47 if err := p.state.Storage.Delete(ctx, attachment.Thumbnail.Path); err != nil && !errors.Is(err, storage.ErrNotFound) { 48 errs = append(errs, fmt.Sprintf("remove thumbnail at path %s: %s", attachment.Thumbnail.Path, err)) 49 } 50 } 51 52 // delete the file from storage 53 if attachment.File.Path != "" { 54 if err := p.state.Storage.Delete(ctx, attachment.File.Path); err != nil && !errors.Is(err, storage.ErrNotFound) { 55 errs = append(errs, fmt.Sprintf("remove file at path %s: %s", attachment.File.Path, err)) 56 } 57 } 58 59 // delete the attachment 60 if err := p.state.DB.DeleteAttachment(ctx, mediaAttachmentID); err != nil && !errors.Is(err, db.ErrNoEntries) { 61 errs = append(errs, fmt.Sprintf("remove attachment: %s", err)) 62 } 63 64 if len(errs) != 0 { 65 return gtserror.NewErrorInternalError(fmt.Errorf("Delete: one or more errors removing attachment with id %s: %s", mediaAttachmentID, strings.Join(errs, "; "))) 66 } 67 68 return nil 69 }