@@ -6,7 +6,7 @@ import { VRef, getRawIdFrom } from "~/raem/ValaaReference";
66import type { MediaInfo , RetrieveMediaContent } from "~/prophet/api/Prophet" ;
77
88import { dumpObject , invariantifyString , thenChainEagerly , vdon } from "~/tools" ;
9- import { encodeDataURI } from "~/tools/html5/urlEncode " ;
9+ import { encodeDataURI } from "~/tools/html5/dataURI " ;
1010import type MediaDecoder from "~/tools/MediaDecoder" ;
1111import { stringFromUTF8ArrayBuffer } from "~/tools/textEncoding" ;
1212
@@ -26,7 +26,6 @@ export type MediaEntry = {
2626 mediaInfo : MediaInfo ,
2727 isPersisted : boolean ,
2828 isInMemory : boolean ,
29- nativeContent ?: any ,
3029} ;
3130
3231export type MediaLookup = {
@@ -45,7 +44,7 @@ export function _reprocessMedia (connection: ScribePartitionConnection, mediaEve
4544 let newEntry : MediaEntry ;
4645 if ( currentEntry ) {
4746 mediaInfo = { ...currentEntry . mediaInfo } ;
48- newEntry = { ...currentEntry , mediaInfo, nativeContent : undefined } ;
47+ newEntry = { ...currentEntry , mediaInfo } ;
4948 } else {
5049 if ( mediaId . isInherited ( ) ) {
5150 mediaInfo = { ...connection . _getMediaEntry ( mediaId ) . mediaInfo } ;
@@ -84,18 +83,13 @@ export function _reprocessMedia (connection: ScribePartitionConnection, mediaEve
8483
8584 const tryRetrieve = async ( ) => {
8685 try {
87- if ( mediaInfo . bvobId && connection . _prophet . tryGetCachedBvobContent ( mediaInfo . bvobId ) ) {
88- if ( currentEntry && ( currentEntry . mediaInfo . bvobId === mediaInfo . bvobId ) ) {
89- // content is in bvob buffer cache with equal bvobId. Reuse.
90- newEntry . nativeContent = currentEntry . nativeContent ;
91- }
92- } else if ( mediaInfo . bvobId || mediaInfo . sourceURL ) {
86+ if ( mediaInfo . bvobId && ! connection . _prophet . tryGetCachedBvobContent ( mediaInfo . bvobId ) ) {
9387 // TODO(iridian): Determine whether media content should be pre-cached or not.
94- const content = await retrieveMediaContent ( mediaId , mediaInfo ) ;
88+ const content = await retrieveMediaContent ( mediaId , { ...mediaInfo ,
89+ type : "application" , subtype : "octet-stream" , mime : "application/octet-stream" ,
90+ } ) ;
9591 if ( typeof content !== "undefined" ) {
96- newEntry . nativeContent = content ;
97- const { persistProcess } = connection . prepareBvob ( newEntry . nativeContent , mediaInfo ) ;
98- await persistProcess ;
92+ await connection . prepareBvob ( content , mediaInfo ) . persistProcess ;
9993 }
10094 }
10195 // Delays actual media info content update into a finalizer function so that recordTruth
@@ -177,39 +171,30 @@ export function _decodeBvobContent (scribe: Scribe, bvobInfo: BvobInfo,
177171 }
178172 return decodedContent ;
179173 } ,
180- ] , onError . bind ( scribe ) ) ;
174+ ] , onError ) ;
181175}
182176
183177export function _getMediaURL ( connection : ScribePartitionConnection , mediaInfo : MediaInfo ,
184- mediaEntry : MediaEntry ) : any {
185- let nativeContent ;
178+ mediaEntry : MediaEntry , onError : Function ) : any {
186179 // Only use cached in-memory nativeContent if its id matches the requested id.
187- if ( ( ! mediaInfo || ! mediaInfo . bvobId || ( mediaInfo . bvobId === mediaEntry . mediaInfo . bvobId ) )
188- && ( typeof mediaEntry . nativeContent !== "undefined" ) ) {
189- nativeContent = mediaEntry . nativeContent ;
190- } else {
191- const bvobId = ( mediaInfo && mediaInfo . bvobId ) || mediaEntry . mediaInfo . bvobId ;
192- if ( ! bvobId ) return undefined ;
193- const bufferCandidate = connection . _prophet . tryGetCachedBvobContent ( bvobId ) ;
194- nativeContent = bufferCandidate &&
195- _nativeObjectFromBufferAndMediaInfo ( bufferCandidate , mediaInfo || mediaEntry . mediaInfo ) ;
196- if ( bvobId === mediaEntry . mediaInfo . bvobId ) {
197- mediaEntry . nativeContent = nativeContent ;
198- }
180+ const bvobId = ( mediaInfo && mediaInfo . bvobId ) || mediaEntry . mediaInfo . bvobId ;
181+ const bvobInfo = connection . _prophet . _bvobLookup [ bvobId || "" ] ;
182+ // Media's with sourceURL or too large/missing bvobs will be handled by Oracle
183+ if ( ! bvobInfo || ! ( bvobInfo . byteLength <= 10000 ) ) return undefined ;
199184 }
200- if ( ( typeof nativeContent === "string" ) && nativeContent . length < 10000 ) {
201- // TODO(iridian): Is there a use case to create data URI's for json types?
202- const { type, subtype } = mediaInfo || mediaEntry . mediaInfo ;
203- return encodeDataURI ( nativeContent , type , subtype ) ;
204- }
205- // TODO(iridian): With systems that support Service Workers we return URL's which the service
206- // workers recognize and can redirect to 'smooth' IndexedDB accesses, see
185+ // TODO(iridian): With systems that support Service Workers we will eventually return URL's which
186+ // the service workers recognize and can redirect to 'smooth' IndexedDB accesses, see
207187 // https://gist.github.com/inexorabletash/687e7c5914049536f5a3
208188 // ( https://www.google.com/search?q=url+to+indexeddb )
209189 // Otherwise IndexedDB can't be accessed by the web pages directly, but horrible hacks must be
210190 // used like so:
211191 // https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB
212- return undefined ;
192+ return thenChainEagerly ( connection . _prophet . readBvobContent ( bvobInfo . bvobId ) ,
193+ ( buffer => {
194+ const { type, subtype } = mediaInfo || mediaEntry . mediaInfo ;
195+ return encodeDataURI ( buffer , type , subtype ) ;
196+ } )
197+ , onError ) ;
213198}
214199
215200// Returns the requested media content immediately as a native object if it is in in-memory cache.
@@ -218,24 +203,10 @@ export function _getMediaURL (connection: ScribePartitionConnection, mediaInfo:
218203// Otherwise throws an error.
219204export function _readMediaContent ( connection : ScribePartitionConnection , mediaInfo : MediaInfo ,
220205 mediaEntry : MediaEntry , onError : Function ) : any {
221- // Only return cached in-memory nativeContent if its id matches the requested id.
222206 const bvobId = mediaInfo . bvobId ;
223- if ( mediaEntry && ( typeof mediaEntry . nativeContent !== "undefined" )
224- && ( ! bvobId || ( bvobId === mediaEntry . mediaInfo . bvobId ) ) ) {
225- return mediaEntry . nativeContent ;
226- }
227207 if ( ! bvobId ) return undefined ;
228- return thenChainEagerly (
229- connection . _prophet . readBvobContent ( bvobId ) ,
230- ( buffer ) => {
231- if ( ! buffer ) return undefined ;
232- // nativeContent should go in favor of bvobInfo decoded contents
233- const nativeContent = _nativeObjectFromBufferAndMediaInfo ( buffer , mediaInfo ) ;
234- if ( mediaEntry && ( bvobId === mediaEntry . mediaInfo . bvobId ) ) {
235- mediaEntry . nativeContent = nativeContent ;
236- }
237- return nativeContent ;
238- } ,
208+ return thenChainEagerly ( connection . _prophet . readBvobContent ( bvobId ) ,
209+ ( buffer ) => ( ! buffer ? undefined : _nativeObjectFromBufferAndMediaInfo ( buffer , mediaInfo ) ) ,
239210 onError ) ;
240211}
241212
0 commit comments