2017-11-18 00:48:34 +00:00
|
|
|
package contented
|
|
|
|
|
|
|
|
import (
|
2023-05-19 07:13:31 +00:00
|
|
|
"context"
|
2023-05-17 06:52:13 +00:00
|
|
|
"encoding/json"
|
2017-11-18 00:48:34 +00:00
|
|
|
"fmt"
|
|
|
|
"html"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2017-11-18 01:11:20 +00:00
|
|
|
"strings"
|
2017-11-18 00:48:34 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-05-19 07:13:31 +00:00
|
|
|
func (this *Server) handlePreview(ctx context.Context, w http.ResponseWriter, fileIDList string) {
|
2017-11-18 00:48:34 +00:00
|
|
|
|
2017-11-18 01:11:20 +00:00
|
|
|
fileIDs := strings.Split(fileIDList, `-`)
|
2017-11-18 00:48:34 +00:00
|
|
|
|
2023-05-17 07:25:45 +00:00
|
|
|
// Early get metadata for the first listed element
|
|
|
|
specialTitle := ""
|
|
|
|
if len(fileIDs) == 1 {
|
|
|
|
mFirst, err := this.Metadata(fileIDs[0])
|
|
|
|
if err != nil { // Same error handling as below -
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
http.Error(w, "Not found", 404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println(err.Error())
|
|
|
|
http.Error(w, "Internal error", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
specialTitle = mFirst.Filename + " (" + fileIDs[0] + ")"
|
|
|
|
} else {
|
|
|
|
specialTitle = fmt.Sprintf("%d images", len(fileIDs))
|
|
|
|
}
|
|
|
|
|
2017-11-18 00:48:34 +00:00
|
|
|
tmpl := `<!DOCTYPE html>
|
2018-06-04 05:22:28 +00:00
|
|
|
<html prefix="og: http://ogp.me/ns#">
|
2017-11-18 00:48:34 +00:00
|
|
|
<head>
|
2023-05-17 07:25:45 +00:00
|
|
|
<title>` + html.EscapeString(specialTitle+" | "+this.opts.ServerPublicProperties.AppTitle) + `</title>
|
2018-06-04 05:22:28 +00:00
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
|
|
<meta property="og:title" content="` + html.EscapeString(this.opts.ServerPublicProperties.AppTitle) + `" />
|
|
|
|
<meta property="og:site_name" content="` + html.EscapeString(this.opts.ServerPublicProperties.AppTitle) + `" />
|
|
|
|
<meta property="og:type" content="website" />
|
|
|
|
`
|
|
|
|
|
2018-06-04 06:28:50 +00:00
|
|
|
if len(this.opts.ServerPublicProperties.CanonicalBaseURL) > 0 {
|
2018-06-04 05:22:28 +00:00
|
|
|
tmpl += `
|
|
|
|
<meta property="og:url" content="` + html.EscapeString(this.opts.ServerPublicProperties.CanonicalBaseURL+`p/`+fileIDList) + `" />
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fileID := range fileIDs {
|
|
|
|
tmpl += `
|
|
|
|
<meta property="og:image" content="` + html.EscapeString(`/thumb/m/`+fileID) + `" />
|
|
|
|
<meta property="og:image:type" content="image/jpeg" />
|
|
|
|
<meta property="og:image:width" content="300" />
|
|
|
|
<meta property="og:image:height" content="300" />
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl += `
|
2017-11-18 00:48:34 +00:00
|
|
|
<style type="text/css">
|
|
|
|
html, body {
|
|
|
|
background: #333;
|
|
|
|
color: #F0F0F0;
|
|
|
|
font-family: sans-serif;
|
|
|
|
}
|
2017-11-18 01:11:20 +00:00
|
|
|
.entry {
|
|
|
|
display: inline-block;
|
|
|
|
margin: 4px;
|
|
|
|
border-radius: 4px;
|
|
|
|
max-width: 340px;
|
|
|
|
}
|
|
|
|
.thumbnail {
|
|
|
|
line-height: 0;
|
|
|
|
width: 340px;
|
2023-05-19 07:07:25 +00:00
|
|
|
height: 340px;
|
2017-11-18 01:11:20 +00:00
|
|
|
text-align: center;
|
2023-05-17 06:52:13 +00:00
|
|
|
position: relative;
|
|
|
|
}
|
|
|
|
.thumbnail-overlay {
|
|
|
|
position: absolute;
|
|
|
|
bottom: 4px;
|
|
|
|
right: 4px;
|
|
|
|
padding: 4px 8px;
|
|
|
|
line-height: 1.5em;
|
|
|
|
|
|
|
|
pointer-events: none;
|
|
|
|
|
|
|
|
background: red;
|
|
|
|
color: white;
|
2017-11-18 01:11:20 +00:00
|
|
|
}
|
|
|
|
.properties {
|
|
|
|
background: #000;
|
|
|
|
padding: 4px;
|
|
|
|
word-break: break-word;
|
|
|
|
}
|
|
|
|
|
2017-11-18 00:48:34 +00:00
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div class="container">
|
2017-11-18 01:11:20 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
for _, fileID := range fileIDs {
|
|
|
|
m, err := this.Metadata(fileID)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
http.Error(w, "Not found", 404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println(err.Error())
|
|
|
|
http.Error(w, "Internal error", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-17 06:52:13 +00:00
|
|
|
if m.MimeType == ALBUM_MIMETYPE {
|
|
|
|
// Special handling for albums
|
2023-05-19 07:13:55 +00:00
|
|
|
f, err := this.ReadFile(ctx, m.FileHash)
|
2023-05-17 06:52:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Opening file '%s' for preview of album '%s': %s", m.FileHash, fileID, err.Error())
|
|
|
|
http.Error(w, "Internal error", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var childIDs []string
|
|
|
|
err = json.NewDecoder(f).Decode(&childIDs)
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to parse album '%s': %s", fileID, err)
|
|
|
|
http.Error(w, "Internal error", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(childIDs) == 0 {
|
|
|
|
log.Printf("Failed to parse album '%s': no entries in album", fileID)
|
|
|
|
http.Error(w, "Internal error", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl += `
|
|
|
|
<div class="entry">
|
|
|
|
<div class="thumbnail">
|
2023-05-17 07:08:36 +00:00
|
|
|
<a href="` + html.EscapeString(`/p/`+strings.Join(childIDs, `-`)) + `"><img loading="lazy" src="` + html.EscapeString(`/thumb/m/`+childIDs[0]) + `"></a>
|
2023-05-17 06:52:13 +00:00
|
|
|
<div class="thumbnail-overlay">` + fmt.Sprintf("%d", len(childIDs)) + ` image(s)</div>
|
|
|
|
</div>
|
|
|
|
<div class="properties">
|
|
|
|
<b>Name:</b> ` + html.EscapeString(m.Filename) + `<br>
|
|
|
|
<b>Hash:</b> <span title="` + html.EscapeString(m.FileHash) + `">hover</span><br>
|
|
|
|
<b>File type:</b> Album<br>
|
|
|
|
<b>Size:</b> ` + fmt.Sprintf("%d", len(childIDs)) + ` image(s)<br>
|
|
|
|
<b>Uploader:</b> ` + html.EscapeString(m.UploadIP) + `<br>
|
|
|
|
<b>Uploaded at:</b> ` + html.EscapeString(m.UploadTime.Format(time.RFC3339)) + `<br>
|
|
|
|
</div>
|
2017-11-18 01:11:20 +00:00
|
|
|
</div>
|
2023-05-17 06:52:13 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
} else {
|
|
|
|
tmpl += `
|
|
|
|
<div class="entry">
|
|
|
|
<div class="thumbnail">
|
2023-05-17 07:08:36 +00:00
|
|
|
<a href="` + html.EscapeString(`/get/`+fileID) + `"><img loading="lazy" src="` + html.EscapeString(`/thumb/m/`+fileID) + `"></a>
|
2023-05-17 06:52:13 +00:00
|
|
|
</div>
|
|
|
|
<div class="properties">
|
|
|
|
<b>Name:</b> ` + html.EscapeString(m.Filename) + `<br>
|
|
|
|
<b>Hash:</b> <span title="` + html.EscapeString(m.FileHash) + `">hover</span><br>
|
|
|
|
<b>File type:</b> ` + html.EscapeString(m.MimeType) + `<br>
|
|
|
|
<b>Size:</b> ` + html.EscapeString(fmt.Sprintf("%d", m.FileSize)) + `<br>
|
|
|
|
<b>Uploader:</b> ` + html.EscapeString(m.UploadIP) + `<br>
|
|
|
|
<b>Uploaded at:</b> ` + html.EscapeString(m.UploadTime.Format(time.RFC3339)) + `<br>
|
|
|
|
</div>
|
2017-11-18 01:11:20 +00:00
|
|
|
</div>
|
2023-05-17 06:52:13 +00:00
|
|
|
`
|
|
|
|
}
|
2017-11-18 01:11:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 00:53:46 +00:00
|
|
|
if this.opts.EnableHomepage {
|
|
|
|
tmpl += `
|
|
|
|
<div class="return">
|
|
|
|
<button onclick="window.location.href='/'">Again...</button>
|
|
|
|
</div>
|
|
|
|
`
|
|
|
|
}
|
|
|
|
tmpl += `
|
2017-11-18 00:48:34 +00:00
|
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html>`
|
|
|
|
|
|
|
|
w.Header().Set(`Content-Type`, `text/html; charset=UTF-8`)
|
|
|
|
w.Header().Set(`Content-Length`, fmt.Sprintf("%d", len(tmpl)))
|
|
|
|
w.WriteHeader(200)
|
|
|
|
w.Write([]byte(tmpl))
|
|
|
|
}
|