diff --git a/internal/models/db.go b/internal/models/db.go index 6529ca6..9e05c8f 100644 --- a/internal/models/db.go +++ b/internal/models/db.go @@ -1,6 +1,8 @@ package models -import "gorm.io/gorm" +import ( + "gorm.io/gorm" +) type ALTRepo struct { gorm.Model @@ -14,9 +16,10 @@ type GitRepo struct { Name string `gorm:"uniqueIndex"` } -type RPMFiles struct { +type RPMFile struct { TaskID int Name string + Arch string } type TaskStatus int @@ -51,7 +54,7 @@ type Task struct { ALTRepoID int ALTRepo ALTRepo - RPMFiles []RPMFiles + Files []RPMFile } type GitRepoAltRepoTask struct { diff --git a/internal/services/taskservice/service.go b/internal/services/taskservice/service.go index 68a0065..6b0eccc 100644 --- a/internal/services/taskservice/service.go +++ b/internal/services/taskservice/service.go @@ -1,13 +1,7 @@ package taskservice import ( - "fmt" - "io" "mime/multipart" - "os" - "os/exec" - "path" - "strings" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/app" ) @@ -28,70 +22,3 @@ func New(app *app.App) *Service { app: app, } } - -func getRPMArchitecture(filePath string) (string, error) { - cmd := exec.Command("rpm", "-qp", "--queryformat", "%{ARCH}", filePath) - - output, err := cmd.Output() - if err != nil { - return "", fmt.Errorf("ошибка при выполнении команды rpm: %v", err) - } - - arch := strings.TrimSpace(string(output)) - return arch, nil -} - -func (s *Service) Upload(input *TaskUploadInput) error { - taskID := input.TaskID - files := input.Files - - localPath := path.Join(taskID) - taskFolderPath := path.Join(s.app.Config.UploadDir, "tasks", localPath) - os.MkdirAll(taskFolderPath, os.ModePerm) - - for _, fileHeader := range files { - file, err := fileHeader.Open() - if err != nil { - return err - } - defer file.Close() - - // Полный путь для файла - filePath := path.Join(taskFolderPath, fileHeader.Filename) - - //Удаляем файл если такой уже существует - if _, err := os.Stat(filePath); err == nil { - err = os.Remove(filePath) - if err != nil { - return err - } - } - // Сохранение файла на сервере - outFile, err := os.Create(filePath) - if err != nil { - return err - } - defer outFile.Close() - - _, err = io.Copy(outFile, file) - if err != nil { - return err - } - - arch, err := getRPMArchitecture(filePath) - if err != nil { - return err - } - - // Символическая ссылка - targetPath := path.Join("../../../../tasks/", localPath, fileHeader.Filename) - symLink := path.Join(s.app.Config.UploadDir, "repo/Sisyphus", arch, "RPMS.aides", fileHeader.Filename) - err = createSymlink(targetPath, symLink) - if err != nil { - return err - } - - } - - return nil -} diff --git a/internal/services/taskservice/upload.go b/internal/services/taskservice/upload.go new file mode 100644 index 0000000..4bd8973 --- /dev/null +++ b/internal/services/taskservice/upload.go @@ -0,0 +1,114 @@ +package taskservice + +import ( + "errors" + "fmt" + "io" + "os" + "os/exec" + "path" + "strconv" + "strings" + + "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models" + "gorm.io/gorm" +) + +func getRPMArchitecture(filePath string) (string, error) { + cmd := exec.Command("rpm", "-qp", "--queryformat", "%{ARCH}", filePath) + + output, err := cmd.Output() + if err != nil { + return "", fmt.Errorf("ошибка при выполнении команды rpm: %v", err) + } + + arch := strings.TrimSpace(string(output)) + return arch, nil +} + +func (s *Service) Upload(input *TaskUploadInput) error { + taskID, err := strconv.Atoi(input.TaskID) + if err != nil { + return err + } + files := input.Files + + task := models.Task{} + result := s.app.Db.Where( + "id = ?", taskID, + ).Where( + "status = ?", models.StatusPending, + ).First(&task) + + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return result.Error + } + if result.Error != nil { + return result.Error + } + + fmt.Printf("%v", task.Status) + + localPath := path.Join(input.TaskID) + taskFolderPath := path.Join(s.app.Config.UploadDir, "tasks", localPath) + os.MkdirAll(taskFolderPath, os.ModePerm) + + for _, fileHeader := range files { + file, err := fileHeader.Open() + if err != nil { + return err + } + defer file.Close() + + if !strings.HasSuffix(fileHeader.Filename, ".rpm") { + return fmt.Errorf("invalid file type: only .rpm files are allowed") + } + + // Полный путь для файла + filePath := path.Join(taskFolderPath, fileHeader.Filename) + + //Удаляем файл если такой уже существует + if _, err := os.Stat(filePath); err == nil { + err = os.Remove(filePath) + if err != nil { + return err + } + } + // Сохранение файла на сервере + outFile, err := os.Create(filePath) + if err != nil { + return err + } + defer outFile.Close() + + _, err = io.Copy(outFile, file) + if err != nil { + return err + } + + arch, err := getRPMArchitecture(filePath) + if err != nil { + return err + } + + fmt.Println(arch) + + // Символическая ссылка + /* + targetPath := path.Join("../../../../tasks/", localPath, fileHeader.Filename) + symLink := path.Join(s.app.Config.UploadDir, "repo/Sisyphus", arch, "RPMS.aides", fileHeader.Filename) + err = createSymlink(targetPath, symLink) + if err != nil { + return err + } + */ + + } + + task.Status = models.StatusCompleted + if err := s.app.Db.Save(&task).Error; err != nil { + return err + } + + return nil +}