78 lines
1.5 KiB
Go
78 lines
1.5 KiB
Go
|
package taskservice
|
||
|
|
||
|
import (
|
||
|
"io"
|
||
|
"mime/multipart"
|
||
|
"os"
|
||
|
"path"
|
||
|
|
||
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/config"
|
||
|
)
|
||
|
|
||
|
type Service struct {
|
||
|
config *config.Config
|
||
|
}
|
||
|
|
||
|
type TaskUploadInput struct {
|
||
|
TaskID string
|
||
|
Repo string
|
||
|
|
||
|
Files []*multipart.FileHeader
|
||
|
}
|
||
|
|
||
|
func New(cfg *config.Config) *Service {
|
||
|
return &Service{
|
||
|
config: cfg,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (s *Service) Upload(input *TaskUploadInput) error {
|
||
|
repo := input.Repo
|
||
|
taskID := input.TaskID
|
||
|
files := input.Files
|
||
|
|
||
|
localPath := path.Join(repo, "task", taskID)
|
||
|
taskFolderPath := path.Join(s.config.UploadDir, "extra", 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
|
||
|
}
|
||
|
// Символическая ссылка
|
||
|
targetPath := path.Join("../extra/", localPath, fileHeader.Filename)
|
||
|
symLink := path.Join(s.config.UploadDir, "out", fileHeader.Filename)
|
||
|
err = createSymlink(targetPath, symLink)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|