2017-11-03 02:09:34 +02:00
const moment = require ( 'moment' ) ;
const { time } = require ( 'lib/time-utils.js' ) ;
const { dirname , basename } = require ( 'lib/path-utils.js' ) ;
const { OneDriveApi } = require ( 'lib/onedrive-api.js' ) ;
2017-06-22 21:44:38 +02:00
class FileApiDriverOneDrive {
2017-07-06 21:29:09 +02:00
constructor ( api ) {
this . api _ = api ;
2017-08-18 22:46:39 +02:00
this . pathCache _ = { } ;
2017-06-22 23:52:27 +02:00
}
api ( ) {
return this . api _ ;
2017-06-22 21:44:38 +02:00
}
itemFilter _ ( ) {
return {
2017-08-18 22:46:39 +02:00
select : 'name,file,folder,fileSystemInfo,parentReference' ,
2017-06-22 21:44:38 +02:00
}
}
makePath _ ( path ) {
2017-06-22 23:52:27 +02:00
return path ;
2017-06-22 21:44:38 +02:00
}
makeItems _ ( odItems ) {
let output = [ ] ;
for ( let i = 0 ; i < odItems . length ; i ++ ) {
output . push ( this . makeItem _ ( odItems [ i ] ) ) ;
}
return output ;
}
makeItem _ ( odItem ) {
2017-07-18 22:03:07 +02:00
let output = {
2017-06-22 21:44:38 +02:00
path : odItem . name ,
isDir : ( 'folder' in odItem ) ,
} ;
2017-07-18 22:03:07 +02:00
if ( 'deleted' in odItem ) {
output . isDeleted = true ;
} else {
output . created _time = moment ( odItem . fileSystemInfo . createdDateTime , 'YYYY-MM-DDTHH:mm:ss.SSSZ' ) . format ( 'x' ) ;
output . updated _time = moment ( odItem . fileSystemInfo . lastModifiedDateTime , 'YYYY-MM-DDTHH:mm:ss.SSSZ' ) . format ( 'x' ) ;
}
return output ;
2017-06-22 21:44:38 +02:00
}
2017-06-24 00:19:59 +02:00
async statRaw _ ( path ) {
2017-06-23 23:32:24 +02:00
let item = null ;
2017-06-22 23:52:27 +02:00
try {
2017-06-23 23:32:24 +02:00
item = await this . api _ . execJson ( 'GET' , this . makePath _ ( path ) , this . itemFilter _ ( ) ) ;
2017-06-22 23:52:27 +02:00
} catch ( error ) {
2017-06-29 20:03:16 +02:00
if ( error . code == 'itemNotFound' ) return null ;
2017-06-22 23:52:27 +02:00
throw error ;
}
2017-06-24 00:19:59 +02:00
return item ;
}
async stat ( path ) {
let item = await this . statRaw _ ( path ) ;
if ( ! item ) return null ;
2017-06-22 21:44:38 +02:00
return this . makeItem _ ( item ) ;
}
async setTimestamp ( path , timestamp ) {
let body = {
fileSystemInfo : {
lastModifiedDateTime : moment . unix ( timestamp / 1000 ) . utc ( ) . format ( 'YYYY-MM-DDTHH:mm:ss.SSS' ) + 'Z' ,
}
} ;
2017-06-24 00:19:59 +02:00
let item = await this . api _ . execJson ( 'PATCH' , this . makePath _ ( path ) , null , body ) ;
return this . makeItem _ ( item ) ;
2017-06-22 21:44:38 +02:00
}
2017-06-29 20:03:16 +02:00
async list ( path , options = null ) {
let query = this . itemFilter _ ( ) ;
let url = this . makePath _ ( path ) + ':/children' ;
if ( options . context ) {
query = null ;
url = options . context ;
}
let r = await this . api _ . execJson ( 'GET' , url , query ) ;
return {
hasMore : ! ! r [ '@odata.nextLink' ] ,
items : this . makeItems _ ( r . value ) ,
context : r [ "@odata.nextLink" ] ,
}
2017-06-22 21:44:38 +02:00
}
2017-07-06 23:30:45 +02:00
async get ( path , options = null ) {
if ( ! options ) options = { } ;
2017-06-22 23:52:27 +02:00
try {
2017-07-06 23:30:45 +02:00
if ( options . target == 'file' ) {
let response = await this . api _ . exec ( 'GET' , this . makePath _ ( path ) + ':/content' , null , null , options ) ;
return response ;
} else {
let content = await this . api _ . execText ( 'GET' , this . makePath _ ( path ) + ':/content' ) ;
return content ;
}
2017-06-22 23:52:27 +02:00
} catch ( error ) {
2017-06-29 20:03:16 +02:00
if ( error . code == 'itemNotFound' ) return null ;
2017-06-22 23:52:27 +02:00
throw error ;
}
2017-06-22 21:44:38 +02:00
}
2017-06-23 23:32:24 +02:00
async mkdir ( path ) {
let item = await this . stat ( path ) ;
if ( item ) return item ;
let parentPath = dirname ( path ) ;
item = await this . api _ . execJson ( 'POST' , this . makePath _ ( parentPath ) + ':/children' , this . itemFilter _ ( ) , {
name : basename ( path ) ,
folder : { } ,
} ) ;
return this . makeItem _ ( item ) ;
2017-06-22 21:44:38 +02:00
}
2017-08-01 23:40:14 +02:00
put ( path , content , options = null ) {
if ( ! options ) options = { } ;
if ( options . source == 'file' ) {
return this . api _ . exec ( 'PUT' , this . makePath _ ( path ) + ':/content' , null , null , options ) ;
} else {
options . headers = { 'Content-Type' : 'text/plain' } ;
return this . api _ . exec ( 'PUT' , this . makePath _ ( path ) + ':/content' , null , content , options ) ;
}
2017-06-22 21:44:38 +02:00
}
delete ( path ) {
return this . api _ . exec ( 'DELETE' , this . makePath _ ( path ) ) ;
}
2017-06-23 23:32:24 +02:00
async move ( oldPath , newPath ) {
2017-07-13 00:32:08 +02:00
// Cannot work in an atomic way because if newPath already exist, the OneDrive API throw an error
// "An item with the same name already exists under the parent". Some posts suggest to use
// @name.conflictBehavior [0]but that doesn't seem to work. So until Microsoft fixes this
// it's not possible to do an atomic move.
//
// [0] https://stackoverflow.com/questions/29191091/onedrive-api-overwrite-on-move
throw new Error ( 'NOT WORKING' ) ;
2017-06-24 00:19:59 +02:00
let previousItem = await this . statRaw _ ( oldPath ) ;
2017-06-23 23:32:24 +02:00
let newDir = dirname ( newPath ) ;
let newName = basename ( newPath ) ;
2017-06-24 00:19:59 +02:00
// We don't want the modification date to change when we move the file so retrieve it
// now set it in the PATCH operation.
2017-06-23 23:32:24 +02:00
let item = await this . api _ . execJson ( 'PATCH' , this . makePath _ ( oldPath ) , this . itemFilter _ ( ) , {
name : newName ,
parentReference : { path : newDir } ,
2017-06-24 00:19:59 +02:00
fileSystemInfo : {
lastModifiedDateTime : previousItem . fileSystemInfo . lastModifiedDateTime ,
} ,
2017-06-23 23:32:24 +02:00
} ) ;
return this . makeItem _ ( item ) ;
2017-06-22 21:44:38 +02:00
}
format ( ) {
throw new Error ( 'Not implemented' ) ;
}
2017-08-18 22:46:39 +02:00
async pathDetails _ ( path ) {
if ( this . pathCache _ [ path ] ) return this . pathCache _ [ path ] ;
let output = await this . api _ . execJson ( 'GET' , path ) ;
this . pathCache _ [ path ] = output ;
return this . pathCache _ [ path ] ;
}
2017-07-18 21:57:49 +02:00
async delta ( path , options = null ) {
let output = {
2017-07-18 22:03:07 +02:00
hasMore : false ,
2017-07-18 21:57:49 +02:00
context : { } ,
items : [ ] ,
} ;
2017-08-19 22:56:28 +02:00
const pathDetails = await this . pathDetails _ ( path ) ;
2017-08-18 22:46:39 +02:00
const pathId = pathDetails . id ;
2017-07-18 21:57:49 +02:00
let context = options ? options . context : null ;
2017-07-24 20:01:40 +02:00
let url = context ? context . nextLink : null ;
2017-07-18 21:57:49 +02:00
let query = null ;
2017-07-24 20:01:40 +02:00
if ( ! url ) {
2017-07-18 21:57:49 +02:00
url = this . makePath _ ( path ) + ':/delta' ;
2017-10-26 23:57:49 +02:00
const query = this . itemFilter _ ( ) ;
2017-07-18 22:03:07 +02:00
query . select += ',deleted' ;
2017-07-18 21:57:49 +02:00
}
2017-10-26 23:57:49 +02:00
let response = null ;
try {
response = await this . api _ . execJson ( 'GET' , url , query ) ;
} catch ( error ) {
if ( error . code === 'resyncRequired' ) {
// Error: Resync required. Replace any local items with the server's version (including deletes) if you're sure that the service was up to date with your local changes when you last sync'd. Upload any local changes that the server doesn't know about.
// Code: resyncRequired
// Request: GET https://graph.microsoft.com/v1.0/drive/root:/Apps/JoplinDev:/delta?select=...
// The delta token has expired or is invalid and so a full resync is required.
// It is an error that is hard to replicate and it's not entirely clear what
// URL is in the Location header. What might happen is that:
// - OneDrive will get all the latest changes (since delta is done at the
// end of the sync process)
// - Client will get all the new files and updates from OneDrive
// This is unknown:
// - Will the files that have been deleted on OneDrive be part of the this
// URL in the Location header?
//
// More info there: https://stackoverflow.com/q/46941371/561309
url = error . headers . get ( 'location' ) ;
response = await this . api _ . execJson ( 'GET' , url , query ) ;
} else {
throw error ;
}
}
2017-08-18 22:46:39 +02:00
let items = [ ] ;
// The delta API might return things that happen in subdirectories of the root and we don't want to
// deal with these since all the files we're interested in are at the root (The .resource dir
// is special since it's managed directly by the clients and resources never change - only the
// associated .md file at the root is synced). So in the loop below we check that the parent is
// indeed the root, otherwise the item is skipped.
// (Not sure but it's possible the delta API also returns events for files that are copied outside
// of the app directory and later deleted or modified. We also don't want to deal with
// these files during sync).
for ( let i = 0 ; i < response . value . length ; i ++ ) {
const v = response . value [ i ] ;
if ( v . parentReference . id !== pathId ) continue ;
items . push ( this . makeItem _ ( v ) ) ;
}
2017-07-18 22:03:07 +02:00
output . items = output . items . concat ( items ) ;
2017-07-18 21:57:49 +02:00
2017-07-18 22:03:07 +02:00
let nextLink = null ;
if ( response [ '@odata.nextLink' ] ) {
nextLink = response [ '@odata.nextLink' ] ;
output . hasMore = true ;
} else {
if ( ! response [ '@odata.deltaLink' ] ) throw new Error ( 'Delta link missing: ' + JSON . stringify ( response ) ) ;
nextLink = response [ '@odata.deltaLink' ] ;
2017-07-18 21:57:49 +02:00
}
2017-07-18 22:03:07 +02:00
output . context = { nextLink : nextLink } ;
2017-07-18 21:57:49 +02:00
// https://dev.onedrive.com/items/view_delta.htm
// The same item may appear more than once in a delta feed, for various reasons. You should use the last occurrence you see.
// So remove any duplicate item from the array.
let temp = [ ] ;
let seenPaths = [ ] ;
for ( let i = output . items . length - 1 ; i >= 0 ; i -- ) {
let item = output . items [ i ] ;
if ( seenPaths . indexOf ( item . path ) >= 0 ) continue ;
temp . splice ( 0 , 0 , item ) ;
seenPaths . push ( item . path ) ;
}
output . items = temp ;
return output ;
}
2017-07-18 21:27:10 +02:00
2017-06-22 21:44:38 +02:00
}
2017-11-03 02:13:17 +02:00
module . exports = { FileApiDriverOneDrive } ;