This commit is contained in:
2017-11-10 12:39:55 +01:00
commit 5a752618c3
579 changed files with 81271 additions and 0 deletions

18
webcontroller/apidoc.go Normal file
View File

@@ -0,0 +1,18 @@
package webcontroller
import (
"net/http"
"fornaxian.com/pixeldrain-web/log"
"fornaxian.com/pixeldrain-web/webcontroller/templates"
"github.com/julienschmidt/httprouter"
)
// ServeAPIDoc serves the API docuementation
func ServeAPIDoc(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
err := templates.Get().ExecuteTemplate(w, "apidoc", nil)
if err != nil {
log.Error("Error executing template apidoc: %s", err)
}
}

14
webcontroller/favicon.go Normal file
View File

@@ -0,0 +1,14 @@
package webcontroller
import (
"net/http"
"github.com/julienschmidt/httprouter"
"fornaxian.com/pixeldrain-web/conf"
)
// ServeFavicon yes we need a controller for this
func ServeFavicon(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
http.ServeFile(w, r, conf.StaticResourceDir()+"favicon.ico")
}

View File

@@ -0,0 +1,140 @@
package webcontroller
import (
"fmt"
"io"
"net/http"
"net/url"
"path/filepath"
"strings"
"fornaxian.com/pixeldrain-web/conf"
"fornaxian.com/pixeldrain-web/pixelapi"
"github.com/julienschmidt/httprouter"
)
// ServeFilePreview controller for GET /u/:id/preview
func ServeFilePreview(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
if p.ByName("id") == "demo" {
ServeFilePreviewDemo(w) // Required for a-ads.com quality check
return
}
inf := pixelapi.GetFileInfo(p.ByName("id")) // TODO: Error handling
if inf == nil {
ServeNotFound(w, r)
return
}
var fp FilePreview
io.WriteString(w, fp.Run(inf))
}
type FilePreview struct {
FileInfo *pixelapi.FileInfo
FileURL string
DownloadURL string
}
func (f FilePreview) Run(inf *pixelapi.FileInfo) string {
f.FileInfo = inf
f.FileURL = conf.ApiURL() + "/file/" + f.FileInfo.ID
f.DownloadURL = conf.ApiURL() + "/file/" + f.FileInfo.ID + "/download"
if strings.HasPrefix(f.FileInfo.MimeType, "image") {
return f.image()
}
if strings.HasPrefix(f.FileInfo.MimeType, "video") {
return f.video()
}
if strings.HasPrefix(f.FileInfo.MimeType, "audio") {
return f.audio()
}
switch f.FileInfo.MimeType {
case
"application/ogg":
return f.audio()
case
"application/matroska",
"application/x-matroska":
return f.video()
case
"application/pdf",
"application/x-pdf":
return f.pdf()
case
"application/octet-stream": // Fallback for when mime type not recognized
switch filepath.Ext(f.FileInfo.FileName) {
case
".mp3":
return f.audio()
case
".mp4":
return f.video()
}
}
// none of the mime type checks triggered, so we return the default page
return f.def()
}
func (f FilePreview) image() string {
return fmt.Sprintf(`<div class="image-container">
<img id="displayImg" src="%s" class="pannable drop-shadow"/>
</div>
<script src="/res/viewer-scripts/image.js"></script>`,
f.FileURL)
}
func (f FilePreview) audio() string {
return fmt.Sprintf(`<div class="image-container">
<br/><br/>
<img src="/res/img/mime/audio.png" alt="Audio"/>
<br/>%s<br/><br/>
<audio id="audioPlayer" controls="controls" autoplay="autoplay" style="width:90%%;">
<source src="%s" />
</audio>
</div>
<script src="/res/viewer-scripts/audio.js"></script>`,
f.FileInfo.FileName,
f.FileURL,
)
}
func (f FilePreview) video() string {
return fmt.Sprintf(`<div class="image-container">
<video id="videoPlayer" autoplay="autoplay" controls="controls" class="center drop-shadow">
<source src="%s"/>
Your web browser does not support the HTML video tag.
</video>
</div>
<script src="/res/viewer-scripts/video.js"></script>`,
f.FileURL,
)
}
func (f FilePreview) pdf() string {
u, _ := url.Parse(f.FileURL)
return f.frame("/res/misc/pdf-viewer/web/viewer.html?file=" + u.String())
}
func (f FilePreview) frame(url string) string {
return fmt.Sprintf(`<iframe src="%s" class="image-container"
seamless="seamless" frameborder="0" allowtransparency="true"
</iframe>`,
url,
)
}
func (f FilePreview) def() string {
return fmt.Sprintf(
`%s<br/>%s<br/><a href="%s"><img src="%s" class="image"></a>`,
f.FileInfo.FileName,
f.FileInfo.MimeType,
f.DownloadURL,
f.FileInfo.Thumbnail,
)
}

