|
| 1 | +package content |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + |
| 9 | + provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" |
| 10 | + libregraph "github.com/opencloud-eu/libre-graph-api-go" |
| 11 | +) |
| 12 | + |
| 13 | +// motionPhotoVideoSignatureLen is the number of trailing bytes we read to confirm |
| 14 | +// an actual video is present. Enough to cover the ISO base media (MP4) box size |
| 15 | +// and the "ftyp" box type at bytes [4:8]. |
| 16 | +const motionPhotoVideoSignatureLen = 12 |
| 17 | + |
| 18 | +// getMotionPhoto reads Google Motion Photo XMP, which Tika exposes under the |
| 19 | +// canonical Camera/Container prefixes. It covers both the current MotionPhoto |
| 20 | +// scheme and the legacy MicroVideo scheme. videoSize (the embedded video's byte |
| 21 | +// length, needed to range-fetch it) is required, so the facet is dropped without it. |
| 22 | +func (t Tika) getMotionPhoto(meta map[string][]string) *libregraph.MotionPhoto { |
| 23 | + var motionPhoto *libregraph.MotionPhoto |
| 24 | + initMotionPhoto := func() { |
| 25 | + if motionPhoto == nil { |
| 26 | + motionPhoto = libregraph.NewMotionPhoto() |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + if v, err := getFirstValue(meta, "Camera:MotionPhotoVersion", "Camera:MicroVideoVersion"); err == nil { |
| 31 | + if i, err := strconv.ParseInt(v, 0, 32); err == nil { |
| 32 | + initMotionPhoto() |
| 33 | + motionPhoto.SetVersion(int32(i)) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + if v, err := getFirstValue(meta, "Camera:MotionPhotoPresentationTimestampUs", "Camera:MicroVideoPresentationTimestampUs"); err == nil { |
| 38 | + if i, err := strconv.ParseInt(v, 0, 64); err == nil { |
| 39 | + initMotionPhoto() |
| 40 | + motionPhoto.SetPresentationTimestampUs(i) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + if size, ok := motionPhotoVideoSize(meta); ok { |
| 45 | + initMotionPhoto() |
| 46 | + motionPhoto.SetVideoSize(size) |
| 47 | + } |
| 48 | + |
| 49 | + if motionPhoto == nil || !motionPhoto.HasVideoSize() { |
| 50 | + return nil |
| 51 | + } |
| 52 | + return motionPhoto |
| 53 | +} |
| 54 | + |
| 55 | +// motionPhotoVideoSize returns the embedded video's byte length. Legacy files |
| 56 | +// carry it as the MicroVideo offset (bytes from EOF to the video start, which |
| 57 | +// equals its length); current files expose it as the length of the Container |
| 58 | +// item whose semantic is "MotionPhoto". |
| 59 | +func motionPhotoVideoSize(meta map[string][]string) (int64, bool) { |
| 60 | + if v, err := getFirstValue(meta, "Camera:MicroVideoOffset"); err == nil { |
| 61 | + if i, err := strconv.ParseInt(v, 0, 64); err == nil { |
| 62 | + return i, true |
| 63 | + } |
| 64 | + } |
| 65 | + for k, vals := range meta { |
| 66 | + if !strings.HasSuffix(k, "/Item:Semantic") || len(vals) == 0 || vals[0] != "MotionPhoto" { |
| 67 | + continue |
| 68 | + } |
| 69 | + if v, err := getFirstValue(meta, strings.TrimSuffix(k, "/Item:Semantic")+"/Item:Length"); err == nil { |
| 70 | + if i, err := strconv.ParseInt(v, 0, 64); err == nil { |
| 71 | + return i, true |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + return 0, false |
| 76 | +} |
| 77 | + |
| 78 | +// looksLikeMP4 reports whether buf begins with an ISO base media (MP4/QuickTime) |
| 79 | +// "ftyp" box, which Google Motion Photo and legacy MicroVideo clips start with. |
| 80 | +func looksLikeMP4(buf []byte) bool { |
| 81 | + return len(buf) >= 8 && string(buf[4:8]) == "ftyp" |
| 82 | +} |
| 83 | + |
| 84 | +// motionPhotoHasVideo confirms that the file actually contains the embedded video |
| 85 | +// the XMP advertises. A photos.google.com share strips the appended video but |
| 86 | +// keeps the XMP, which would otherwise make us expose an unplayable facet. The |
| 87 | +// video is appended at the end, so it starts at fileSize-videoSize; we read a few |
| 88 | +// bytes there and require an MP4 signature. |
| 89 | +func (t Tika) motionPhotoHasVideo(ctx context.Context, ri *provider.ResourceInfo, videoSize int64) bool { |
| 90 | + size := int64(ri.GetSize()) |
| 91 | + if videoSize <= 0 || videoSize >= size { |
| 92 | + return false |
| 93 | + } |
| 94 | + |
| 95 | + rc, err := t.RetrieveRange(ctx, ri.GetId(), size-videoSize, motionPhotoVideoSignatureLen) |
| 96 | + if err != nil { |
| 97 | + t.logger.Debug().Err(err).Interface("ResourceID", ri.GetId()).Msg("could not read motion photo video header, dropping facet") |
| 98 | + return false |
| 99 | + } |
| 100 | + defer rc.Close() |
| 101 | + |
| 102 | + buf := make([]byte, motionPhotoVideoSignatureLen) |
| 103 | + n, _ := io.ReadFull(rc, buf) |
| 104 | + return looksLikeMP4(buf[:n]) |
| 105 | +} |
0 commit comments