@@ -394,136 +394,6 @@ private FQID getFolder(FQID root, String folder) {
394394 }
395395 return null ;
396396 }
397-
398- /**
399- * Creates a folder with the specified name in the specified parent folder in the authenticated user's OneDrive.
400- * @param name the name of the folder
401- * @param parent the parent folder
402- * @return the created folder
403- * @throws IOException
404- */
405- @ NotNull
406- private File createFolder (String name , File parent ) throws IOException {
407- File file = getFolder (name , parent );
408- if (file != null ) {
409- return file ;
410- }
411- Request request = new Request .Builder ()
412- .addHeader ("Authorization" , "Bearer " + accessToken )
413- .url ("https://graph.microsoft.com/v1.0/me/drive/root:/" + parent .getPath ())
414- .build ();
415- Response response = DriveBackup .httpClient .newCall (request ).execute ();
416- JSONObject parsedResponse = new JSONObject (response .body ().string ());
417- response .close ();
418- String parentId = parsedResponse .getString ("id" );
419- RequestBody requestBody = RequestBody .create (
420- "{" +
421- " \" name\" : \" " + name + "\" ," +
422- " \" folder\" : {}," +
423- " \" @name.conflictBehavior\" : \" fail\" " +
424- "}" , jsonMediaType );
425- request = new Request .Builder ()
426- .addHeader ("Authorization" , "Bearer " + accessToken )
427- .url ("https://graph.microsoft.com/v1.0/me/drive/items/" + parentId + "/children" )
428- .post (requestBody )
429- .build ();
430- response = DriveBackup .httpClient .newCall (request ).execute ();
431- boolean folderCreated = response .isSuccessful ();
432- response .close ();
433- if (!folderCreated ) {
434- throw new IOException ("Couldn't create folder " + name );
435- }
436- return parent .add (name );
437- }
438-
439- /**
440- * Creates a folder with the specified name in the root of the authenticated user's OneDrive.
441- * @param name the name of the folder
442- * @return the created folder
443- * @throws IOException
444- */
445- @ NotNull
446- private File createFolder (String name ) throws IOException {
447- File file = getFolder (name );
448- if (file != null ) {
449- return file ;
450- }
451- RequestBody requestBody = RequestBody .create (
452- "{" +
453- " \" name\" : \" " + name + "\" ," +
454- " \" folder\" : {}," +
455- " \" @name.conflictBehavior\" : \" fail\" " +
456- "}" , jsonMediaType );
457- Request request = new Request .Builder ()
458- .addHeader ("Authorization" , "Bearer " + accessToken )
459- .url ("https://graph.microsoft.com/v1.0/me/drive/root/children" )
460- .post (requestBody )
461- .build ();
462- Response response = DriveBackup .httpClient .newCall (request ).execute ();
463- boolean folderCreated = response .isSuccessful ();
464- response .close ();
465- if (!folderCreated ) {
466- throw new IOException ("Couldn't create folder " + name );
467- }
468- return new File ().add (name );
469- }
470-
471- /**
472- * Returns the folder in the specified parent folder of the authenticated user's OneDrive with the specified name.
473- * @param name the name of the folder
474- * @param parent the parent folder
475- * @return the folder or {@code null}
476- */
477- @ Nullable
478- private File getFolder (String name , File parent ) {
479- try {
480- Request request = new Request .Builder ()
481- .addHeader ("Authorization" , "Bearer " + accessToken )
482- .url ("https://graph.microsoft.com/v1.0/me/drive/root:/" + parent .getPath () + ":/children" )
483- .build ();
484- Response response = DriveBackup .httpClient .newCall (request ).execute ();
485- JSONObject parsedResponse = new JSONObject (response .body ().string ());
486- response .close ();
487- JSONArray jsonArray = parsedResponse .getJSONArray ("value" );
488- for (int i = 0 ; i < jsonArray .length (); i ++) {
489- String folderName = jsonArray .getJSONObject (i ).getString ("name" );
490- if (name .equals (folderName )) {
491- return parent .add (name );
492- }
493- }
494- } catch (Exception exception ) {
495- return null ;
496- }
497- return null ;
498- }
499-
500- /**
501- * Returns the folder in the root of the authenticated user's OneDrive with the specified name.
502- * @param name the name of the folder
503- * @return the folder or {@code null}
504- */
505- @ Nullable
506- private File getFolder (String name ) {
507- try {
508- Request request = new Request .Builder ()
509- .addHeader ("Authorization" , "Bearer " + accessToken )
510- .url ("https://graph.microsoft.com/v1.0/me/drive/root/children" )
511- .build ();
512- Response response = DriveBackup .httpClient .newCall (request ).execute ();
513- JSONObject parsedResponse = new JSONObject (response .body ().string ());
514- response .close ();
515- JSONArray jsonArray = parsedResponse .getJSONArray ("value" );
516- for (int i = 0 ; i < jsonArray .length (); i ++) {
517- String folderName = jsonArray .getJSONObject (i ).getString ("name" );
518- if (name .equals (folderName )) {
519- return new File ().add (name );
520- }
521- }
522- } catch (Exception exception ) {
523- return null ;
524- }
525- return null ;
526- }
527397
528398 /**
529399 * Deletes the oldest files in the specified folder past the number to retain from the authenticated user's OneDrive.
@@ -573,71 +443,6 @@ private void pruneBackups(String parent) throws Exception {
573443 }
574444 }
575445
576- /**
577- * A file/folder in the authenticated user's OneDrive
578- */
579- private static final class File {
580- private ArrayList <String > filePath = new ArrayList <>();
581-
582- /**
583- * Creates a reference of the {@code File} object
584- */
585- File () {
586- }
587-
588- /**
589- * Returns a {@code File} with the specified folder added to the file path.
590- * @param folder the {@code File}
591- */
592- @ NotNull
593- private File add (String folder ) {
594- File childFile = new File ();
595- if (getPath ().isEmpty ()) {
596- childFile .setPath (folder );
597- } else {
598- childFile .setPath (getPath () + "/" + folder );
599- }
600- return childFile ;
601- }
602-
603- /**
604- * Sets the path of the file/folder
605- * @param path the path, as an {@code String}
606- */
607- private void setPath (@ NotNull String path ) {
608- filePath .clear ();
609- Collections .addAll (filePath , path .split ("/" ));
610- }
611-
612- /**
613- * Gets the path of the file/folder
614- * @return the path, as a {@code String}
615- */
616- @ NotNull
617- private String getPath () {
618- return String .join ("/" , filePath );
619- }
620-
621- /**
622- * Gets the name of the file/folder
623- * @return the name, including any file extensions
624- */
625- private String getName () {
626- return filePath .get (filePath .size () - 1 );
627- }
628-
629- /**
630- * Gets the path of the parent folder of the file/folder.
631- * @return the path, as a String
632- */
633- @ NotNull
634- private String getParent () {
635- ArrayList <String > parentPath = new ArrayList <>(filePath );
636- parentPath .remove (parentPath .size () - 1 );
637- return String .join ("/" , parentPath );
638- }
639- }
640-
641446 /**
642447 * A range of bytes
643448 */
0 commit comments