11import GitHub from './GitHubAPI/GitHub' ;
22import { lookup } from 'mime-types' ;
3+ import Repository from './GitHubAPI/Repository' ;
4+ import Gist from './GitHubAPI/Gist' ;
35
4- const fsOperation = acode . require ( 'fsoperation' ) ;
56const Url = acode . require ( 'url' ) ;
7+ const fsOperation = acode . require ( 'fs' ) || acode . require ( 'fsOperation' ) ;
68const helpers = acode . require ( 'helpers' ) ;
79const prompt = acode . require ( 'prompt' ) ;
10+ const encodings = acode . require ( 'encodings' ) ;
11+
812const test = ( url ) => / ^ g h : / . test ( url ) ;
913
1014githubFs . remove = ( ) => {
@@ -108,7 +112,9 @@ export default function githubFs(token, settings) {
108112 * @returns
109113 */
110114 function readRepo ( user , repoAtBranch , path ) {
115+ /**@type {GitHub } */
111116 let gh ;
117+ /**@type {Repository } */
112118 let repo ;
113119 const [ repoName , branch ] = repoAtBranch . split ( '@' ) ;
114120 let sha = '' ;
@@ -156,23 +162,51 @@ export default function githubFs(token, settings) {
156162 data = await data . arrayBuffer ( ) ;
157163
158164 if ( encoding ) {
165+ if ( encodings ?. decode ) {
166+ const decoded = await encodings . decode ( data , encoding ) ;
167+ if ( decoded ) return decoded ;
168+ }
169+
170+ /**@deprecated just for backward compatibility */
159171 return helpers . decodeText ( data , encoding ) ;
160172 }
161173
162174 return data ;
163175 } ,
164- async writeFile ( data ) {
176+ async writeFile ( data , encoding ) {
165177 if ( ! path ) throw new Error ( 'Cannot write to root directory' )
166178 const commitMessage = await getCommitMessage ( `update ${ path } ` ) ;
167179 if ( ! commitMessage ) return ;
180+
181+ let encode = true ;
182+
183+ if ( encoding ) {
184+ if ( data instanceof ArrayBuffer && encodings ?. decode ) {
185+ data = await encodings . decode ( data , encoding ) ;
186+ }
187+
188+ if ( encoding && encodings ?. encode ) {
189+ data = await encodings . encode ( data , encoding ) ;
190+ }
191+
192+ if ( data instanceof ArrayBuffer && encodings ?. decode ) {
193+ data = await encodings . decode ( data , encoding ) ;
194+ }
195+ } else if ( data instanceof ArrayBuffer ) {
196+ // convert to base64
197+ data = await bufferToBase64 ( data ) ;
198+ encode = false ;
199+ }
200+
168201 await init ( ) ;
169- await repo . writeFile ( branch , path , data , commitMessage ) ;
202+ await repo . writeFile ( branch , path , data , commitMessage , { encode } ) ;
170203 } ,
171204 async createFile ( name , data = '' ) {
172205 await init ( ) ;
173206 const newPath = path === '' ? name : Url . join ( path , name ) ;
174207 // check if file exists
175208 let sha ;
209+ let encode = true ;
176210 try {
177211 sha = await repo . getSha ( branch , newPath ) ;
178212 } catch ( e ) {
@@ -183,9 +217,15 @@ export default function githubFs(token, settings) {
183217 throw new Error ( 'File already exists' ) ;
184218 }
185219
220+ if ( data instanceof ArrayBuffer ) {
221+ // convert to base64
222+ data = await bufferToBase64 ( data ) ;
223+ encode = false ;
224+ }
225+
186226 const commitMessage = await getCommitMessage ( `create ${ newPath } ` ) ;
187227 if ( ! commitMessage ) return ;
188- await repo . writeFile ( branch , newPath , data , commitMessage ) ;
228+ await repo . writeFile ( branch , newPath , data , commitMessage , { encode } ) ;
189229 return githubFs . constructUrl ( 'repo' , user , repoName , newPath , branch ) ;
190230 } ,
191231 async createDirectory ( dirname ) {
@@ -269,8 +309,11 @@ export default function githubFs(token, settings) {
269309 }
270310
271311 function readGist ( gistId , path ) {
312+ /**@type {string } */
272313 let file ;
314+ /**@type {GitHub } */
273315 let gh ;
316+ /**@type {Gist } */
274317 let gist ;
275318 const getFile = async ( ) => {
276319 if ( ! file ) {
@@ -289,20 +332,42 @@ export default function githubFs(token, settings) {
289332 async lsDir ( ) {
290333 throw new Error ( 'Not implemented' ) ;
291334 } ,
292- async readFile ( encoding , progress ) {
335+ async readFile ( encoding ) {
293336 await init ( ) ;
294337 let { content : data } = await getFile ( ) ;
295338 const textEncoder = new TextEncoder ( ) ;
296339 data = textEncoder . encode ( file . content ) ;
297340
298341 if ( encoding ) {
342+ if ( encodings ?. decode ) {
343+ const decoded = await encodings . decode ( data , encoding ) ;
344+ if ( decoded ) return decoded ;
345+ }
346+
299347 return helpers . decodeText ( data , encoding ) ;
300348 }
301349
302350 return data ;
303351 } ,
304- async writeFile ( data ) {
352+ async writeFile ( data , encoding ) {
305353 await init ( ) ;
354+
355+ encoding = settings . value . defaultFileEncoding || 'utf-8' ;
356+
357+ if ( encoding ) {
358+ if ( data instanceof ArrayBuffer && encodings ?. decode ) {
359+ data = await encodings . decode ( data , encoding ) ;
360+ }
361+
362+ if ( encoding && encodings ?. encode ) {
363+ data = await encodings . encode ( data , encoding ) ;
364+ }
365+
366+ if ( data instanceof ArrayBuffer && encodings ?. decode ) {
367+ data = await encodings . decode ( data , encoding ) ;
368+ }
369+ }
370+
306371 await gist . update ( {
307372 files : {
308373 [ path ] : {
@@ -347,3 +412,19 @@ export default function githubFs(token, settings) {
347412 }
348413 }
349414}
415+
416+ async function bufferToBase64 ( buffer ) {
417+ const blob = new Blob ( [ buffer ] ) ;
418+ const reader = new FileReader ( ) ;
419+
420+ reader . readAsDataURL ( blob ) ;
421+ return new Promise ( ( resolve , reject ) => {
422+ reader . onloadend = ( ) => {
423+ // strip off the data: url prefix
424+ const content = reader . result . slice ( reader . result . indexOf ( ',' ) + 1 ) ;
425+ resolve ( content ) ;
426+ } ;
427+
428+ reader . onerror = reject ;
429+ } ) ;
430+ }
0 commit comments