From 720c27e38f1bb7b7c0ed70ca2815bc49532009c7 Mon Sep 17 00:00:00 2001 From: ElegantMonkey Date: Sun, 14 May 2017 12:28:48 -0300 Subject: [PATCH] Get FileList of new uploaded .torrent files --- router/upload.go | 17 +++++++++++++++++ router/uploadHandler.go | 12 ++++++++++++ 2 files changed, 29 insertions(+) diff --git a/router/upload.go b/router/upload.go index 2b143da9..57a65759 100644 --- a/router/upload.go +++ b/router/upload.go @@ -24,6 +24,13 @@ import ( "github.com/zeebo/bencode" ) +// Use this, because we seem to avoid using models, and we would need +// the torrent ID to create the File in the DB +type UploadedFile struct { + Path string + Filesize int64 +} + // UploadForm serializing HTTP form for torrent upload type UploadForm struct { Name string @@ -39,6 +46,7 @@ type UploadForm struct { SubCategoryID int Filesize int64 Filepath string + FileList []UploadedFile } // TODO: these should be in another package (?) @@ -150,6 +158,15 @@ func (f *UploadForm) ExtractInfo(r *http.Request) error { // extract filesize f.Filesize = int64(torrent.TotalSize()) + + // extract filelist + fileInfos := torrent.Info.GetFiles() + for _, info := range fileInfos { + f.FileList = append(f.FileList, UploadedFile{ + Path: info.Path.FilePath(), + Filesize: int64(info.Length), + }) + } } else { // No torrent file provided magnetUrl, err := url.Parse(string(f.Magnet)) //? diff --git a/router/uploadHandler.go b/router/uploadHandler.go index 50bb47da..e395889d 100644 --- a/router/uploadHandler.go +++ b/router/uploadHandler.go @@ -60,6 +60,18 @@ func UploadHandler(w http.ResponseWriter, r *http.Request) { Description: uploadForm.Description, UploaderID: user.ID} db.ORM.Create(&torrent) + + // add filelist to files db, if we have one + if len(uploadForm.FileList) > 0 { + for _, uploadedFile := range uploadForm.FileList { + file := model.File{ + TorrentID: torrent.ID, + Path: uploadedFile.Path, + Filesize: uploadedFile.Filesize} + db.ORM.Create(&file) + } + } + url, err := Router.Get("view_torrent").URL("id", strconv.FormatUint(uint64(torrent.ID), 10)) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError)