magarena/src/magic/data/CardImageFile.java

52 lines
1.3 KiB
Java
Raw Normal View History

2013-04-12 19:32:25 -07:00
package magic.data;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
2013-04-12 19:32:25 -07:00
import java.net.Proxy;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import magic.model.MagicCardDefinition;
2014-08-17 00:37:58 -07:00
import magic.utility.MagicFileSystem;
2013-04-12 19:32:25 -07:00
public class CardImageFile extends DownloadableFile {
2013-04-12 19:32:25 -07:00
private final File file;
2013-04-12 19:32:25 -07:00
private final URL url;
private final String cardName;
2013-06-23 18:33:35 -07:00
public CardImageFile(final MagicCardDefinition cdef) throws MalformedURLException {
2014-08-17 00:37:58 -07:00
file = MagicFileSystem.getCardImageFile(cdef);
url = new URL(cdef.getImageURL());
cardName = cdef.getName();
2013-04-12 19:32:25 -07:00
}
2013-06-23 18:33:35 -07:00
@Override
2013-04-12 19:32:25 -07:00
public String getFilename() {
return file.getName();
}
@Override
public File getLocalFile() {
2013-04-12 19:32:25 -07:00
return file;
}
2013-06-23 18:33:35 -07:00
@Override
public void download(final Proxy proxy) throws IOException {
final File tempFile = new File(file.getParent(), "~" + file.getName());
DownloadableFile.downloadToFile(proxy, url, tempFile);
Files.move(tempFile.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING);
2013-04-12 19:32:25 -07:00
}
public String getCardName() {
return cardName;
2013-04-12 19:32:25 -07:00
}
@Override
public URL getDownloadUrl() {
return url;
}
2013-04-12 19:32:25 -07:00
}