proper marking of file as deleted
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -23,5 +24,10 @@ func main() {
|
|||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
dav.Router(mux, c, t)
|
dav.Router(mux, c, t)
|
||||||
http.ListenAndServe(":8123", mux)
|
addr := fmt.Sprintf(":%s", c.GetPort())
|
||||||
|
log.Printf("Starting server on %s\n", addr)
|
||||||
|
err := http.ListenAndServe(addr, mux)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicf("Failed to start server: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
type ConfigInterface interface {
|
type ConfigInterface interface {
|
||||||
GetVersion() string
|
GetVersion() string
|
||||||
GetToken() string
|
GetToken() string
|
||||||
|
GetPort() string
|
||||||
GetDirectories() []string
|
GetDirectories() []string
|
||||||
MeetsConditions(directory, fileID, fileName string) bool
|
MeetsConditions(directory, fileID, fileName string) bool
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,4 +3,5 @@ package config
|
|||||||
type ZurgConfig struct {
|
type ZurgConfig struct {
|
||||||
Version string `yaml:"zurg"`
|
Version string `yaml:"zurg"`
|
||||||
Token string `yaml:"token"`
|
Token string `yaml:"token"`
|
||||||
|
Port string `yaml:"port"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,10 @@ func (z *ZurgConfigV1) GetToken() string {
|
|||||||
return z.Token
|
return z.Token
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (z *ZurgConfigV1) GetPort() string {
|
||||||
|
return z.Port
|
||||||
|
}
|
||||||
|
|
||||||
func (z *ZurgConfigV1) GetDirectories() []string {
|
func (z *ZurgConfigV1) GetDirectories() []string {
|
||||||
var rootDirectories []string
|
var rootDirectories []string
|
||||||
for directory := range z.Directories {
|
for directory := range z.Directories {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
type ZurgConfigV1 struct {
|
type ZurgConfigV1 struct {
|
||||||
ZurgConfig
|
ZurgConfig `yaml:",inline"`
|
||||||
Directories map[string]*DirectoryFilterConditionsV1 `yaml:"directories"`
|
Directories map[string]*DirectoryFilterConditionsV1 `yaml:"directories"`
|
||||||
}
|
}
|
||||||
type DirectoryFilterConditionsV1 struct {
|
type DirectoryFilterConditionsV1 struct {
|
||||||
|
|||||||
@@ -79,11 +79,16 @@ func Setup(app *aero.Application, c config.ConfigInterface, t *torrent.TorrentMa
|
|||||||
}
|
}
|
||||||
|
|
||||||
filenameV2, linkFragment := davextra.ExtractLinkFragment(filename)
|
filenameV2, linkFragment := davextra.ExtractLinkFragment(filename)
|
||||||
link := getLink(torrents, filenameV2, linkFragment)
|
torrent, file := getFile(torrents, filenameV2, linkFragment)
|
||||||
if link == "" {
|
if file == nil {
|
||||||
|
log.Println("Cannot find file", filename)
|
||||||
|
return ctx.Error(http.StatusNotFound, "Cannot find file")
|
||||||
|
}
|
||||||
|
if file.Link == "" {
|
||||||
log.Println("Link not found", filename)
|
log.Println("Link not found", filename)
|
||||||
return ctx.Error(http.StatusNotFound, "Cannot find file")
|
return ctx.Error(http.StatusNotFound, "Cannot find file")
|
||||||
}
|
}
|
||||||
|
link := file.Link
|
||||||
|
|
||||||
unrestrictFn := func() (*realdebrid.UnrestrictResponse, error) {
|
unrestrictFn := func() (*realdebrid.UnrestrictResponse, error) {
|
||||||
return realdebrid.UnrestrictLink(c.GetToken(), link)
|
return realdebrid.UnrestrictLink(c.GetToken(), link)
|
||||||
@@ -94,6 +99,7 @@ func Setup(app *aero.Application, c config.ConfigInterface, t *torrent.TorrentMa
|
|||||||
// when unrestricting fails, it means the file is not available anymore
|
// when unrestricting fails, it means the file is not available anymore
|
||||||
// if it's the only file, tough luck
|
// if it's the only file, tough luck
|
||||||
log.Println("Cannot unrestrict link", link, filenameV2)
|
log.Println("Cannot unrestrict link", link, filenameV2)
|
||||||
|
t.MarkFileAsDeleted(torrent, file)
|
||||||
return ctx.Error(http.StatusNotFound, "Cannot find file")
|
return ctx.Error(http.StatusNotFound, "Cannot find file")
|
||||||
}
|
}
|
||||||
if resp.Filename != filenameV2 {
|
if resp.Filename != filenameV2 {
|
||||||
|
|||||||
@@ -40,12 +40,18 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
|
|||||||
}
|
}
|
||||||
|
|
||||||
filenameV2, linkFragment := davextra.ExtractLinkFragment(filename)
|
filenameV2, linkFragment := davextra.ExtractLinkFragment(filename)
|
||||||
link := getLink(torrents, filenameV2, linkFragment)
|
torrent, file := getFile(torrents, filenameV2, linkFragment)
|
||||||
if link == "" {
|
if file == nil {
|
||||||
|
log.Println("Cannot find file", filename)
|
||||||
|
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if file.Link == "" {
|
||||||
log.Println("Link not found", filename)
|
log.Println("Link not found", filename)
|
||||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
link := file.Link
|
||||||
|
|
||||||
unrestrictFn := func() (*realdebrid.UnrestrictResponse, error) {
|
unrestrictFn := func() (*realdebrid.UnrestrictResponse, error) {
|
||||||
return realdebrid.UnrestrictLink(c.GetToken(), link)
|
return realdebrid.UnrestrictLink(c.GetToken(), link)
|
||||||
@@ -58,6 +64,7 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
|
|||||||
// if it's the only file, try to readd it
|
// if it's the only file, try to readd it
|
||||||
// delete the old one, add a new one
|
// delete the old one, add a new one
|
||||||
log.Println("Cannot unrestrict link", link, filenameV2)
|
log.Println("Cannot unrestrict link", link, filenameV2)
|
||||||
|
t.MarkFileAsDeleted(torrent, file)
|
||||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -72,15 +79,15 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
|
|||||||
http.Redirect(w, r, resp.Download, http.StatusFound)
|
http.Redirect(w, r, resp.Download, http.StatusFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getLink finds a link by a fragment, it might be wrong
|
// getFile finds a link by a fragment, it might be wrong
|
||||||
func getLink(torrents []torrent.Torrent, filename, fragment string) string {
|
func getFile(torrents []torrent.Torrent, filename, fragment string) (*torrent.Torrent, *torrent.File) {
|
||||||
for _, torrent := range torrents {
|
for t := range torrents {
|
||||||
for _, file := range torrent.SelectedFiles {
|
for f, file := range torrents[t].SelectedFiles {
|
||||||
fname := filepath.Base(file.Path)
|
fname := filepath.Base(file.Path)
|
||||||
if filename == fname && strings.HasPrefix(file.Link, fmt.Sprintf("https://real-debrid.com/d/%s", fragment)) {
|
if filename == fname && strings.HasPrefix(file.Link, fmt.Sprintf("https://real-debrid.com/d/%s", fragment)) {
|
||||||
return file.Link
|
return &torrents[t], &torrents[t].SelectedFiles[f]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ""
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -254,6 +254,12 @@ func (t *TorrentManager) getInfo(torrentID string) *Torrent {
|
|||||||
return torrent
|
return torrent
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *TorrentManager) MarkFileAsDeleted(torrent *Torrent, file *File) {
|
||||||
|
log.Println("Marking file as deleted", file.Path)
|
||||||
|
file.Link = ""
|
||||||
|
t.writeToFile(torrent.ID, torrent)
|
||||||
|
}
|
||||||
|
|
||||||
func (t *TorrentManager) GetInfo(torrentID string) *Torrent {
|
func (t *TorrentManager) GetInfo(torrentID string) *Torrent {
|
||||||
for i := range t.torrents {
|
for i := range t.torrents {
|
||||||
if t.torrents[i].ID == torrentID {
|
if t.torrents[i].ID == torrentID {
|
||||||
|
|||||||
@@ -110,7 +110,9 @@ func canFetchFirstByte(url string) bool {
|
|||||||
_, err := resp.Body.Read(buffer)
|
_, err := resp.Body.Read(buffer)
|
||||||
return err == nil
|
return err == nil
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return false
|
||||||
|
}
|
||||||
// If server doesn't support partial content, try reading the first byte and immediately close
|
// If server doesn't support partial content, try reading the first byte and immediately close
|
||||||
buffer := make([]byte, 1)
|
buffer := make([]byte, 1)
|
||||||
_, err = resp.Body.Read(buffer)
|
_, err = resp.Body.Read(buffer)
|
||||||
|
|||||||
Reference in New Issue
Block a user