Add image thumbnail previews to the uploads page.
This commit is contained in:
parent
42fb7a2003
commit
2042c7520d
4
imageprocess/imageprocess.go
Normal file
4
imageprocess/imageprocess.go
Normal file
@ -0,0 +1,4 @@
|
||||
package imageprocess
|
||||
|
||||
type Processor struct {
|
||||
}
|
47
imageprocess/thumb.go
Normal file
47
imageprocess/thumb.go
Normal file
@ -0,0 +1,47 @@
|
||||
package imageprocess
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"image/png"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/tardisx/discord-auto-upload/upload"
|
||||
"golang.org/x/image/draw"
|
||||
)
|
||||
|
||||
const (
|
||||
thumbnailMaxX = 128
|
||||
thumbnailMaxY = 128
|
||||
)
|
||||
|
||||
func (ip *Processor) ThumbPNG(ul *upload.Upload, w io.Writer) error {
|
||||
file, err := os.Open(ul.OriginalFilename)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not open file: %s", err)
|
||||
}
|
||||
defer file.Close()
|
||||
im, _, err := image.Decode(file)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not decode file: %s", err)
|
||||
}
|
||||
|
||||
newXY := image.Point{}
|
||||
// test width
|
||||
if im.Bounds().Max.X/thumbnailMaxX > im.Bounds().Max.Y/thumbnailMaxY {
|
||||
newXY.X = thumbnailMaxX
|
||||
newXY.Y = im.Bounds().Max.Y / (im.Bounds().Max.X / thumbnailMaxX)
|
||||
} else {
|
||||
newXY.Y = thumbnailMaxY
|
||||
newXY.X = im.Bounds().Max.X / (im.Bounds().Max.Y / thumbnailMaxY)
|
||||
}
|
||||
|
||||
dst := image.NewRGBA(image.Rect(0, 0, newXY.X, newXY.Y))
|
||||
draw.BiLinear.Scale(dst, dst.Rect, im, im.Bounds(), draw.Over, nil)
|
||||
|
||||
png.Encode(w, dst)
|
||||
|
||||
return nil
|
||||
|
||||
}
|
@ -98,6 +98,15 @@ func (u *Uploader) Upload() {
|
||||
}
|
||||
}
|
||||
|
||||
func (u *Uploader) UploadById(id int32) *Upload {
|
||||
for _, anUpload := range u.Uploads {
|
||||
if anUpload.Id == int32(id) {
|
||||
return anUpload
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *Upload) processUpload() error {
|
||||
daulog.SendLog(fmt.Sprintf("Uploading: %s", u.OriginalFilename), daulog.LogTypeInfo)
|
||||
|
||||
|
@ -11,6 +11,7 @@
|
||||
<tr>
|
||||
<th>filename</th>
|
||||
<th>actions</th>
|
||||
<th> </th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@ -20,8 +21,8 @@
|
||||
<td>
|
||||
<button @click="start_upload(ul.id)" type="button" class="btn btn-primary">upload</button>
|
||||
<button @click="skip_upload(ul.id)" type="button" class="btn btn-primary">reject</button>
|
||||
|
||||
</td>
|
||||
<td><img :src="'/rest/image/'+ul.id+'/thumb'"></td>
|
||||
</tr>
|
||||
</template>
|
||||
|
||||
@ -35,6 +36,7 @@
|
||||
<tr>
|
||||
<th>filename</th>
|
||||
<th>actions</th>
|
||||
<th> </th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@ -45,6 +47,7 @@
|
||||
<td>
|
||||
<button @click="start_upload(ul.id)" type="button" class="btn btn-primary">upload</button>
|
||||
</td>
|
||||
<td><img :src="'/rest/image/'+ul.id+'/thumb'"></td>
|
||||
</tr>
|
||||
</template>
|
||||
|
||||
@ -58,6 +61,7 @@
|
||||
<tr>
|
||||
<th>filename</th>
|
||||
<th>state</th>
|
||||
<th> </th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@ -66,6 +70,7 @@
|
||||
<tr>
|
||||
<td x-text="ul.original_file"></td>
|
||||
<td x-text="ul.state"></td>
|
||||
<td><img :src="'/rest/image/'+ul.id+'/thumb'"></td>
|
||||
</tr>
|
||||
</template>
|
||||
|
||||
|
@ -16,6 +16,7 @@ import (
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/tardisx/discord-auto-upload/config"
|
||||
"github.com/tardisx/discord-auto-upload/imageprocess"
|
||||
daulog "github.com/tardisx/discord-auto-upload/log"
|
||||
"github.com/tardisx/discord-auto-upload/upload"
|
||||
"github.com/tardisx/discord-auto-upload/version"
|
||||
@ -168,6 +169,30 @@ func (ws *WebService) getUploads(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte(text))
|
||||
}
|
||||
|
||||
func (ws *WebService) imageThumb(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
w.Header().Set("Content-Type", "image/png")
|
||||
processor := imageprocess.Processor{}
|
||||
|
||||
vars := mux.Vars(r)
|
||||
id, err := strconv.ParseInt(vars["id"], 10, 32)
|
||||
if err != nil {
|
||||
returnJSONError(w, "bad id")
|
||||
return
|
||||
}
|
||||
|
||||
ul := ws.Uploader.UploadById(int32(id))
|
||||
if ul == nil {
|
||||
returnJSONError(w, "bad id")
|
||||
return
|
||||
}
|
||||
err = processor.ThumbPNG(ul, w)
|
||||
if err != nil {
|
||||
returnJSONError(w, "could not create thumb")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (ws *WebService) modifyUpload(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
@ -182,26 +207,28 @@ func (ws *WebService) modifyUpload(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
for _, anUpload := range ws.Uploader.Uploads {
|
||||
if anUpload.Id == int32(id) {
|
||||
if anUpload.State == upload.StatePending {
|
||||
if change == "start" {
|
||||
anUpload.State = upload.StateQueued
|
||||
res := StartUploadResponse{Success: true, Message: "upload queued"}
|
||||
resString, _ := json.Marshal(res)
|
||||
w.Write(resString)
|
||||
return
|
||||
} else if change == "skip" {
|
||||
anUpload.State = upload.StateSkipped
|
||||
res := StartUploadResponse{Success: true, Message: "upload skipped"}
|
||||
resString, _ := json.Marshal(res)
|
||||
w.Write(resString)
|
||||
return
|
||||
} else {
|
||||
returnJSONError(w, "bad change type")
|
||||
return
|
||||
}
|
||||
}
|
||||
anUpload := ws.Uploader.UploadById(int32(id))
|
||||
if anUpload == nil {
|
||||
returnJSONError(w, "bad id")
|
||||
return
|
||||
}
|
||||
|
||||
if anUpload.State == upload.StatePending {
|
||||
if change == "start" {
|
||||
anUpload.State = upload.StateQueued
|
||||
res := StartUploadResponse{Success: true, Message: "upload queued"}
|
||||
resString, _ := json.Marshal(res)
|
||||
w.Write(resString)
|
||||
return
|
||||
} else if change == "skip" {
|
||||
anUpload.State = upload.StateSkipped
|
||||
res := StartUploadResponse{Success: true, Message: "upload skipped"}
|
||||
resString, _ := json.Marshal(res)
|
||||
w.Write(resString)
|
||||
return
|
||||
} else {
|
||||
returnJSONError(w, "bad change type")
|
||||
return
|
||||
}
|
||||
}
|
||||
res := StartUploadResponse{Success: false, Message: "upload does not exist, or already queued"}
|
||||
@ -211,7 +238,6 @@ func (ws *WebService) modifyUpload(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
returnJSONError(w, "bad request")
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
@ -223,6 +249,8 @@ func (ws *WebService) StartWebServer() {
|
||||
r.HandleFunc("/rest/uploads", ws.getUploads)
|
||||
r.HandleFunc("/rest/upload/{id:[0-9]+}/{change}", ws.modifyUpload)
|
||||
|
||||
r.HandleFunc("/rest/image/{id:[0-9]+}/thumb", ws.imageThumb)
|
||||
|
||||
r.HandleFunc("/rest/config", ws.handleConfig)
|
||||
r.PathPrefix("/").HandlerFunc(ws.getStatic)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user