99 lines
2.8 KiB
Go
99 lines
2.8 KiB
Go
package universal
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/debridmediamanager/zurg/internal/torrent"
|
|
"github.com/debridmediamanager/zurg/internal/version"
|
|
"github.com/debridmediamanager/zurg/pkg/logutil"
|
|
"github.com/debridmediamanager/zurg/pkg/realdebrid"
|
|
)
|
|
|
|
func CheckFile(directory, torrentName, fileName string, w http.ResponseWriter, req *http.Request, torMgr *torrent.TorrentManager, log *logutil.Logger) {
|
|
torrents, ok := torMgr.DirectoryMap.Get(directory)
|
|
if !ok {
|
|
log.Warnf("Cannot find directory %s", directory)
|
|
http.Error(w, "File not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
torrent, ok := torrents.Get(torrentName)
|
|
if !ok {
|
|
log.Warnf("Cannot find torrent %s from path %s", torrentName, req.URL.Path)
|
|
http.Error(w, "File not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
file, ok := torrent.SelectedFiles.Get(fileName)
|
|
if !ok || file.State.Is("deleted_file") {
|
|
// log.Warnf("Cannot find file %s from path %s", fileName, req.URL.Path)
|
|
http.Error(w, "File not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
contentType := getContentMimeType(fileName)
|
|
contentLength := fmt.Sprintf("%d", file.Bytes)
|
|
lastModified := file.Ended
|
|
w.Header().Set("Content-Type", contentType)
|
|
w.Header().Set("Content-Length", contentLength)
|
|
w.Header().Set("Last-Modified", lastModified)
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
|
|
func CheckDownloadLink(download *realdebrid.Download, w http.ResponseWriter, req *http.Request, torMgr *torrent.TorrentManager, log *logutil.Logger) {
|
|
contentType := getContentMimeType(download.Filename)
|
|
contentLength := fmt.Sprintf("%d", download.Filesize)
|
|
lastModified := download.Generated
|
|
w.Header().Set("Content-Type", contentType)
|
|
w.Header().Set("Content-Length", contentLength)
|
|
w.Header().Set("Last-Modified", lastModified)
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
|
|
func CheckVersionFile(w http.ResponseWriter, req *http.Request, torMgr *torrent.TorrentManager, log *logutil.Logger) {
|
|
_, size := version.GetFile()
|
|
contentType := getContentMimeType(version.FILE)
|
|
contentLength := fmt.Sprintf("%d", size)
|
|
w.Header().Set("Content-Type", contentType)
|
|
w.Header().Set("Content-Length", contentLength)
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
|
|
func getContentMimeType(filePath string) string {
|
|
fileExt := strings.ToLower(filepath.Ext(filePath))
|
|
switch fileExt {
|
|
case ".mkv":
|
|
return "video/x-matroska"
|
|
case ".mp4":
|
|
return "video/mp4"
|
|
case ".avi":
|
|
return "video/x-msvideo"
|
|
case ".wmv":
|
|
return "video/x-ms-wmv"
|
|
case ".m4v":
|
|
return "video/x-m4v"
|
|
case ".mp3":
|
|
return "audio/mpeg"
|
|
case ".rar":
|
|
return "application/x-rar-compressed"
|
|
case ".zip":
|
|
return "application/zip"
|
|
case ".txt":
|
|
return "text/plain"
|
|
case ".srt":
|
|
return "text/srt"
|
|
case ".jpg":
|
|
return "image/jpeg"
|
|
case ".png":
|
|
return "image/png"
|
|
case ".gif":
|
|
return "image/gif"
|
|
case ".webp":
|
|
return "image/webp"
|
|
default:
|
|
return "application/octet-stream"
|
|
}
|
|
}
|