support skynet

This commit is contained in:
2020-02-04 19:37:46 +01:00
parent aea57286d9
commit 3ebf9785da
20 changed files with 852 additions and 611 deletions

View File

@@ -95,3 +95,45 @@ func (wc *WebController) adminGlobalsForm(td *TemplateData, r *http.Request) (f
}
return f
}
// func (wc *WebController) adminFileDeleteForm(td *TemplateData, r *http.Request) (f Form) {
// if isAdmin, err := td.PixelAPI.UserIsAdmin(); err != nil {
// td.Title = err.Error()
// return Form{Title: td.Title}
// } else if !isAdmin.IsAdmin {
// td.Title = ";)"
// return Form{Title: td.Title}
// }
// td.Title = "Admin file removal"
// f = Form{
// Name: "admin_file_removal",
// Title: td.Title,
// PreFormHTML: template.HTML("<p>Paste any pixeldrain file links in here to remove them</p>"),
// Fields: []Field{
// {
// Name: "files",
// Label: "Files to delete",
// Type: FieldTypeTextarea,
// },
// },
// BackLink: "/admin",
// SubmitLabel: "Submit",
// }
// if f.ReadInput(r) {
// filesText := f.FieldVal("files")
// // Get all links from the text
// strings.Index(filesText, "/u/")
// if len(f.SubmitMessages) == 0 {
// // Request was a success
// f.SubmitSuccess = true
// f.SubmitMessages = []template.HTML{template.HTML(
// fmt.Sprintf("Success! %d values updated", successfulUpdates),
// )}
// }
// }
// return f
// }

View File

@@ -2,10 +2,14 @@ package webcontroller
import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
pdmimetype "github.com/Fornaxian/pd_mime_type"
"fornaxian.com/pixeldrain-web/pixelapi"
"github.com/Fornaxian/log"
"github.com/julienschmidt/httprouter"
@@ -152,3 +156,90 @@ func (wc *WebController) serveListViewer(w http.ResponseWriter, r *http.Request,
log.Error("Error executing template file_viewer: %s", err)
}
}
// ServeFileViewer controller for GET /s/:id
func (wc *WebController) serveSkynetViewer(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
var err error
templateData := wc.newTemplateData(w, r)
// Get the first few bytes from the file to probe the content type and
// length
rq, err := http.NewRequest("GET", "https://siasky.net/"+p.ByName("id"), nil)
if err != nil {
panic(err)
}
// Range header limits the number of bytes which will be read
rq.Header.Set("Range", "bytes=0-1023")
resp, err := wc.httpClient.Do(rq)
if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.StatusCode >= 500 {
w.WriteHeader(http.StatusInternalServerError)
wc.templates.Get().ExecuteTemplate(w, "500", templateData)
return
} else if resp.StatusCode >= 400 {
w.WriteHeader(http.StatusNotFound)
wc.templates.Get().ExecuteTemplate(w, "file_not_found", templateData)
return
}
head, err := ioutil.ReadAll(resp.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
wc.templates.Get().ExecuteTemplate(w, "500", templateData)
return
}
var fileType = resp.Header.Get("Content-Type")
if fileType == "application/octet-stream" || fileType == "" {
fileType = pdmimetype.Detect(head)
}
// Now get the size of the file from the content-range header
contentRange := resp.Header.Get("Content-Range")
if contentRange == "" {
w.WriteHeader(http.StatusInternalServerError)
wc.templates.Get().ExecuteTemplate(w, "500", templateData)
return
}
contentRange = strings.TrimPrefix(contentRange, "bytes ")
size, err := strconv.ParseUint(strings.Split(contentRange, "/")[1], 10, 64)
if err != nil {
panic(err)
}
templateData.OGData = ""
templateData.Title = fmt.Sprintf("Skylink ~ pixeldrain")
templateData.Other = viewerData{
Type: "skylink",
APIResponse: pixelapi.FileInfo{
Success: true,
ID: p.ByName("id"),
Name: "skynet_file.dat",
Size: size,
Views: 0,
BandwidthUsed: 0,
DateUpload: time.Now(),
DateLastView: time.Now(),
MimeType: fileType,
MimeImage: "",
ThumbnailHREF: "",
Availability: "",
},
}
var templateName = "file_viewer"
if browserCompat(r.UserAgent()) {
templateName = "file_viewer_compat"
}
err = wc.templates.Get().ExecuteTemplate(w, templateName, templateData)
if err != nil && !strings.Contains(err.Error(), "broken pipe") {
log.Error("Error executing template file_viewer: %s", err)
}
}

View File

@@ -6,6 +6,7 @@ import (
"net/http"
"os"
"strings"
"time"
"github.com/google/uuid"
@@ -30,6 +31,8 @@ type WebController struct {
// page-specific variables
captchaSiteKey string
httpClient *http.Client
}
// New initializes a new WebController by registering all the request handlers
@@ -50,6 +53,7 @@ func New(
apiURLInternal: apiURLInternal,
apiURLExternal: apiURLExternal,
sessionCookieDomain: sessionCookieDomain,
httpClient: &http.Client{Timeout: time.Minute * 10},
}
wc.templates = NewTemplateManager(resourceDir, apiURLExternal, debugMode)
wc.templates.ParseTemplates(false)
@@ -88,6 +92,7 @@ func New(
r.GET(p+"/u/:id" /* */, wc.serveFileViewer)
r.GET(p+"/u/:id/preview" /**/, wc.serveFilePreview)
r.GET(p+"/l/:id" /* */, wc.serveListViewer)
r.GET(p+"/s/:id" /* */, wc.serveSkynetViewer)
r.GET(p+"/t" /* */, wc.serveTemplate("paste", false))
r.GET(p+"/donation" /* */, wc.serveTemplate("donation", false))
r.GET(p+"/widgets" /* */, wc.serveTemplate("widgets", false))