fix(search): truncate oversized files for Tika and fix ISO extraction
Send truncated stream via io.LimitReader instead of skipping Tika entirely for files exceeding ContentExtractionSizeLimit. EXIF metadata lives in the JPEG header, so even a truncated read extracts all photo data from large files like Pixel Motion Photos. Also prefer the standard exif:IsoSpeedRatings field over the Canon- specific "Base ISO", falling back to the latter when absent. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
95ba2da3fb
commit
6973f7db93
2 files changed
+64
-8
No files matched your search
@@ -3,6 +3,7 @@ package content
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -62,11 +63,6 @@ func (t Tika) Extract(ctx context.Context, ri *provider.ResourceInfo) (Document,
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
if ri.Size > t.ContentExtractionSizeLimit {
|
||||
t.logger.Info().Interface("ResourceID", ri.Id).Str("Name", ri.Name).Msg("file exceeds content extraction size limit. skipping.")
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
if ri.Type != provider.ResourceType_RESOURCE_TYPE_FILE {
|
||||
return doc, nil
|
||||
}
|
||||
@@ -77,7 +73,12 @@ func (t Tika) Extract(ctx context.Context, ri *provider.ResourceInfo) (Document,
|
||||
}
|
||||
defer data.Close()
|
||||
|
||||
metas, err := t.tika.MetaRecursive(ctx, data)
|
||||
var reader io.Reader = data
|
||||
if ri.Size > t.ContentExtractionSizeLimit {
|
||||
reader = io.LimitReader(data, int64(t.ContentExtractionSizeLimit))
|
||||
}
|
||||
|
||||
metas, err := t.tika.MetaRecursive(ctx, reader)
|
||||
if err != nil {
|
||||
return doc, err
|
||||
}
|
||||
@@ -200,7 +201,12 @@ func (t Tika) getPhoto(meta map[string][]string) *libregraph.Photo {
|
||||
}
|
||||
}
|
||||
|
||||
if v, err := getFirstValue(meta, "Base ISO"); err == nil {
|
||||
if v, err := getFirstValue(meta, "exif:IsoSpeedRatings"); err == nil {
|
||||
if i, err := strconv.ParseInt(v, 0, 32); err == nil {
|
||||
initPhoto()
|
||||
photo.SetIso(int32(i))
|
||||
}
|
||||
} else if v, err := getFirstValue(meta, "Base ISO"); err == nil {
|
||||
if i, err := strconv.ParseInt(v, 0, 32); err == nil {
|
||||
initPhoto()
|
||||
photo.SetIso(int32(i))
|
||||
|
||||
@@ -191,7 +191,7 @@ var _ = Describe("Tika", func() {
|
||||
"exif:ExposureTime": "0.001",
|
||||
"exif:FNumber": "1.8",
|
||||
"exif:FocalLength": "50",
|
||||
"Base ISO": "100",
|
||||
"exif:IsoSpeedRatings": "100",
|
||||
"tiff:Orientation": "1",
|
||||
"exif:DateTimeOriginal": "2018-01-01T12:34:56"
|
||||
}
|
||||
@@ -216,6 +216,56 @@ var _ = Describe("Tika", func() {
|
||||
Expect(photo.TakenDateTime).To(Equal(libregraph.PtrTime(time.Date(2018, 1, 1, 12, 34, 56, 0, time.UTC))))
|
||||
})
|
||||
|
||||
It("falls back to Base ISO when exif:IsoSpeedRatings is absent", func() {
|
||||
fullResponse = `[
|
||||
{
|
||||
"tiff:Make": "Canon",
|
||||
"Base ISO": "200"
|
||||
}
|
||||
]`
|
||||
doc, err := tika.Extract(context.TODO(), &provider.ResourceInfo{
|
||||
Type: provider.ResourceType_RESOURCE_TYPE_FILE,
|
||||
Size: 1,
|
||||
})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
Expect(doc.Photo).ToNot(BeNil())
|
||||
Expect(doc.Photo.Iso).To(Equal(libregraph.PtrInt32(200)))
|
||||
})
|
||||
|
||||
It("extracts metadata from oversized files using truncated stream", func() {
|
||||
fullResponse = `[
|
||||
{
|
||||
"tiff:Make": "Google",
|
||||
"tiff:Model": "Pixel 7",
|
||||
"exif:FNumber": "1.85",
|
||||
"exif:FocalLength": "6.81",
|
||||
"Base ISO": "58",
|
||||
"tiff:Orientation": "1",
|
||||
"exif:DateTimeOriginal": "2024-06-15T14:30:00",
|
||||
"tiff:ImageWidth": "4080",
|
||||
"tiff:ImageLength": "3072"
|
||||
}
|
||||
]`
|
||||
|
||||
tika.ContentExtractionSizeLimit = 10
|
||||
|
||||
doc, err := tika.Extract(context.TODO(), &provider.ResourceInfo{
|
||||
Type: provider.ResourceType_RESOURCE_TYPE_FILE,
|
||||
Size: 8_000_000,
|
||||
})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
Expect(doc.Photo).ToNot(BeNil())
|
||||
Expect(doc.Photo.CameraMake).To(Equal(libregraph.PtrString("Google")))
|
||||
Expect(doc.Photo.CameraModel).To(Equal(libregraph.PtrString("Pixel 7")))
|
||||
Expect(doc.Photo.TakenDateTime).ToNot(BeNil())
|
||||
|
||||
Expect(doc.Image).ToNot(BeNil())
|
||||
Expect(doc.Image.Width).To(Equal(libregraph.PtrInt32(4080)))
|
||||
Expect(doc.Image.Height).To(Equal(libregraph.PtrInt32(3072)))
|
||||
})
|
||||
|
||||
It("removes stop words", func() {
|
||||
body = "body to test stop words!!! against almost everyone"
|
||||
language = "en"
|
||||
|
||||
Reference in new issue
Block a user