From 8c6472b2b694fe51e46e37bbc09706780a6ea914 Mon Sep 17 00:00:00 2001 From: Abhinay Agarwal Date: Thu, 12 Sep 2024 21:59:27 +0530 Subject: [PATCH] add temporary file to monitor successful download --- .../emoji/DownloadableEmojiSpriteLoader.java | 35 +++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/emoji/src/main/java/com/gluonhq/emoji/DownloadableEmojiSpriteLoader.java b/emoji/src/main/java/com/gluonhq/emoji/DownloadableEmojiSpriteLoader.java index 8589b6a..8cb245b 100644 --- a/emoji/src/main/java/com/gluonhq/emoji/DownloadableEmojiSpriteLoader.java +++ b/emoji/src/main/java/com/gluonhq/emoji/DownloadableEmojiSpriteLoader.java @@ -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); @@ -129,13 +137,12 @@ 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); } @@ -143,6 +150,18 @@ private void downloadFile(URL url, Path filePath) throws IOException { 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); } @@ -150,4 +169,8 @@ 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"); + } }