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

6
.idea/workspace.xml generated
View File

@ -4,7 +4,7 @@
<option name="autoReloadType" value="ALL" /> <option name="autoReloadType" value="ALL" />
</component> </component>
<component name="ChangeListManager"> <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$/.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" /> <change beforePath="$PROJECT_DIR$/pkg/internal/services/analyzer.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/analyzer.go" afterDir="false" />
</list> </list>
@ -117,7 +117,6 @@
</option> </option>
</component> </component>
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<MESSAGE value=":sparkles: Attachment pool basis" />
<MESSAGE value=":sparkles: Attachment has pool" /> <MESSAGE value=":sparkles: Attachment has pool" />
<MESSAGE value=":sparkles: Pool clean by lifecycle config" /> <MESSAGE value=":sparkles: Pool clean by lifecycle config" />
<MESSAGE value=":recycle: Split mark and delete file" /> <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=":bug: Fix account_id where clause causing indexing issue" />
<MESSAGE value=":sparkles: Attachment API can edit metadata" /> <MESSAGE value=":sparkles: Attachment API can edit metadata" />
<MESSAGE value=":lock: Fix Attachment will contains GPS information" /> <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>
<component name="VgoProject"> <component name="VgoProject">
<settings-migrated>true</settings-migrated> <settings-migrated>true</settings-migrated>

View File

@ -6,6 +6,7 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"github.com/barasher/go-exiftool" "github.com/barasher/go-exiftool"
"github.com/samber/lo"
"image" "image"
"io" "io"
"os" "os"
@ -128,6 +129,13 @@ func AnalyzeAttachment(file models.Attachment) error {
return fmt.Errorf("attachment doesn't exists in temporary storage: %v", err) 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] { switch strings.SplitN(file.MimeType, "/", 2)[0] {
case "image": case "image":
// Dealing with image // Dealing with image
@ -161,7 +169,7 @@ func AnalyzeAttachment(file models.Attachment) error {
for k, _ := range data.Fields { for k, _ := range data.Fields {
if strings.HasPrefix(k, "GPS") { if strings.HasPrefix(k, "GPS") {
data.Clear(k) data.Clear(k)
} else { } else if lo.Contains(exifWhitelist, k) {
file.Metadata["exif"].(map[string]any)[k] = data.Fields[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 { for k, _ := range data.Fields {
if strings.HasPrefix(k, "GPS") { if strings.HasPrefix(k, "GPS") {
data.Clear(k) data.Clear(k)
} else { } else if lo.Contains(exifWhitelist, k) {
file.Metadata["exif"].(map[string]any)[k] = data.Fields[k] file.Metadata["exif"].(map[string]any)[k] = data.Fields[k]
} }
} }