43 lines
717 B
Go
43 lines
717 B
Go
package video
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"mime/multipart"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
)
|
|
|
|
func SaveVideo(file multipart.File, handler *multipart.FileHeader, title, description string) (*Video, error) {
|
|
|
|
os.MkdirAll("storage/videos", os.ModePerm)
|
|
|
|
filename := fmt.Sprintf("%d_%s", time.Now().Unix(), handler.Filename)
|
|
filePath := filepath.Join("storage/videos", filename)
|
|
|
|
dst, err := os.Create(filePath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer dst.Close()
|
|
|
|
_, err = io.Copy(dst, file)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
video := Video{
|
|
Title: title,
|
|
Description: description,
|
|
FileURL: filePath,
|
|
}
|
|
|
|
err = InsertVideo(&video)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &video, nil
|
|
}
|