View File

@@ -0,0 +1,65 @@
package webcontroller
import (
"net/http"
"strings"
"fornaxian.com/pixeldrain-web/log"
"fornaxian.com/pixeldrain-web/pixelapi"
"fornaxian.com/pixeldrain-web/webcontroller/templates"
"github.com/julienschmidt/httprouter"
)
// ServeFileViewer controller for GET /u/:id
func ServeFileViewer(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
if p.ByName("id") == "demo" {
ServeFileViewerDemo(w) // Required for a-ads.com quality check
return
}
var list = strings.Contains(p.ByName("id"), ",")
var ids []string
if list {
ids = strings.Split(p.ByName("id"), ",")
} else {
ids = append(ids, p.ByName("id"))
}
var finfo []*pixelapi.FileInfo
for _, id := range ids {
inf := pixelapi.GetFileInfo(id)
if inf == nil {
continue
}
finfo = append(finfo, inf)
}
if len(finfo) == 0 {
ServeNotFound(w, r)
return
}
var err error
if list {
listdata := map[string]interface{}{
"data": finfo,
"date_created": "now",
"title": "Concatenation of files",
"date_lastview": "now",
"views": 0,
}
err = templates.Get().ExecuteTemplate(w, "file_viewer", map[string]interface{}{
"APIResponse": listdata,
"Type": "list",
})
} else {
err = templates.Get().ExecuteTemplate(w, "file_viewer", map[string]interface{}{
"APIResponse": finfo[0],
"Type": "file",
})
}
if err != nil {
log.Error("Error executing template file_viewer: %s", err)
}
}

View File

@@ -0,0 +1,52 @@
package webcontroller
import (
"io"
"net/http"
"fornaxian.com/pixeldrain-web/webcontroller/templates"
)
// ServeFileViewerDemo is a dummy API response that responds with info about a
// non-existent demo file. This is required by the a-ads ad network to allow for
// automatic checking of the presence of the ad unit on this page.
func ServeFileViewerDemo(w http.ResponseWriter) {
templates.Get().ExecuteTemplate(w, "file_viewer", map[string]interface{}{
"APIResponse": map[string]interface{}{
"id": "demo",
"file_name": "Demo file",
"date_upload": "2017-01-01 12:34:56",
"date_lastview": "2017-01-01 12:34:56",
"file_size": 123456789,
"views": 1,
"mime_type": "text/demo",
"description": "A file to demonstrate the viewer page",
"mime_image": "/res/img/mime/text.png",
"thumbnail": "/res/img/mime/text.png",
},
"Type": "file",
})
}
// ServeFilePreviewDemo serves the content of the demo file. It contains a nice
// message to the human reviewers of the a-ads ad network so they can properly
// categorize the website.
func ServeFilePreviewDemo(w http.ResponseWriter) {
io.WriteString(w,
`<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js?skin=desert"></script>
<div class="text-container"><pre class="pre-container linenums" style="width: 100%">
, __ _
/|/ \o | | | o
|___/ _ | | __| ,_ __, _ _
| | /\/ |/ |/ / | / | / | | / |/ |
| |_/ /\_/|__/|__/\_/|_/ |_/\_/|_/|_/ | |_/
This is a demonstration of Pixeldrain's file viewer.
The website automatically detects what kind of file you requested and prepares a page for viewing it properly. This is what a text file would look like on Pixeldrain. You can upload your own text file at pixeldrain.com/t.
Pixeldrain is a free service for sharing files with large or small groups of people. For more information visit the home page by pressing the home button on the toolbar at the left side of the screen.
Press the Details button or "i" for more info about Pixeldrain's file viewer.
</pre></div>`)
}

