More changes to uploads:
- Have got upload images appearing in the post list - Allowed for deleting uploads - Allowed for seeing the upload progress - Fixed the setting of upload properties like the MIME type - Removed the stripe exif logic with just re-encoding PNGs and JPEGs by loading them and saving them
This commit is contained in:
parent
d0cebe6564
commit
199ff9feb9
21 changed files with 471 additions and 65 deletions
|
|
@ -1,31 +1,57 @@
|
|||
package uploadfiles
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"emperror.dev/errors"
|
||||
"github.com/barasher/go-exiftool"
|
||||
"github.com/disintegration/imaging"
|
||||
"lmika.dev/lmika/weiro/models"
|
||||
)
|
||||
|
||||
const (
|
||||
applicationOctetStream = "application/octet-stream"
|
||||
)
|
||||
|
||||
var supportedRencodableImageTypes = map[string]bool{
|
||||
"image/jpeg": true,
|
||||
"image/png": true,
|
||||
applicationOctetStream: true,
|
||||
}
|
||||
var supportedReencoableExtensions = map[string]bool{
|
||||
".jpg": true,
|
||||
".jpeg": true,
|
||||
".png": true,
|
||||
}
|
||||
|
||||
func (p *Provider) StripeEXIFData(site models.Site, up models.Upload) error {
|
||||
uploadFilename := p.uploadFileName(site, up)
|
||||
|
||||
et, err := exiftool.NewExiftool()
|
||||
if !supportedRencodableImageTypes[up.MIMEType] {
|
||||
return errors.New("unsupported image format: " + up.MIMEType)
|
||||
}
|
||||
if up.MIMEType == applicationOctetStream && !supportedReencoableExtensions[filepath.Ext(uploadFilename)] {
|
||||
return errors.New("unsupported image format")
|
||||
}
|
||||
|
||||
img, err := imaging.Open(uploadFilename)
|
||||
if err != nil {
|
||||
return err
|
||||
return errors.Wrap(err, "failed to open image file")
|
||||
}
|
||||
defer et.Close()
|
||||
|
||||
fileInfos := et.ExtractMetadata(uploadFilename)
|
||||
if len(fileInfos) == 0 {
|
||||
return errors.New("no exif data found")
|
||||
tmpName := strings.TrimSuffix(uploadFilename, filepath.Ext(uploadFilename)) + ".tmp." + filepath.Ext(uploadFilename)
|
||||
if err := imaging.Save(img, tmpName); err != nil {
|
||||
return errors.Wrap(err, "failed to save image file")
|
||||
}
|
||||
fileInfo := fileInfos[0]
|
||||
fileInfo.ClearAll()
|
||||
|
||||
fileOut := []exiftool.FileMetadata{fileInfo}
|
||||
et.WriteMetadata(fileOut)
|
||||
if fileOut[0].Err != nil {
|
||||
return fileOut[0].Err
|
||||
if err := os.Remove(uploadFilename); err != nil {
|
||||
_ = os.Remove(tmpName)
|
||||
return errors.Wrap(err, "failed to remove image file")
|
||||
}
|
||||
|
||||
if err := os.Rename(tmpName, uploadFilename); err != nil {
|
||||
return errors.Wrap(err, "failed to rename image file")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -37,6 +37,17 @@ func (p *Provider) OpenUpload(site models.Site, up models.Upload) (io.ReadCloser
|
|||
return os.Open(fullPath)
|
||||
}
|
||||
|
||||
func (p *Provider) DeleteUpload(site models.Site, up models.Upload) error {
|
||||
fullPath := p.uploadFileName(site, up)
|
||||
if err := os.Remove(fullPath); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Provider) UploadDir(site models.Site) string {
|
||||
return filepath.Join(p.baseDir, site.GUID)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue