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