Use exif whitelist to prevent produce garbage data

This commit is contained in:
LittleSheep 2024-10-16 22:12:35 +08:00
parent 831717fabc
commit e123c82c2b
2 changed files with 13 additions and 5 deletions

View File

@ -4,7 +4,7 @@
<option name="autoReloadType" value="ALL" />
</component>
<component name="ChangeListManager">
<list default="true" id="18dd0d68-b4b8-40db-9734-9119b5c848bd" name="更改" comment=":lock: Fix Attachment will contains GPS information">
<list default="true" id="18dd0d68-b4b8-40db-9734-9119b5c848bd" name="更改" comment=":sparkles: Save EXIF into file metadata">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/services/analyzer.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/analyzer.go" afterDir="false" />
</list>
@ -117,7 +117,6 @@
</option>
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value=":sparkles: Attachment pool basis" />
<MESSAGE value=":sparkles: Attachment has pool" />
<MESSAGE value=":sparkles: Pool clean by lifecycle config" />
<MESSAGE value=":recycle: Split mark and delete file" />
@ -142,7 +141,8 @@
<MESSAGE value=":bug: Fix account_id where clause causing indexing issue" />
<MESSAGE value=":sparkles: Attachment API can edit metadata" />
<MESSAGE value=":lock: Fix Attachment will contains GPS information" />
<option name="LAST_COMMIT_MESSAGE" value=":lock: Fix Attachment will contains GPS information" />
<MESSAGE value=":sparkles: Save EXIF into file metadata" />
<option name="LAST_COMMIT_MESSAGE" value=":sparkles: Save EXIF into file metadata" />
</component>
<component name="VgoProject">
<settings-migrated>true</settings-migrated>

View File

@ -6,6 +6,7 @@ import (
"encoding/hex"
"fmt"
"github.com/barasher/go-exiftool"
"github.com/samber/lo"
"image"
"io"
"os"
@ -128,6 +129,13 @@ func AnalyzeAttachment(file models.Attachment) error {
return fmt.Errorf("attachment doesn't exists in temporary storage: %v", err)
}
exifWhitelist := []string{
"Model", "ShutterSpeed", "ISO", "Megapixels", "Aperture",
"ColorSpace", "ColorTemperature", "ColorTone", "Contrast",
"ExposureTime", "FNumber", "FocalLength", "Flash", "HDREffect",
"LensModel",
}
switch strings.SplitN(file.MimeType, "/", 2)[0] {
case "image":
// Dealing with image
@ -161,7 +169,7 @@ func AnalyzeAttachment(file models.Attachment) error {
for k, _ := range data.Fields {
if strings.HasPrefix(k, "GPS") {
data.Clear(k)
} else {
} else if lo.Contains(exifWhitelist, k) {
file.Metadata["exif"].(map[string]any)[k] = data.Fields[k]
}
}
@ -203,7 +211,7 @@ func AnalyzeAttachment(file models.Attachment) error {
for k, _ := range data.Fields {
if strings.HasPrefix(k, "GPS") {
data.Clear(k)
} else {
} else if lo.Contains(exifWhitelist, k) {
file.Metadata["exif"].(map[string]any)[k] = data.Fields[k]
}
}