Skip to content

Commit

Permalink
* Images: added cleanup routine on startup (app will fix and delete a…
Browse files Browse the repository at this point in the history
…ll broken or temporary images files, see #6267);
  • Loading branch information
JayDi85 committed Sep 8, 2020
1 parent d0ccafd commit f670de0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
7 changes: 6 additions & 1 deletion Mage.Client/src/main/java/mage/client/MageFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.mage.card.arcane.ManaSymbols;
import org.mage.plugins.card.images.DownloadPicturesService;
import org.mage.plugins.card.info.CardInfoPaneImpl;
import org.mage.plugins.card.utils.CardImageUtils;
import org.mage.plugins.card.utils.impl.ImageManagerImpl;

import javax.imageio.ImageIO;
Expand Down Expand Up @@ -235,11 +236,15 @@ public void windowClosing(WindowEvent e) {
RepositoryUtil.bootstrapLocalDb();
// re-create database on empty (e.g. after new build cleaned db on startup)
if (RepositoryUtil.CARD_DB_RECREATE_BY_CLIENT_SIDE && RepositoryUtil.isDatabaseEmpty()) {
LOGGER.info("DB: creating cards database");
LOGGER.info("DB: creating cards database...");
CardScanner.scan();
LOGGER.info("Done.");
}

// IMAGES CHECK
LOGGER.info("Images: search broken files...");
CardImageUtils.checkAndFixImageFiles();

if (RateCard.PRELOAD_CARD_RATINGS_ON_STARTUP) {
RateCard.bootstrapCardsAndRatings();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,21 @@
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Locale;
import java.util.prefs.Preferences;

public final class CardImageUtils {

private static final HashMap<CardDownloadData, String> pathCache = new HashMap<>();
private static final Logger log = Logger.getLogger(CardImageUtils.class);
private static final Logger LOGGER = Logger.getLogger(CardImageUtils.class);

/**
* @param card
Expand Down Expand Up @@ -54,7 +61,7 @@ public static String generateTokenImagePath(CardDownloadData card) {

//log.warn("Token image file not found. Set: " + card.getSet() + " Token Set Code: " + card.getTokenSetCode() + " Name: " + card.getName() + " File path: " + getTokenImagePath(card));
} else {
log.warn("Trying to get token path for non token card. Set: " + card.getSet() + " Set Code: " + card.getTokenSetCode() + " Name: " + card.getName());
LOGGER.warn("Trying to get token path for non token card. Set: " + card.getSet() + " Set Code: " + card.getTokenSetCode() + " Name: " + card.getName());
}
return null;
}
Expand Down Expand Up @@ -296,4 +303,56 @@ public static Document downloadHtmlDocument(String urlString) throws NumberForma
}
return doc;
}

public static void checkAndFixImageFiles() {
// search broken files and delete it (zero size files)
// search temp files and delete it (.tmp files from zip library)
Path rootPath = new File(CardImageUtils.getImagesDir()).toPath();
Collection<Path> brokenFilesList = new ArrayList<>();
Collection<Path> tempFilesList = new ArrayList<>();
try {
Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
// 1. broken files (need zero size files): delete with warning
if (attrs.size() == 0) {
brokenFilesList.add(file);
return FileVisitResult.CONTINUE;
}

// 2. temp files delete without warning
if (file.toString().endsWith(".tmp")) {
tempFilesList.add(file);
}

return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
LOGGER.error("Can't load files list from images folder: " + rootPath.toAbsolutePath().toString(), e);
}

// temp files must be deleted without errors
for (Path tempFile : tempFilesList) {
try {
Files.delete(tempFile);
} catch (Throwable e) {
// ignore any error (e.g. it opened by xmage app)
}
}

// broken files must be informed
if (!brokenFilesList.isEmpty()) {
LOGGER.warn("Images: found " + brokenFilesList.size() + " broken files. Trying to fix it...");
for (Path brokenFile : brokenFilesList) {
try {
Files.delete(brokenFile);
} catch (Throwable e) {
// stop clean on any error
LOGGER.error("Images check: ERROR, can't delete broken file: " + brokenFile.toAbsolutePath().toString(), e);
break;
}
}
}
}
}

0 comments on commit f670de0

Please sign in to comment.