Skip to content

Commit

Permalink
fix(sync): continue on unsuccessful downloads and print an error
Browse files Browse the repository at this point in the history
  • Loading branch information
thetric committed Apr 17, 2018
1 parent ba575b4 commit 8c5f28d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.github.thetric.iliasdownloader.service.IliasService
import com.github.thetric.iliasdownloader.service.model.CourseFile
import com.github.thetric.iliasdownloader.service.model.CourseFolder
import com.github.thetric.iliasdownloader.service.model.IliasItem
import com.github.thetric.iliasdownloader.service.webparser.impl.IliasHttpException
import mu.KotlinLogging
import java.io.File
import java.nio.file.Files
Expand Down Expand Up @@ -79,10 +80,31 @@ class ItemDownloadingItemVisitor(
}

private fun syncAndSaveFile(path: Path, file: CourseFile) {
iliasService.getContentAsStream(file).use {
Files.copy(it, path, StandardCopyOption.REPLACE_EXISTING)
try {
iliasService.getContentAsStream(file).use {
Files.copy(it, path, StandardCopyOption.REPLACE_EXISTING)
}
Files.setLastModifiedTime(path, toFileTime(file.modified))
} catch (e: IliasHttpException) {
// skip downloads with an unexpected HTTP status code (not 2xx).
// in some rare cases (see issue #11) the webdav interface can
// throw a 403 forbidden when accessing a file, although the user
// can download the file via the 'normal' web interface.
// other files in the course don't seem affected and can be
// downloaded.
log.error {
getLocalizedMessage(
"sync.download.failed",
e.url,
e.statusCode,
file.name
)
}
log.trace(
e,
{ "Download of ${e.url} failed with HTTP status code ${e.statusCode}" }
)
}
Files.setLastModifiedTime(path, toFileTime(file.modified))
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/ilias-cli.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ sync.courses.prompt=Enter the numbers of the courses to sync (separate by a spac
sync.courses.prompt.errors.out-of-range=Invalid course selection! The selection must contain indices between 1 and {0}
sync.download.file.started=Downloading file "{0}" ({1,number,integer} bytes)...
sync.download.file.finished=Saved to {0}
sync.download.started=
sync.download.finished=
sync.download.file.found=Found file "{0}"
sync.course.found=Found course "{0}"
sync.download.failed=Failed to download {0} (HTTP status code {1,number,integer}). Skipping "{2}" in this sync.
1 change: 1 addition & 0 deletions src/main/resources/ilias-cli_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ sync.download.started=Lade Datei "{0}" ({1,number,integer} Bytes) herunter...
sync.download.finished=Gespeichert unter {0}
sync.download.file.found=Datei "{0}" gefunden
sync.course.found=Kurs "{0}" gefunden
sync.download.failed=Download von {0} fehlgeschlagen (HTTP status code {1,number,integer}). "{2}" wird bei diesem Sync übersprungen.

0 comments on commit 8c5f28d

Please sign in to comment.