@@ -85,121 +85,121 @@ export const parseArrayLikeToDetails = async (
8585 cid : string ,
8686 onProgress ?: onProgressCallback
8787) : Promise < IPFSContentDetails > => {
88- try {
89- if ( ! content || ! content ?. result ) {
90- return {
91- gateway : true ,
92- text : cid . toString ( ) ,
93- cid,
94- } ;
95- }
88+ // try {
89+ if ( ! content || ! content ?. result ) {
90+ return {
91+ gateway : true ,
92+ text : cid . toString ( ) ,
93+ cid,
94+ } ;
95+ }
9696
97- const { result, meta } = content ;
97+ const { result, meta } = content ;
9898
99- const mime = meta ?. mime ;
99+ const mime = meta ?. mime ;
100100
101- if ( ! mime ) {
102- return {
103- cid,
104- gateway : true ,
105- text : `Can't detect MIME for ${ cid . toString ( ) } ` ,
106- } ;
107- }
108- const contentType = mimeToBaseContentType ( mime ) ;
109- const contentCid = content . cid ;
110-
111- const response : IPFSContentDetails = {
112- link : `/ipfs/${ cid } ` ,
113- gateway : false ,
114- cid : contentCid ,
115- type : contentType ,
101+ if ( ! mime ) {
102+ return {
103+ cid,
104+ gateway : true ,
105+ text : `Can't detect MIME for ${ cid . toString ( ) } ` ,
116106 } ;
107+ }
108+ const contentType = mimeToBaseContentType ( mime ) ;
109+ const contentCid = content . cid ;
110+
111+ const response : IPFSContentDetails = {
112+ link : `/ipfs/${ cid } ` ,
113+ gateway : false ,
114+ cid : contentCid ,
115+ type : contentType ,
116+ } ;
117+
118+ if ( detectGatewayContentType ( mime ) ) {
119+ return { ...response , gateway : true } ;
120+ }
117121
118- if ( detectGatewayContentType ( mime ) ) {
119- return { ...response , gateway : true } ;
120- }
122+ const rawData =
123+ typeof result !== 'string'
124+ ? await getResponseResult ( result , onProgress )
125+ : result ;
121126
122- const rawData =
123- typeof result !== 'string'
124- ? await getResponseResult ( result , onProgress )
125- : result ;
127+ const isStringData = typeof rawData === 'string' ;
126128
127- const isStringData = typeof rawData === 'string' ;
129+ // console.log(rawData);
130+ if ( ! rawData ) {
131+ return {
132+ ...response ,
133+ gateway : true ,
134+ text : `Can't parse content for ${ cid . toString ( ) } ` ,
135+ } ;
136+ }
128137
129- // console.log(rawData);
130- if ( ! rawData ) {
138+ // clarify text-content subtypes
139+ if ( response . type === 'text' ) {
140+ // render svg as image
141+ if ( ! isStringData && isSvg ( Buffer . from ( rawData ) ) ) {
131142 return {
132143 ...response ,
133- gateway : true ,
134- text : `Can't parse content for ${ cid . toString ( ) } ` ,
144+ type : 'image' ,
145+ content : createImgData ( rawData , 'image/svg+xml' ) ,
135146 } ;
136147 }
137148
138- // clarify text-content subtypes
139- if ( response . type === 'text' ) {
140- // render svg as image
141- if ( ! isStringData && isSvg ( Buffer . from ( rawData ) ) ) {
142- return {
143- ...response ,
144- type : 'image' ,
145- content : createImgData ( rawData , 'image/svg+xml' ) ,
146- } ;
147- }
148-
149- const str = isStringData ? rawData : uint8ArrayToAsciiString ( rawData ) ;
150-
151- if ( str . match ( PATTERN_IPFS_HASH ) ) {
152- return {
153- ...response ,
154- type : 'cid' ,
155- content : str ,
156- } ;
157- }
158- if ( str . match ( PATTERN_HTTP ) ) {
159- return {
160- ...response ,
161- type : 'link' ,
162- content : str ,
163- } ;
164- }
165- if ( isHtml ( str ) ) {
166- return {
167- ...response ,
168- type : 'html' ,
169- gateway : true ,
170- content : cid . toString ( ) ,
171- } ;
172- }
173-
174- // TODO: search can bel longer for 42???!
175- // also cover ipns links
149+ const str = isStringData ? rawData : uint8ArrayToAsciiString ( rawData ) ;
150+
151+ if ( str . match ( PATTERN_IPFS_HASH ) ) {
176152 return {
177153 ...response ,
178- link : str . length > 42 ? `/ipfs/${ cid } ` : `/search/${ str } ` ,
179- type : 'text' ,
180- text : shortenString ( str ) ,
154+ type : 'cid' ,
181155 content : str ,
182156 } ;
183157 }
184-
185- if ( ! isStringData ) {
186- if ( response . type === 'image' ) {
187- return { ...response , content : createImgData ( rawData , mime ) } ; // file
188- }
189- if ( response . type === 'pdf' ) {
190- return {
191- ...response ,
192- content : createObjectURL ( rawData , mime ) ,
193- gateway : true ,
194- } ; // file
195- }
158+ if ( str . match ( PATTERN_HTTP ) ) {
159+ return {
160+ ...response ,
161+ type : 'link' ,
162+ content : str ,
163+ } ;
164+ }
165+ if ( isHtml ( str ) ) {
166+ return {
167+ ...response ,
168+ type : 'html' ,
169+ gateway : true ,
170+ content : cid . toString ( ) ,
171+ } ;
196172 }
197173
198- return response ;
199- } catch ( e ) {
200- console . log ( '----parseRawIpfsData' , e , cid ) ;
201- return undefined ;
174+ // TODO: search can bel longer for 42???!
175+ // also cover ipns links
176+ return {
177+ ...response ,
178+ link : str . length > 42 ? `/ipfs/${ cid } ` : `/search/${ str } ` ,
179+ type : 'text' ,
180+ text : shortenString ( str ) ,
181+ content : str ,
182+ } ;
202183 }
184+
185+ if ( ! isStringData ) {
186+ if ( response . type === 'image' ) {
187+ return { ...response , content : createImgData ( rawData , mime ) } ; // file
188+ }
189+ if ( response . type === 'pdf' ) {
190+ return {
191+ ...response ,
192+ content : createObjectURL ( rawData , mime ) ,
193+ gateway : true ,
194+ } ; // file
195+ }
196+ }
197+
198+ return response ;
199+ // } catch (e) {
200+ // console.log('----parseRawIpfsData', e, cid);
201+ // return undefined;
202+ // }
203203} ;
204204
205205export const contentToUint8Array = async (
0 commit comments