2013-04-12 19:32:25 -07:00
|
|
|
package magic.data;
|
|
|
|
|
|
|
|
import java.io.File;
|
2014-08-02 11:26:46 -07:00
|
|
|
import java.io.IOException;
|
2014-08-16 03:22:28 -07:00
|
|
|
import java.net.MalformedURLException;
|
2013-04-12 19:32:25 -07:00
|
|
|
import java.net.Proxy;
|
|
|
|
import java.net.URL;
|
2014-08-28 09:23:34 -07:00
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.StandardCopyOption;
|
2014-08-16 03:22:28 -07:00
|
|
|
import magic.model.MagicCardDefinition;
|
2014-08-17 00:37:58 -07:00
|
|
|
import magic.utility.MagicFileSystem;
|
2013-04-12 19:32:25 -07:00
|
|
|
|
2014-08-28 08:50:30 -07:00
|
|
|
public class CardImageFile extends DownloadableFile {
|
2013-04-12 19:32:25 -07:00
|
|
|
|
2014-08-28 09:23:34 -07:00
|
|
|
private final File file;
|
2013-04-12 19:32:25 -07:00
|
|
|
private final URL url;
|
2014-08-16 03:22:28 -07:00
|
|
|
private final String cardName;
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2014-08-28 08:50:30 -07:00
|
|
|
public CardImageFile(final MagicCardDefinition cdef) throws MalformedURLException {
|
2014-08-17 00:37:58 -07:00
|
|
|
file = MagicFileSystem.getCardImageFile(cdef);
|
2014-08-16 03:22:28 -07:00
|
|
|
url = new URL(cdef.getImageURL());
|
|
|
|
cardName = cdef.getName();
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2014-08-02 11:26:46 -07:00
|
|
|
@Override
|
2013-04-12 19:32:25 -07:00
|
|
|
public String getFilename() {
|
|
|
|
return file.getName();
|
|
|
|
}
|
|
|
|
|
2014-08-02 11:26:46 -07:00
|
|
|
@Override
|
2015-11-16 05:43:49 -08:00
|
|
|
public File getLocalFile() {
|
2013-04-12 19:32:25 -07:00
|
|
|
return file;
|
|
|
|
}
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2014-08-02 11:26:46 -07:00
|
|
|
@Override
|
|
|
|
public void download(final Proxy proxy) throws IOException {
|
2014-08-28 09:23:34 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-08-16 03:22:28 -07:00
|
|
|
public String getCardName() {
|
|
|
|
return cardName;
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|
2014-05-03 01:15:11 -07:00
|
|
|
|
2014-08-16 03:22:28 -07:00
|
|
|
@Override
|
|
|
|
public URL getDownloadUrl() {
|
|
|
|
return url;
|
2014-05-03 01:15:11 -07:00
|
|
|
}
|
|
|
|
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|