@@ -80,41 +80,45 @@ const catFile = () => {
80
80
if ( multihash ) {
81
81
// Get a file or many files
82
82
ipfs . files . get ( multihash , ( err , stream ) => {
83
- if ( err ) onError ( err )
83
+ if ( err ) {
84
+ onError ( err )
85
+ }
84
86
console . log ( stream )
87
+
85
88
// .get gives us a stream of files
86
89
stream . on ( 'data' , ( file ) => {
87
- console . log ( file )
90
+ console . log ( 'got file' , file )
91
+
88
92
const buf = [ ]
93
+
89
94
if ( file . content ) {
90
95
// once we get a file, we also want to read the data for that file
91
- file . content . on ( 'data' , ( data ) => {
92
- console . log ( 'file got data' )
93
- buf . push ( data )
94
- } )
95
- file . content . on ( 'end' , ( data ) => {
96
- console . log ( 'file got end' )
97
- console . log ( buf )
96
+ file . content . on ( 'data' , ( data ) => buf . push ( data ) )
97
+
98
+ file . content . on ( 'end' , ( ) => {
99
+ console . log ( 'The buf' , buf )
100
+
101
+ const content = new window . Blob ( buf , { type : 'application/octet-binary' } )
102
+ const contentUrl = window . URL . createObjectURL ( content )
103
+
104
+ const listItem = document . createElement ( 'div' )
105
+ const link = document . createElement ( 'a' )
106
+ link . setAttribute ( 'href' , contentUrl )
107
+ link . setAttribute ( 'download' , multihash )
108
+ const date = ( new Date ( ) ) . toLocaleTimeString ( )
109
+
110
+ link . innerText = date + ' - ' + multihash + ' - Size: ' + file . size
111
+ const fileList = document . querySelector ( '.file-list' )
112
+
113
+ listItem . appendChild ( link )
114
+ fileList . insertBefore ( listItem , fileList . firstChild )
98
115
} )
99
- // TODO currently, can't grab content from file either in get...
100
- const downloadContent = window . btoa ( window . unescape ( window . encodeURIComponent ( 'content of file' ) ) )
101
- const downloadLink = 'data:application/octet-stream;charset=utf-8;base64,' + downloadContent
102
- const listItem = document . createElement ( 'div' )
103
- const link = document . createElement ( 'a' )
104
- link . setAttribute ( 'href' , downloadLink )
105
- link . setAttribute ( 'download' , multihash )
106
- const date = ( new Date ( ) ) . toLocaleTimeString ( )
107
-
108
- link . innerText = date + ' - ' + multihash + ' - Size: ' + file . size
109
- const fileList = document . querySelector ( '.file-list' )
110
-
111
- listItem . appendChild ( link )
112
- fileList . insertBefore ( listItem , fileList . firstChild )
116
+
117
+ file . content . resume ( )
113
118
}
114
119
} )
115
- stream . on ( 'end' , ( ) => {
116
- console . log ( 'no more files' )
117
- } )
120
+
121
+ stream . on ( 'end' , ( ) => console . log ( 'no more files' ) )
118
122
} )
119
123
}
120
124
}
0 commit comments