Redo router and use chi, implement anchor file
This commit is contained in:
287
internal/handlers/home.go
Normal file
287
internal/handlers/home.go
Normal file
@@ -0,0 +1,287 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/debridmediamanager/zurg/internal/config"
|
||||
"github.com/debridmediamanager/zurg/internal/version"
|
||||
"github.com/debridmediamanager/zurg/pkg/realdebrid"
|
||||
)
|
||||
|
||||
type SponsorResponse struct {
|
||||
Patreon string `json:"patreon"`
|
||||
Github string `json:"github"`
|
||||
Paypal string `json:"paypal"`
|
||||
}
|
||||
type RootResponse struct {
|
||||
Version string `json:"version"`
|
||||
BuiltAt string `json:"built_at"`
|
||||
GitCommit string `json:"git_commit"`
|
||||
Dav string `json:"dav"`
|
||||
Html string `json:"html"`
|
||||
Logs string `json:"logs"`
|
||||
UserInfo *realdebrid.User `json:"user_info"` // Replace UserInfoType with the actual type
|
||||
MemAlloc uint64 `json:"mem_alloc"` // Memory allocation in MB
|
||||
TotalAlloc uint64 `json:"total_alloc"` // Total memory allocated in MB
|
||||
Sys uint64 `json:"sys"` // System memory in MB
|
||||
NumGC uint32 `json:"num_gc"` // Number of completed GC cycles
|
||||
PID int `json:"pid"` // Process ID
|
||||
Sponsor SponsorResponse `json:"sponsor_zurg"` // Sponsorship links
|
||||
Config config.ZurgConfig `json:"config"`
|
||||
}
|
||||
|
||||
func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
|
||||
userInfo, err := zr.api.GetUserInformation()
|
||||
if err != nil {
|
||||
http.Error(resp, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var mem runtime.MemStats
|
||||
runtime.ReadMemStats(&mem)
|
||||
|
||||
response := RootResponse{
|
||||
Version: version.GetVersion(),
|
||||
BuiltAt: version.GetBuiltAt(),
|
||||
GitCommit: version.GetGitCommit(),
|
||||
Dav: fmt.Sprintf("//%s/dav/", req.Host),
|
||||
Html: fmt.Sprintf("//%s/http/", req.Host),
|
||||
Logs: fmt.Sprintf("//%s/logs/", req.Host),
|
||||
UserInfo: userInfo,
|
||||
MemAlloc: bToMb(mem.Alloc),
|
||||
TotalAlloc: bToMb(mem.TotalAlloc),
|
||||
Sys: bToMb(mem.Sys),
|
||||
NumGC: mem.NumGC,
|
||||
PID: os.Getpid(),
|
||||
Sponsor: SponsorResponse{
|
||||
Patreon: "https://www.patreon.com/debridmediamanager",
|
||||
Github: "https://github.com/sponsors/debridmediamanager",
|
||||
Paypal: "https://paypal.me/yowmamasita",
|
||||
},
|
||||
Config: zr.cfg.GetConfig(),
|
||||
}
|
||||
|
||||
out := `<table border="1px">
|
||||
<tr>
|
||||
<th colspan="3">zurg</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Version</td>
|
||||
<td colspan="2">%s</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Built At</td>
|
||||
<td colspan="2">%s</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Git Commit</td>
|
||||
<td colspan="2">%s</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>DAV</td>
|
||||
<td colspan="2"><a href="%s">%s</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>HTML</td>
|
||||
<td colspan="2"><a href="%s">%s</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Logs</td>
|
||||
<td colspan="2"><a href="%s">%s</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Memory Allocation</td>
|
||||
<td colspan="2">%d MB</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Total Memory Allocated</td>
|
||||
<td colspan="2">%d MB</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>System Memory</td>
|
||||
<td colspan="2">%d MB</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Number of GC Cycles</td>
|
||||
<td colspan="2">%d</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Process ID</td>
|
||||
<td colspan="2">%d</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="3">Sponsor Zurg</td>
|
||||
<td>Patreon</td>
|
||||
<td><a href="%s">%s</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Github</td>
|
||||
<td><a href="%s">%s</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Paypal</td>
|
||||
<td><a href="%s">%s</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="6">User Info</td>
|
||||
<td>Username</td>
|
||||
<td>%s</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Points</td>
|
||||
<td>%d</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Locale</td>
|
||||
<td>%s</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Type</td>
|
||||
<td>%s</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Premium</td>
|
||||
<td>%d seconds</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Expiration</td>
|
||||
<td>%s</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="20">Config</td>
|
||||
<td>Version</td>
|
||||
<td>%s</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Token</td>
|
||||
<td>%s</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Host</td>
|
||||
<td>%s</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Port</td>
|
||||
<td>%s</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Number of Workers</td>
|
||||
<td>%d</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Refresh Every Seconds</td>
|
||||
<td>%d</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Retain RD Torrent Name</td>
|
||||
<td>%t</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Retain Folder Name Extension</td>
|
||||
<td>%t</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Can Repair</td>
|
||||
<td>%t</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Delete Rar Files</td>
|
||||
<td>%t</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>RealDebrid Timeout</td>
|
||||
<td>%d</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Use Download Cache</td>
|
||||
<td>%t</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Rate Limit Sleep Seconds</td>
|
||||
<td>%d</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Retries Until Failed</td>
|
||||
<td>%d</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Preferred Hosts</td>
|
||||
<td>%s</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Network Buffer Size</td>
|
||||
<td>%d</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Serve From Rclone</td>
|
||||
<td>%t</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Verify Download Link</td>
|
||||
<td>%t</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Force IPv6</td>
|
||||
<td>%t</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>On Library Update</td>
|
||||
<td>%s</td>
|
||||
</tr>
|
||||
</table>
|
||||
`
|
||||
out = fmt.Sprintf(out,
|
||||
response.Version,
|
||||
response.BuiltAt,
|
||||
response.GitCommit,
|
||||
response.Dav,
|
||||
response.Dav,
|
||||
response.Html,
|
||||
response.Html,
|
||||
response.Logs,
|
||||
response.Logs,
|
||||
response.MemAlloc,
|
||||
response.TotalAlloc,
|
||||
response.Sys,
|
||||
response.NumGC,
|
||||
response.PID,
|
||||
response.Sponsor.Patreon,
|
||||
response.Sponsor.Patreon,
|
||||
response.Sponsor.Github,
|
||||
response.Sponsor.Github,
|
||||
response.Sponsor.Paypal,
|
||||
response.Sponsor.Paypal,
|
||||
response.UserInfo.Username,
|
||||
response.UserInfo.Points,
|
||||
response.UserInfo.Locale,
|
||||
response.UserInfo.Type,
|
||||
response.UserInfo.Premium,
|
||||
response.UserInfo.Expiration,
|
||||
response.Config.Version,
|
||||
strings.Replace(response.Config.Token, response.Config.Token[len(response.Config.Token)-48:], "*****", 1),
|
||||
response.Config.Host,
|
||||
response.Config.Port,
|
||||
response.Config.NumOfWorkers,
|
||||
response.Config.RefreshEverySeconds,
|
||||
response.Config.RetainRDTorrentName,
|
||||
response.Config.RetainFolderNameExtension,
|
||||
response.Config.CanRepair,
|
||||
response.Config.DeleteRarFiles,
|
||||
response.Config.RealDebridTimeout,
|
||||
response.Config.UseDownloadCache,
|
||||
response.Config.RateLimitSleepSeconds,
|
||||
response.Config.RetriesUntilFailed,
|
||||
strings.Join(response.Config.PreferredHosts, ", "),
|
||||
response.Config.NetworkBufferSize,
|
||||
response.Config.ServeFromRclone,
|
||||
response.Config.VerifyDownloadLink,
|
||||
response.Config.ForceIPv6,
|
||||
response.Config.OnLibraryUpdate,
|
||||
)
|
||||
|
||||
fmt.Fprint(resp, out)
|
||||
}
|
||||
298
internal/handlers/router.go
Normal file
298
internal/handlers/router.go
Normal file
@@ -0,0 +1,298 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/debridmediamanager/zurg/internal/config"
|
||||
"github.com/debridmediamanager/zurg/internal/dav"
|
||||
intHttp "github.com/debridmediamanager/zurg/internal/http"
|
||||
"github.com/debridmediamanager/zurg/internal/torrent"
|
||||
"github.com/debridmediamanager/zurg/internal/universal"
|
||||
"github.com/debridmediamanager/zurg/internal/version"
|
||||
"github.com/debridmediamanager/zurg/pkg/logutil"
|
||||
"github.com/debridmediamanager/zurg/pkg/realdebrid"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type Handlers struct {
|
||||
getfile *universal.GetFile
|
||||
torMgr *torrent.TorrentManager
|
||||
cfg config.ConfigInterface
|
||||
api *realdebrid.RealDebrid
|
||||
log *logutil.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
chi.RegisterMethod("PROPFIND")
|
||||
chi.RegisterMethod("MKCOL")
|
||||
chi.RegisterMethod("MOVE")
|
||||
}
|
||||
|
||||
func AttachHandlers(router *chi.Mux, getfile *universal.GetFile, torMgr *torrent.TorrentManager, cfg config.ConfigInterface, api *realdebrid.RealDebrid, log *logutil.Logger) {
|
||||
hs := &Handlers{
|
||||
getfile: getfile,
|
||||
torMgr: torMgr,
|
||||
cfg: cfg,
|
||||
api: api,
|
||||
log: log,
|
||||
}
|
||||
|
||||
router.Use(globalOptionsHandler)
|
||||
|
||||
router.Get("/", hs.handleHome)
|
||||
router.Get("/{mountType}/version.txt", hs.handleVersionFile)
|
||||
|
||||
router.Get("/http/", hs.handleHttpRoot)
|
||||
router.Get("/http/{directory}/", hs.handleHttpTorrentsList)
|
||||
router.Get("/http/{directory}/{torrent}/", hs.handleHttpFilesList)
|
||||
router.Get("/http/{directory}/{torrent}/{file}", hs.universalDownloadFileHandler)
|
||||
router.Head("/http/{directory}/{torrent}/{file}", hs.httpHeadHandler)
|
||||
|
||||
router.Get("/dav/", hs.handleDavRoot)
|
||||
router.Get("/dav/{directory}/", hs.handleDavTorrentsList)
|
||||
router.Get("/dav/{directory}/{torrent}/", hs.handleDavFilesList)
|
||||
router.Get("/dav/{directory}/{torrent}/{file}", hs.universalDownloadFileHandler)
|
||||
router.MethodFunc("PROPFIND", "/dav/", hs.handleDavRoot)
|
||||
router.MethodFunc("PROPFIND", "/dav/{directory}/", hs.handleDavTorrentsList)
|
||||
router.MethodFunc("PROPFIND", "/dav/{directory}/{torrent}/", hs.handleDavFilesList)
|
||||
router.MethodFunc("PROPFIND", "/dav/{directory}/{torrent}/{file}", hs.davCheckSingleFileHandler)
|
||||
|
||||
router.Get("/infuse/", hs.handleInfuseRoot)
|
||||
router.Get("/infuse/{directory}/", hs.handleInfuseTorrentsList)
|
||||
router.Get("/infuse/{directory}/{torrent}/", hs.handleInfuseFilesList)
|
||||
router.Get("/infuse/{directory}/{torrent}/{file}", hs.universalDownloadFileHandler)
|
||||
router.MethodFunc("PROPFIND", "/infuse/", hs.handleInfuseRoot)
|
||||
router.MethodFunc("PROPFIND", "/infuse/{directory}/", hs.handleInfuseTorrentsList)
|
||||
router.MethodFunc("PROPFIND", "/infuse/{directory}/{torrent}/", hs.handleInfuseFilesList)
|
||||
|
||||
// note: reused handlers for dav and infuse
|
||||
router.Delete("/{mountType}/{directory}/{torrent}/", hs.deleteTorrentHandler)
|
||||
router.Delete("/{mountType}/{directory}/{torrent}/{file}", hs.deleteFileHandler)
|
||||
router.MethodFunc("MKCOL", "/{mountType}/{directory}/{torrent}/", hs.mkcolTorrentHandler)
|
||||
router.MethodFunc("MOVE", "/{mountType}/{directory}/{torrent}/", hs.moveTorrentHandler)
|
||||
router.MethodFunc("MOVE", "/{mountType}/{directory}/{torrent}/{file}", hs.moveFileHandler)
|
||||
|
||||
// logs route
|
||||
router.Get("/logs", hs.logsHandler)
|
||||
router.Get("/logs/", hs.logsHandler)
|
||||
|
||||
router.MethodNotAllowed(func(resp http.ResponseWriter, req *http.Request) {
|
||||
hs.log.Debugf("Method not allowed: %s %s %v", req.Method, req.URL, req.Header)
|
||||
http.Error(resp, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
})
|
||||
}
|
||||
|
||||
// handle root request
|
||||
|
||||
func (hs *Handlers) innerRootHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(*torrent.TorrentManager) ([]byte, error), contentType string) {
|
||||
out, err := handleFunc(hs.torMgr)
|
||||
if err != nil {
|
||||
http.Error(resp, "Not Found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
resp.Header().Set("Content-Type", contentType)
|
||||
resp.WriteHeader(http.StatusOK)
|
||||
resp.Write(out)
|
||||
}
|
||||
|
||||
func (hs *Handlers) handleHttpRoot(resp http.ResponseWriter, req *http.Request) {
|
||||
hs.innerRootHandler(resp, req, intHttp.ServeRootDirectory, "text/html; charset=\"utf-8\"")
|
||||
}
|
||||
|
||||
func (hs *Handlers) handleDavRoot(resp http.ResponseWriter, req *http.Request) {
|
||||
hs.innerRootHandler(resp, req, dav.ServeRootDirectory, "text/xml; charset=\"utf-8\"")
|
||||
}
|
||||
|
||||
func (hs *Handlers) handleInfuseRoot(resp http.ResponseWriter, req *http.Request) {
|
||||
hs.innerRootHandler(resp, req, dav.ServeRootDirectoryForInfuse, "text/xml; charset=\"utf-8\"")
|
||||
}
|
||||
|
||||
// handle torrent list request
|
||||
|
||||
func (hs *Handlers) innerTorrentsListHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(string, *torrent.TorrentManager) ([]byte, error), contentType string) {
|
||||
directory := chi.URLParam(req, "directory")
|
||||
out, err := handleFunc(directory, hs.torMgr)
|
||||
if err != nil {
|
||||
http.Error(resp, "Not Found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
resp.Header().Set("Content-Type", contentType)
|
||||
resp.WriteHeader(http.StatusOK)
|
||||
resp.Write(out)
|
||||
}
|
||||
|
||||
func (hs *Handlers) handleHttpTorrentsList(resp http.ResponseWriter, req *http.Request) {
|
||||
directory := chi.URLParam(req, "directory")
|
||||
handlerFunc := intHttp.ServeTorrentsList
|
||||
if directory == config.DOWNLOADS {
|
||||
handlerFunc = func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
|
||||
return intHttp.ServeDownloadsList(torMgr)
|
||||
}
|
||||
}
|
||||
hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/html; charset=\"utf-8\"")
|
||||
}
|
||||
|
||||
func (hs *Handlers) handleDavTorrentsList(resp http.ResponseWriter, req *http.Request) {
|
||||
directory := chi.URLParam(req, "directory")
|
||||
handlerFunc := dav.ServeTorrentsList
|
||||
if directory == config.DOWNLOADS {
|
||||
handlerFunc = func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
|
||||
return dav.ServeDownloadsList(torMgr)
|
||||
}
|
||||
}
|
||||
hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"")
|
||||
}
|
||||
|
||||
func (hs *Handlers) handleInfuseTorrentsList(resp http.ResponseWriter, req *http.Request) {
|
||||
directory := chi.URLParam(req, "directory")
|
||||
handlerFunc := dav.ServeTorrentsListForInfuse
|
||||
if directory == config.DOWNLOADS {
|
||||
handlerFunc = func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
|
||||
return dav.ServeDownloadsListForInfuse(torMgr)
|
||||
}
|
||||
}
|
||||
hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"")
|
||||
}
|
||||
|
||||
// handle files list request
|
||||
|
||||
func (hs *Handlers) innerFilesListHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(string, string, *torrent.TorrentManager) ([]byte, error), contentType string) {
|
||||
directory := chi.URLParam(req, "directory")
|
||||
torrentName := chi.URLParam(req, "torrent")
|
||||
out, err := handleFunc(directory, torrentName, hs.torMgr)
|
||||
if err != nil {
|
||||
http.Error(resp, "Not Found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
resp.Header().Set("Content-Type", contentType)
|
||||
resp.WriteHeader(http.StatusOK)
|
||||
resp.Write(out)
|
||||
}
|
||||
|
||||
func (hs *Handlers) handleHttpFilesList(resp http.ResponseWriter, req *http.Request) {
|
||||
hs.innerFilesListHandler(resp, req, intHttp.ServeFilesList, "text/html; charset=\"utf-8\"")
|
||||
}
|
||||
|
||||
func (hs *Handlers) handleDavFilesList(resp http.ResponseWriter, req *http.Request) {
|
||||
hs.innerFilesListHandler(resp, req, dav.ServeFilesList, "text/xml; charset=\"utf-8\"")
|
||||
}
|
||||
|
||||
func (hs *Handlers) handleInfuseFilesList(resp http.ResponseWriter, req *http.Request) {
|
||||
hs.innerFilesListHandler(resp, req, dav.ServeFilesListForInfuse, "text/xml; charset=\"utf-8\"")
|
||||
}
|
||||
|
||||
func (hs *Handlers) handleVersionFile(resp http.ResponseWriter, req *http.Request) {
|
||||
out, _ := version.GetFile()
|
||||
resp.Header().Set("Content-Type", "text/plain; charset=\"utf-8\"")
|
||||
resp.WriteHeader(http.StatusOK)
|
||||
resp.Write(out)
|
||||
}
|
||||
|
||||
func (hs *Handlers) deleteFileHandler(resp http.ResponseWriter, req *http.Request) {
|
||||
directory := chi.URLParam(req, "directory")
|
||||
torrentName := chi.URLParam(req, "torrent")
|
||||
fileName := chi.URLParam(req, "file")
|
||||
if dav.HandleDeleteFile(directory, torrentName, fileName, hs.torMgr) != nil {
|
||||
http.Error(resp, "Not Found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
resp.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (hs *Handlers) deleteTorrentHandler(resp http.ResponseWriter, req *http.Request) {
|
||||
directory := chi.URLParam(req, "directory")
|
||||
torrentName := chi.URLParam(req, "torrent")
|
||||
if dav.HandleDeleteTorrent(directory, torrentName, hs.torMgr) != nil {
|
||||
http.Error(resp, "Not Found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
resp.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (hs *Handlers) davCheckSingleFileHandler(resp http.ResponseWriter, req *http.Request) {
|
||||
directory := chi.URLParam(req, "directory")
|
||||
torrentName := chi.URLParam(req, "torrent")
|
||||
fileName := chi.URLParam(req, "file")
|
||||
out, err := dav.HandleSingleFile(directory, torrentName, fileName, hs.torMgr)
|
||||
if err != nil {
|
||||
fmt.Println(">>>>>>>>>>>>>>>>>>>. not found", err)
|
||||
http.Error(resp, "Not Found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
fmt.Println(">>>>>>>>>>>>>>>>>>>. found yey")
|
||||
resp.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"")
|
||||
resp.WriteHeader(http.StatusOK)
|
||||
resp.Write(out)
|
||||
}
|
||||
|
||||
func (hs *Handlers) mkcolTorrentHandler(resp http.ResponseWriter, req *http.Request) {
|
||||
fmt.Println(">>>>>>>>>>>>>>>>>>> mkcolTorrentHandler")
|
||||
resp.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (hs *Handlers) moveFileHandler(resp http.ResponseWriter, req *http.Request) {
|
||||
directory := chi.URLParam(req, "directory")
|
||||
torrentName := chi.URLParam(req, "torrent")
|
||||
fileName := chi.URLParam(req, "file")
|
||||
newName := req.Header.Get("Destination")
|
||||
newName = filepath.Base(newName)
|
||||
fmt.Println(">>>>>>>>>>>>>>>>>>> moveFileHandler", fileName, ">>>>>>>>", newName)
|
||||
if dav.HandleRenameFile(directory, torrentName, fileName, newName, hs.torMgr) != nil {
|
||||
fmt.Println(">>>>>>>>>>>>>>>>>>> moveFileHandler not found")
|
||||
http.Error(resp, "Not Found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
fmt.Println(">>>>>>>>>>>>>>>>>>> moveFileHandler yay")
|
||||
resp.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (hs *Handlers) moveTorrentHandler(resp http.ResponseWriter, req *http.Request) {
|
||||
directory := chi.URLParam(req, "directory")
|
||||
torrentName := chi.URLParam(req, "torrent")
|
||||
newName := req.Header.Get("Destination")
|
||||
newName = filepath.Base(newName)
|
||||
if dav.HandleRenameTorrent(directory, torrentName, newName, hs.torMgr) != nil {
|
||||
http.Error(resp, "Not Found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
resp.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func globalOptionsHandler(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "OPTIONS" {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func (hs *Handlers) universalDownloadFileHandler(resp http.ResponseWriter, req *http.Request) {
|
||||
directory := chi.URLParam(req, "directory")
|
||||
torrentName := chi.URLParam(req, "torrent")
|
||||
fileName := chi.URLParam(req, "file")
|
||||
hs.getfile.ServeFile(directory, torrentName, fileName, resp, req, hs.torMgr, hs.cfg, hs.log)
|
||||
}
|
||||
|
||||
func (hs *Handlers) httpHeadHandler(resp http.ResponseWriter, req *http.Request) {
|
||||
directory := chi.URLParam(req, "directory")
|
||||
torrentName := chi.URLParam(req, "torrent")
|
||||
fileName := chi.URLParam(req, "file")
|
||||
universal.HandleHeadRequest(directory, torrentName, fileName, resp, req, hs.torMgr, hs.log)
|
||||
}
|
||||
|
||||
func (hs *Handlers) logsHandler(resp http.ResponseWriter, req *http.Request) {
|
||||
logs, err := hs.log.GetLogsFromFile()
|
||||
if err != nil {
|
||||
http.Error(resp, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(resp, logs)
|
||||
}
|
||||
|
||||
func bToMb(b uint64) uint64 {
|
||||
return b / 1024 / 1024
|
||||
}
|
||||
Reference in New Issue
Block a user