|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package file_record |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "fmt" |
| 25 | + "os" |
| 26 | + "path/filepath" |
| 27 | + "time" |
| 28 | + |
| 29 | + "github.com/apache/answer/internal/base/constant" |
| 30 | + "github.com/apache/answer/internal/entity" |
| 31 | + "github.com/apache/answer/internal/service/revision" |
| 32 | + "github.com/apache/answer/internal/service/service_config" |
| 33 | + "github.com/apache/answer/internal/service/siteinfo_common" |
| 34 | + "github.com/apache/answer/pkg/checker" |
| 35 | + "github.com/apache/answer/pkg/dir" |
| 36 | + "github.com/apache/answer/pkg/writer" |
| 37 | + "github.com/segmentfault/pacman/log" |
| 38 | +) |
| 39 | + |
| 40 | +// FileRecordRepo file record repository |
| 41 | +type FileRecordRepo interface { |
| 42 | + AddFileRecord(ctx context.Context, fileRecord *entity.FileRecord) (err error) |
| 43 | + UpdateFileRecord(ctx context.Context, fileRecord *entity.FileRecord) (err error) |
| 44 | + GetFileRecordPage(ctx context.Context, page, pageSize int, cond *entity.FileRecord) ( |
| 45 | + fileRecordList []*entity.FileRecord, total int64, err error) |
| 46 | + DeleteFileRecord(ctx context.Context, id int) (err error) |
| 47 | +} |
| 48 | + |
| 49 | +// FileRecordService file record service |
| 50 | +type FileRecordService struct { |
| 51 | + fileRecordRepo FileRecordRepo |
| 52 | + revisionRepo revision.RevisionRepo |
| 53 | + serviceConfig *service_config.ServiceConfig |
| 54 | + siteInfoService siteinfo_common.SiteInfoCommonService |
| 55 | +} |
| 56 | + |
| 57 | +// NewFileRecordService new file record service |
| 58 | +func NewFileRecordService( |
| 59 | + fileRecordRepo FileRecordRepo, |
| 60 | + revisionRepo revision.RevisionRepo, |
| 61 | + serviceConfig *service_config.ServiceConfig, |
| 62 | + siteInfoService siteinfo_common.SiteInfoCommonService, |
| 63 | +) *FileRecordService { |
| 64 | + return &FileRecordService{ |
| 65 | + fileRecordRepo: fileRecordRepo, |
| 66 | + revisionRepo: revisionRepo, |
| 67 | + serviceConfig: serviceConfig, |
| 68 | + siteInfoService: siteInfoService, |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// AddFileRecord add file record |
| 73 | +func (fs *FileRecordService) AddFileRecord(ctx context.Context, userID, filePath, fileURL, source string) { |
| 74 | + record := &entity.FileRecord{ |
| 75 | + UserID: userID, |
| 76 | + FilePath: filePath, |
| 77 | + FileURL: fileURL, |
| 78 | + Source: source, |
| 79 | + Status: entity.FileRecordStatusAvailable, |
| 80 | + ObjectID: "0", |
| 81 | + } |
| 82 | + if err := fs.fileRecordRepo.AddFileRecord(ctx, record); err != nil { |
| 83 | + log.Errorf("add file record error: %v", err) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// CleanOrphanUploadFiles clean orphan upload files |
| 88 | +func (fs *FileRecordService) CleanOrphanUploadFiles(ctx context.Context) { |
| 89 | + page, pageSize := 1, 1000 |
| 90 | + |
| 91 | + for { |
| 92 | + fileRecordList, total, err := fs.fileRecordRepo.GetFileRecordPage(ctx, page, pageSize, &entity.FileRecord{ |
| 93 | + Status: entity.FileRecordStatusAvailable, |
| 94 | + }) |
| 95 | + if err != nil { |
| 96 | + log.Errorf("get file record page error: %v", err) |
| 97 | + return |
| 98 | + } |
| 99 | + if len(fileRecordList) == 0 || total == 0 { |
| 100 | + break |
| 101 | + } |
| 102 | + for _, fileRecord := range fileRecordList { |
| 103 | + // If this file record created in 48 hours, no need to check |
| 104 | + if fileRecord.CreatedAt.AddDate(0, 0, 2).After(time.Now()) { |
| 105 | + continue |
| 106 | + } |
| 107 | + if checker.IsNotZeroString(fileRecord.ObjectID) { |
| 108 | + _, exist, err := fs.revisionRepo.GetLastRevisionByObjectID(ctx, fileRecord.ObjectID) |
| 109 | + if err != nil { |
| 110 | + log.Errorf("get last revision by object id error: %v", err) |
| 111 | + continue |
| 112 | + } |
| 113 | + if exist { |
| 114 | + continue |
| 115 | + } |
| 116 | + } else { |
| 117 | + lastRevision, exist, err := fs.revisionRepo.GetLastRevisionByFileURL(ctx, fileRecord.FileURL) |
| 118 | + if err != nil { |
| 119 | + log.Errorf("get last revision by file url error: %v", err) |
| 120 | + continue |
| 121 | + } |
| 122 | + if exist { |
| 123 | + // update the file record object id |
| 124 | + fileRecord.ObjectID = lastRevision.ObjectID |
| 125 | + if err := fs.fileRecordRepo.UpdateFileRecord(ctx, fileRecord); err != nil { |
| 126 | + log.Errorf("update file record object id error: %v", err) |
| 127 | + } |
| 128 | + continue |
| 129 | + } |
| 130 | + } |
| 131 | + // Delete and move the file record |
| 132 | + if err := fs.deleteAndMoveFileRecord(ctx, fileRecord); err != nil { |
| 133 | + log.Error(err) |
| 134 | + } |
| 135 | + } |
| 136 | + page++ |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func (fs *FileRecordService) PurgeDeletedFiles(ctx context.Context) { |
| 141 | + deletedPath := filepath.Join(fs.serviceConfig.UploadPath, constant.DeletedSubPath) |
| 142 | + log.Infof("purge deleted files: %s", deletedPath) |
| 143 | + err := os.RemoveAll(deletedPath) |
| 144 | + if err != nil { |
| 145 | + log.Errorf("purge deleted files error: %v", err) |
| 146 | + return |
| 147 | + } |
| 148 | + err = dir.CreateDirIfNotExist(deletedPath) |
| 149 | + if err != nil { |
| 150 | + log.Errorf("create deleted directory error: %v", err) |
| 151 | + } |
| 152 | + return |
| 153 | +} |
| 154 | + |
| 155 | +func (fs *FileRecordService) deleteAndMoveFileRecord(ctx context.Context, fileRecord *entity.FileRecord) error { |
| 156 | + // Delete the file record |
| 157 | + if err := fs.fileRecordRepo.DeleteFileRecord(ctx, fileRecord.ID); err != nil { |
| 158 | + return fmt.Errorf("delete file record error: %v", err) |
| 159 | + } |
| 160 | + |
| 161 | + // Move the file to the deleted directory |
| 162 | + oldFilename := filepath.Base(fileRecord.FilePath) |
| 163 | + oldFilePath := filepath.Join(fs.serviceConfig.UploadPath, fileRecord.FilePath) |
| 164 | + deletedPath := filepath.Join(fs.serviceConfig.UploadPath, constant.DeletedSubPath, oldFilename) |
| 165 | + |
| 166 | + if err := writer.MoveFile(oldFilePath, deletedPath); err != nil { |
| 167 | + return fmt.Errorf("move file error: %v", err) |
| 168 | + } |
| 169 | + |
| 170 | + log.Debugf("delete and move file: %s", fileRecord.FileURL) |
| 171 | + return nil |
| 172 | +} |
0 commit comments