19
webcontroller/history.go Normal file
View File

@@ -0,0 +1,19 @@
package webcontroller
import (
"net/http"
"fornaxian.com/pixeldrain-web/conf"
"fornaxian.com/pixeldrain-web/log"
"fornaxian.com/pixeldrain-web/webcontroller/templates"
"github.com/julienschmidt/httprouter"
)
func ServeHistory(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
err := templates.Get().ExecuteTemplate(w, "history-cookies", map[string]interface{}{
"APIURL": conf.ApiURL(),
})
if err != nil {
log.Error("Error executing template history: %s", err)
}
}

14
webcontroller/home.go Normal file
View File

@@ -0,0 +1,14 @@
package webcontroller
import (
"net/http"
"fornaxian.com/pixeldrain-web/webcontroller/templates"
"github.com/julienschmidt/httprouter"
)
// ServeHome serves the home page
func ServeHome(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
templates.Get().ExecuteTemplate(w, "home", nil)
}

13
webcontroller/notfound.go Normal file
View File

@@ -0,0 +1,13 @@
package webcontroller
import (
"net/http"
"fornaxian.com/pixeldrain-web/log"
"fornaxian.com/pixeldrain-web/webcontroller/templates"
)
func ServeNotFound(w http.ResponseWriter, r *http.Request) {
log.Debug("Not Found: %s", r.URL)
templates.Get().ExecuteTemplate(w, "error", nil)
}

14
webcontroller/paste.go Normal file
View File

@@ -0,0 +1,14 @@
package webcontroller
import (
"net/http"
"fornaxian.com/pixeldrain-web/webcontroller/templates"
"github.com/julienschmidt/httprouter"
)
// ServePaste serves the page that is used to upload plain text files
func ServePaste(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
templates.Get().ExecuteTemplate(w, "paste", nil)
}

View File

@@ -0,0 +1,21 @@
package templates
import (
"html/template"
"time"
"fornaxian.com/pixeldrain-web/conf"
)
var funcMap = template.FuncMap{
"bgPatternCount": bgPatternCount,
"debugMode": debugMode,
}
func bgPatternCount() uint8 {
return uint8(time.Now().UnixNano() % 16)
}
func debugMode() bool {
return conf.DebugMode()
}

View File

@@ -0,0 +1,46 @@
package templates
import (
"html/template"
"os"
"path/filepath"
"fornaxian.com/pixeldrain-web/conf"
"fornaxian.com/pixeldrain-web/log"
)
var templates *template.Template
var templatePaths []string
// ParseTemplates parses the templates in the template directory which is
// defined in the config file
func ParseTemplates() {
filepath.Walk(conf.TemplateDir(), func(path string, f os.FileInfo, err error) error {
if f.IsDir() {
return nil
}
templatePaths = append(templatePaths, path)
log.Info("Template found: %s", path)
return nil
})
tpl := template.New("")
// Import template functions from funcs.go
tpl = tpl.Funcs(funcMap)
var err error
tpl, err = tpl.ParseFiles(templatePaths...)
if err != nil {
log.Error("Template parsing failed: %v", err)
}
// Swap out the old templates with the new templates, to minimize
// modifications to the original variable.
templates = tpl
}
// Get returns the templates, so they can be used to render views
func Get() *template.Template {
return templates
}