@@ -3,7 +3,7 @@ import { saveAs } from 'file-saver'
33import JSZip from 'jszip'
44import { createContext , ReactChild , ReactElement } from 'react'
55
6- import { BEE_HOSTS , META_FILE_NAME , POSTAGE_STAMP , PREVIEW_FILE_NAME } from '../constants'
6+ import { BEE_HOSTS , META_FILE_NAME , POSTAGE_STAMP } from '../constants'
77import { detectIndexHtml } from '../utils/file'
88import { ManifestJs } from '../utils/manifest'
99import { packageFile } from '../utils/SwarmFile'
@@ -45,37 +45,21 @@ function hashToIndex(hash: Reference | string) {
4545}
4646
4747export function Provider ( { children } : Props ) : ReactElement {
48- const upload = async ( files : SwarmFile [ ] , metadata : Metadata , preview ?: Blob ) => {
48+ const upload = async ( files : SwarmFile [ ] , metadata : Metadata ) => {
4949 const fls = files . map ( packageFile ) // Apart from packaging, this is needed to not modify the original files array as it can trigger effects
5050 let indexDocument = files . length === 1 ? files [ 0 ] . name : detectIndexHtml ( files ) || undefined
5151
5252 // TODO: Remove once this is fixed in bee-js https://github.com/ethersphere/bee-js/issues/531
5353 if ( indexDocument ) indexDocument = unescape ( encodeURIComponent ( indexDocument ) )
54- const lastModified = files [ 0 ] . lastModified
55-
56- // We want to store only some metadata
57- const mtd : SwarmMetadata = {
58- name : metadata . name ,
59- size : metadata . size ,
60- }
61-
62- // Type of the file only makes sense for a single file
63- if ( files . length === 1 ) mtd . type = metadata . type
6454
55+ const lastModified = files [ 0 ] . lastModified
6556 fls . push (
66- new File ( [ JSON . stringify ( mtd ) ] , META_FILE_NAME , {
57+ new File ( [ JSON . stringify ( metadata ) ] , META_FILE_NAME , {
6758 type : 'application/json' ,
6859 lastModified,
6960 } ) ,
7061 )
7162
72- if ( preview ) {
73- const previewFile = new File ( [ preview ] , PREVIEW_FILE_NAME , {
74- lastModified,
75- } )
76- fls . push ( previewFile )
77- }
78-
7963 const { reference } = await randomBee . uploadFiles ( POSTAGE_STAMP , fls , { indexDocument } )
8064 const hashIndex = hashToIndex ( reference )
8165
@@ -110,51 +94,48 @@ export function Provider({ children }: Props): ReactElement {
11094 preview ?: string
11195 entries : Record < string , string >
11296 } > => {
113- let metadata : Metadata | undefined = { size : 0 , type : 'unknown' , name : hash , isWebsite : false }
97+ let metadata : Metadata | undefined
11498 let entries : Record < string , string > = { }
115- let preview
99+ let indexDocument : string | null = null
100+ let preview : string | undefined
116101
117102 const hashIndex = hashToIndex ( hash )
118103 const bee = new Bee ( BEE_HOSTS [ hashIndex ] )
119104
120- try {
121- const mtdt = await bee . downloadFile ( hash , META_FILE_NAME )
122- metadata = { ...metadata , ...( JSON . parse ( mtdt . data . text ( ) ) as Metadata ) }
123- } catch ( e ) { } // eslint-disable-line no-empty
124-
125105 try {
126106 const manifestJs = new ManifestJs ( bee )
127107 const isManifest = await manifestJs . isManifest ( hash )
128108
129109 if ( ! isManifest ) throw Error ( 'The specified hash does not contain valid content.' )
130- entries = await manifestJs . getHashes ( hash )
131-
132- const indexDocument = await manifestJs . getIndexDocumentPath ( hash )
133-
134- metadata . isWebsite = Boolean ( indexDocument && / .* \. h t m l ? $ / i. test ( indexDocument ) ) // only html documents can be websites
135110
136- preview = getPreview ( entries , hash )
137-
138- // Erase the files added by the gateway
139- delete entries [ META_FILE_NAME ]
140- delete entries [ PREVIEW_FILE_NAME ]
141-
142- metadata . count = Object . keys ( entries ) . length
143-
144- if ( metadata . count > 1 ) metadata . type = 'folder'
111+ entries = await manifestJs . getHashes ( hash , { exclude : [ META_FILE_NAME ] } )
112+ indexDocument = await manifestJs . getIndexDocumentPath ( hash )
145113 } catch ( e ) { } // eslint-disable-line no-empty
146114
147- metadata . hash = hash
148-
149- return { metadata, preview, entries }
150- }
115+ try {
116+ const remoteMetadata = await bee . downloadFile ( hash , META_FILE_NAME )
117+ const formattedMetadata = JSON . parse ( remoteMetadata . data . text ( ) ) as Metadata
151118
152- const getPreview = ( entries : Record < string , string > , hash : Reference | string ) : string | undefined => {
153- if ( ! entries [ PREVIEW_FILE_NAME ] ) return
119+ if ( formattedMetadata . isVideo || formattedMetadata . isImage ) {
120+ preview = `${ BEE_HOSTS [ hashIndex ] } /bzz/${ hash } `
121+ }
154122
155- const hashIndex = hashToIndex ( hash )
123+ metadata = { ...formattedMetadata , hash }
124+ } catch ( e ) {
125+ const count = Object . keys ( entries ) . length
126+ metadata = {
127+ hash,
128+ type : count > 1 ? 'folder' : 'unknown' ,
129+ name : hash ,
130+ count,
131+ isWebsite : Boolean ( indexDocument && / .* \. h t m l ? $ / i. test ( indexDocument ) ) ,
132+ isVideo : Boolean ( indexDocument && / .* \. ( m p 4 | w e b m | o g g | m p 3 | o g g | w a v ) $ / i. test ( indexDocument ) ) ,
133+ isImage : Boolean ( indexDocument && / .* \. ( j p g | j p e g | p n g | g i f | w e b p | s v g ) $ / i. test ( indexDocument ) ) ,
134+ // naive assumption based on indexDocument, we don't want to donwload the whole manifest
135+ }
136+ }
156137
157- return ` ${ BEE_HOSTS [ hashIndex ] } /bzz/ ${ hash } / ${ PREVIEW_FILE_NAME } `
138+ return { metadata , preview , entries }
158139 }
159140
160141 const getChunk = ( hash : Reference | string ) : Promise < Data > => {
0 commit comments