package contented import ( "context" "encoding/json" "fmt" "html" "log" "net/http" "os" "strings" "time" ) func (this *Server) handlePreview(ctx context.Context, w http.ResponseWriter, fileIDList string) { fileIDs := strings.Split(fileIDList, `-`) // 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)) } tmpl := ` ` + html.EscapeString(specialTitle+" | "+this.opts.ServerPublicProperties.AppTitle) + ` ` if len(this.opts.ServerPublicProperties.CanonicalBaseURL) > 0 { tmpl += ` ` } for _, fileID := range fileIDs { tmpl += ` ` } tmpl += `
` for _, fileID := range fileIDs { m, err := this.Metadata(fileID) if err != nil { if os.IsNotExist(err) { // If this is just one image out of many, show a 404 box and continue to show the other entries // But if this is only a single image requested, abandon the whole pageload if len(fileIDs) == 1 { http.Error(w, "Not found", 404) return } tmpl += `
Requested ID ` + html.EscapeString(fileID) + ` not found in storage (404)
` continue } log.Println(err.Error()) http.Error(w, "Internal error", 500) return } if m.MimeType == ALBUM_MIMETYPE { // Special handling for albums f, err := this.ReadFile(ctx, m.FileHash) 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 } albumThumb := `/nothumb_340.png` if len(childIDs) > 0 { albumThumb = `/thumb/m/` + childIDs[0] } tmpl += `
` + fmt.Sprintf("%d", len(childIDs)) + ` image(s)
Name: ` + html.EscapeString(m.Filename) + `
Hash: hover
File type: Album
Size: ` + fmt.Sprintf("%d", len(childIDs)) + ` image(s)
Uploader: ` + html.EscapeString(m.UploadIP) + `
Uploaded at: ` + html.EscapeString(m.UploadTime.Format(time.RFC3339)) + `
` } else { tmpl += `
Name: ` + html.EscapeString(m.Filename) + `
Hash: hover
File type: ` + html.EscapeString(m.MimeType) + `
Size: ` + html.EscapeString(fmt.Sprintf("%d", m.FileSize)) + `
Uploader: ` + html.EscapeString(m.UploadIP) + `
Uploaded at: ` + html.EscapeString(m.UploadTime.Format(time.RFC3339)) + `
` } } if this.opts.EnableHomepage { tmpl += `
` } tmpl += `
` 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)) }