2018-03-09 22:59:12 +02:00
const RNFS = require ( 'react-native-fs' ) ;
const FsDriverBase = require ( 'lib/fs-driver-base' ) ;
2017-12-12 01:52:42 +02:00
2018-02-25 23:08:32 +02:00
class FsDriverRN extends FsDriverBase {
2018-03-09 22:59:12 +02:00
2017-12-12 01:52:42 +02:00
appendFileSync ( path , string ) {
2018-03-09 22:59:12 +02:00
throw new Error ( 'Not implemented' ) ;
2017-12-12 01:52:42 +02:00
}
2018-03-09 22:59:12 +02:00
appendFile ( path , string , encoding = 'base64' ) {
2017-12-12 01:52:42 +02:00
return RNFS . appendFile ( path , string , encoding ) ;
}
2018-03-09 22:59:12 +02:00
writeFile ( path , string , encoding = 'base64' ) {
2018-01-17 20:51:15 +02:00
return RNFS . writeFile ( path , string , encoding ) ;
}
2018-01-25 23:15:58 +02:00
// same as rm -rf
async remove ( path ) {
2018-03-18 01:00:01 +02:00
return await this . unlink ( path ) ;
2018-01-25 23:15:58 +02:00
}
2017-12-12 01:52:42 +02:00
writeBinaryFile ( path , content ) {
2018-03-09 22:59:12 +02:00
throw new Error ( 'Not implemented' ) ;
2017-12-12 01:52:42 +02:00
}
2018-01-17 23:01:41 +02:00
// Returns a format compatible with Node.js format
rnfsStatToStd _ ( stat , path ) {
return {
birthtime : stat . ctime ? stat . ctime : stat . mtime , // Confusingly, "ctime" normally means "change time" but here it's used as "creation time". Also sometimes it is null
mtime : stat . mtime ,
isDirectory : ( ) => stat . isDirectory ( ) ,
path : path ,
size : stat . size ,
2018-03-09 22:59:12 +02:00
} ;
2018-01-17 23:01:41 +02:00
}
2018-02-25 23:08:32 +02:00
async readDirStats ( path , options = null ) {
if ( ! options ) options = { } ;
2018-03-09 22:59:12 +02:00
if ( ! ( 'recursive' in options ) ) options . recursive = false ;
2018-01-17 23:01:41 +02:00
let items = await RNFS . readDir ( path ) ;
let output = [ ] ;
for ( let i = 0 ; i < items . length ; i ++ ) {
const item = items [ i ] ;
const relativePath = item . path . substr ( path . length + 1 ) ;
output . push ( this . rnfsStatToStd _ ( item , relativePath ) ) ;
2018-02-25 23:08:32 +02:00
output = await this . readDirStatsHandleRecursion _ ( path , item , output , options ) ;
2018-01-17 23:01:41 +02:00
}
return output ;
}
2017-12-28 21:57:21 +02:00
async move ( source , dest ) {
return RNFS . moveFile ( source , dest ) ;
}
async exists ( path ) {
return RNFS . exists ( path ) ;
2017-12-19 21:01:29 +02:00
}
2018-01-17 20:51:15 +02:00
async mkdir ( path ) {
2018-01-17 23:01:41 +02:00
return RNFS . mkdir ( path ) ;
2018-01-17 20:51:15 +02:00
}
async stat ( path ) {
2018-01-17 23:01:41 +02:00
try {
const r = await RNFS . stat ( path ) ;
return this . rnfsStatToStd _ ( r , path ) ;
} catch ( error ) {
2018-03-09 22:59:12 +02:00
if ( error && ( ( error . message && error . message . indexOf ( 'exist' ) >= 0 ) || error . code === 'ENOENT' ) ) {
2018-01-17 23:01:41 +02:00
// Probably { [Error: File does not exist] framesToPop: 1, code: 'EUNSPECIFIED' }
// which unfortunately does not have a proper error code. Can be ignored.
return null ;
} else {
throw error ;
}
}
2018-01-17 20:51:15 +02:00
}
2018-01-17 23:01:41 +02:00
// NOTE: DOES NOT WORK - no error is thrown and the function is called with the right
// arguments but the function returns `false` and the timestamp is not set.
// Current setTimestamp is not really used so keep it that way, but careful if it
// becomes needed.
2018-01-17 20:51:15 +02:00
async setTimestamp ( path , timestampDate ) {
2018-01-17 23:01:41 +02:00
// return RNFS.touch(path, timestampDate, timestampDate);
2018-01-17 20:51:15 +02:00
}
2017-12-12 01:52:42 +02:00
async open ( path , mode ) {
// Note: RNFS.read() doesn't provide any way to know if the end of file has been reached.
// So instead we stat the file here and use stat.size to manually check for end of file.
// Bug: https://github.com/itinance/react-native-fs/issues/342
2018-01-17 20:51:15 +02:00
const stat = await this . stat ( path ) ;
2017-12-12 01:52:42 +02:00
return {
path : path ,
offset : 0 ,
mode : mode ,
stat : stat ,
2018-03-09 22:59:12 +02:00
}
2017-12-12 01:52:42 +02:00
}
2017-12-12 19:51:07 +02:00
close ( handle ) {
2017-12-12 01:52:42 +02:00
return null ;
}
2018-03-09 22:59:12 +02:00
readFile ( path , encoding = 'utf8' ) {
if ( encoding === 'Buffer' ) throw new Error ( 'Raw buffer output not supported for FsDriverRN.readFile' ) ;
2018-01-17 20:51:15 +02:00
return RNFS . readFile ( path , encoding ) ;
}
// Always overwrite destination
async copy ( source , dest ) {
let retry = false ;
try {
await RNFS . copyFile ( source , dest ) ;
} catch ( error ) {
// On iOS it will throw an error if the file already exist
retry = true ;
await this . unlink ( dest ) ;
}
if ( retry ) await RNFS . copyFile ( source , dest ) ;
2017-12-12 01:52:42 +02:00
}
async unlink ( path ) {
try {
await RNFS . unlink ( path ) ;
} catch ( error ) {
2018-03-09 22:59:12 +02:00
if ( error && ( ( error . message && error . message . indexOf ( 'exist' ) >= 0 ) || error . code === 'ENOENT' ) ) {
2017-12-12 01:52:42 +02:00
// Probably { [Error: File does not exist] framesToPop: 1, code: 'EUNSPECIFIED' }
// which unfortunately does not have a proper error code. Can be ignored.
} else {
throw error ;
}
}
}
2018-03-09 22:59:12 +02:00
async readFileChunk ( handle , length , encoding = 'base64' ) {
2017-12-12 01:52:42 +02:00
if ( handle . offset + length > handle . stat . size ) {
length = handle . stat . size - handle . offset ;
}
if ( ! length ) return null ;
let output = await RNFS . read ( handle . path , length , handle . offset , encoding ) ;
handle . offset += length ;
return output ? output : null ;
}
2018-03-09 22:59:12 +02:00
2017-12-12 01:52:42 +02:00
}
2018-03-09 22:59:12 +02:00
module . exports . FsDriverRN = FsDriverRN ;