package contented import ( "encoding/json" "fmt" "html" "log" "net/http" "os" "path/filepath" "strings" "time" ) func (this *Server) handlePreview(w http.ResponseWriter, fileIDList string) { fileIDs := strings.Split(fileIDList, `-`) tmpl := ` ` + html.EscapeString(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) { http.Error(w, "Not found", 404) return } log.Println(err.Error()) http.Error(w, "Internal error", 500) return } if m.MimeType == ALBUM_MIMETYPE { // Special handling for albums f, err := os.Open(filepath.Join(this.opts.DataDirectory, 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 } if len(childIDs) == 0 { log.Printf("Failed to parse album '%s': no entries in album", fileID) http.Error(w, "Internal error", 500) return } 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)) }