Skip to content

Commit

Permalink
add temporary file to monitor successful download
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinayagarwal committed Sep 12, 2024
1 parent 71ca413 commit 8c6472b
Showing 1 changed file with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,17 @@ private void downloadSprites(int... sizes) {
for (int size : sizes) {
if (!localFileExists(size)) {
try {
Path localFilePath = getLocalFilePath(size);
Path parentDir = localFilePath.getParent();
if (parentDir != null && !Files.exists(parentDir)) {
Files.createDirectories(parentDir);
}
Path tmpFilePath = getTmpFilePath(size);
LOG.fine("Creating tmp file: " + tmpFilePath);
Files.createFile(tmpFilePath);
LOG.fine("Download sprite file for size: " + size);
String url = String.format(EMOJI_PNG_URL, commit, size);
downloadFile(new URI(url).toURL(), getLocalFilePath(size));
downloadFile(new URI(url).toURL(), localFilePath, tmpFilePath);
} catch (IOException | URISyntaxException e) {
LOG.severe("Download sprite failed: " + e.getMessage());
throw new RuntimeException("Unable to load local image file", e);
Expand All @@ -129,25 +137,40 @@ public InputStream loadCSV() {
return DownloadableEmojiSpriteLoader.class.getResourceAsStream("emoji.csv");
}

private void downloadFile(URL url, Path filePath) throws IOException {
Path parentDir = filePath.getParent();
if (parentDir != null && !Files.exists(parentDir)) {
Files.createDirectories(parentDir);
}
private void downloadFile(URL url, Path filePath, Path tmpFile) throws IOException {
try (InputStream inputStream = url.openStream()) {
Files.copy(inputStream, filePath, StandardCopyOption.REPLACE_EXISTING);
LOG.fine("Sprite downloaded successfully as: " + filePath);
LOG.fine("Removing tmp file: " + tmpFile);
Files.delete(tmpFile);
} catch (IOException e) {
LOG.log(Level.SEVERE, "Downloading file failed", e);
}
}

private boolean localFileExists(int size) {
Path localFilePath = getLocalFilePath(size);
Path tmpFilePath = getTmpFilePath(size);
if (Files.exists(tmpFilePath)) {
LOG.fine("Temporary download file found");
LOG.fine("Delete file and re-try downloading");
try {
Files.deleteIfExists(localFilePath);
Files.delete(tmpFilePath);
return false;
} catch (IOException e) {
throw new RuntimeException("Unable to delete file: " + localFilePath);
}
}
return Files.exists(localFilePath);
}

private Path getLocalFilePath(int size) {
String fileName = "sheet_apple_" + size + ".png";
return Paths.get(String.format(LOCAL_PATH, commit), fileName);
}

private Path getTmpFilePath(int size) {
return Paths.get(String.format(LOCAL_PATH, commit)).resolve(size + ".downloading");
}
}

0 comments on commit 8c6472b

Please sign in to comment.