88import org .jetbrains .annotations .NotNull ;
99import org .jetbrains .annotations .Nullable ;
1010import org .json .JSONArray ;
11+ import org .json .JSONException ;
1112import org .json .JSONObject ;
1213import ratismal .drivebackup .UploadThread .UploadLogger ;
1314import ratismal .drivebackup .config .ConfigParser ;
2122
2223import java .io .IOException ;
2324import java .io .RandomAccessFile ;
24- import java .util .ArrayList ;
2525import java .util .Arrays ;
26- import java .util .Collections ;
2726import java .util .Iterator ;
2827import java .util .List ;
2928import java .util .concurrent .TimeUnit ;
@@ -38,10 +37,9 @@ public class OneDriveUploader extends Uploader {
3837 public static final int EXPONENTIAL_BACKOFF_FACTOR = 5 ;
3938 public static final int MAX_RETRY_ATTEMPTS = 3 ;
4039
41- private UploadLogger logger ;
40+ private final UploadLogger logger ;
4241
4342 private long totalUploaded ;
44- private long lastUploaded ;
4543 private String accessToken = "" ;
4644 private String refreshToken ;
4745
@@ -194,7 +192,6 @@ public void uploadFile(java.io.File file, String location) throws IOException {
194192 DriveBackup .httpClient .newCall (request ).execute ().close ();
195193 throw new IOException (String .format ("Upload failed after %d retries. %d %s" , MAX_RETRY_ATTEMPTS , uploadResponse .code (), uploadResponse .message ()));
196194 }
197- // TODO handle 507 Insufficent Storage?
198195 if (uploadResponse .code () >= 500 && uploadResponse .code () < 600 ) {
199196 Thread .sleep (exponentialBackoffMillis );
200197 exponentialBackoffMillis *= EXPONENTIAL_BACKOFF_FACTOR ;
@@ -258,8 +255,8 @@ private String normalizePath(@NotNull String path) {
258255
259256 /**
260257 * joins the two paths with '/' while handling emptiness of either side
261- * @param lhs
262- * @param rhs
258+ * @param lhs left hand side of to be joined path
259+ * @param rhs right hand side of to be joined path
263260 * @return joined path
264261 */
265262 @ NotNull
@@ -284,7 +281,7 @@ private String concatPath(@NotNull String lhs, @NotNull String rhs) {
284281 private FQID createPath (@ NotNull String path ) throws IOException {
285282 Iterator <String > parts = Arrays .stream (path .split ("/" )).iterator ();
286283 FQID root = createRootFolder (parts .next ());
287- for (; parts .hasNext (); ) {
284+ while ( parts .hasNext ()) {
288285 String folder = parts .next ();
289286 root = createFolder (root , folder );
290287 }
@@ -389,7 +386,7 @@ private FQID getRootFolder(@NotNull String folder) {
389386 * @return FQID or null if not found
390387 */
391388 @ Nullable
392- private FQID getFolder (@ NotNull FQID root ,@ NotNull String folder ) {
389+ private FQID getFolder (@ NotNull FQID root , @ NotNull String folder ) {
393390 try {
394391 Request request = new Request .Builder ()
395392 .addHeader ("Authorization" , "Bearer " + accessToken )
@@ -402,7 +399,7 @@ private FQID getFolder(@NotNull FQID root,@NotNull String folder) {
402399 JSONArray children = parsedResponse .getJSONArray ("value" );
403400 for (int i = 0 ; i < children .length (); i ++) {
404401 JSONObject childItem = children .getJSONObject (i );
405- String folderName = childItem .getString ("name" );
402+ String folderName = childItem .getString ("name" ); // TODO filter non folders
406403 if (folder .equals (folderName )) {
407404 if (childItem .has ("remoteItem" )) {
408405 childItem = childItem .optJSONObject ("remoteItem" );
@@ -424,9 +421,10 @@ private FQID getFolder(@NotNull FQID root,@NotNull String folder) {
424421 * <p>
425422 * The number of files to retain is specified by the user in the {@code config.yml}
426423 * @param parent the folder containing the files
427- * @throws Exception
424+ * @throws IOException on request execution failure
425+ * @throws JSONException if the response does not contain the expected items
428426 */
429- private void pruneBackups (@ NotNull FQID parent ) throws Exception {
427+ private void pruneBackups (@ NotNull FQID parent ) throws Exception , JSONException {
430428 int fileLimit = ConfigParser .getConfig ().backupStorage .keepCount ;
431429 if (fileLimit == -1 ) {
432430 return ;
@@ -440,7 +438,7 @@ private void pruneBackups(@NotNull FQID parent) throws Exception {
440438 JSONObject parsedResponse = new JSONObject (childItemResponse .body ().string ());
441439 items = parsedResponse .getJSONArray ("value" );
442440 }
443- if (fileLimit >= items .length ()) {
441+ if (fileLimit >= items .length ()) { // TODO filter non backup files (folders & files not created by plugin)
444442 return ;
445443 }
446444 logger .info (
@@ -458,6 +456,7 @@ private void pruneBackups(@NotNull FQID parent) throws Exception {
458456 .build ();
459457 DriveBackup .httpClient .newCall (deleteRequest ).execute ().close (); // TODO handle deletion failure
460458 }
459+ // TODO handle @odata.nextLink
461460 }
462461
463462 /**
@@ -482,7 +481,6 @@ private Range(long start, long end) {
482481 * Resets the number of bytes uploaded in the last chunk, and the number of bytes uploaded in total.
483482 */
484483 private void resetRanges () {
485- lastUploaded = 0 ;
486484 totalUploaded = 0 ;
487485 }
488486
@@ -510,7 +508,7 @@ private void setRanges(@NotNull String[] stringRanges) {
510508 /**
511509 * Gets an array of bytes to upload next from the file buffer based on the number of bytes uploaded so far.
512510 * @return the array of bytes
513- * @throws IOException
511+ * @throws IOException on file read errors
514512 */
515513 private byte @ NotNull [] getChunk () throws IOException {
516514 byte [] bytes = new byte [CHUNK_SIZE ];
0 commit comments