Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7a0690f6 authored by Vincent Bourgmayer's avatar Vincent Bourgmayer
Browse files

replace String fields for url & filePath in DownloadTask by RomSource

parent c0298ac7
Loading
Loading
Loading
Loading
+20 −18
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package ecorp.easy.installer.tasks;

import ecorp.easy.installer.AppConstants;
import ecorp.easy.installer.models.RomSource;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
@@ -51,56 +52,57 @@ import org.slf4j.LoggerFactory;
 * @author Ingo
 */
public class DownloadTask extends Task<Boolean>{
    private final static String checkSumExtension = ".sha256sum";
    private final static String CHECKSUM_EXT = ".sha256sum";
    private final static Logger logger = LoggerFactory.getLogger(DownloadTask.class);
    /**
     * Constant size
     */
    private static final long[] CST_SIZE = {1024, 1024*1024, 1024*1024*1024, 1024*1024*1024*1024};
    
    /**
     * Constants units
     */
    private static final String[] CST_UNITS = {"KB", "MB", "GB", "TB"};
    
    final private ResourceBundle i18n;
    final private String targetUrl;
    final private String fileName;
    final private RomSource source; //encapsulate URL & filename
    
    /**
     * COnstruction of the download task
     * @param targetUrl the web path to the resource
     * @param fileName name of the file
     * Construction of the download task
     * @param source RomSource instance containing URL & filename
     * @param resources used to send already translated message
     */
    public DownloadTask(String targetUrl, String fileName, ResourceBundle resources){
        this.targetUrl = targetUrl;
        this.fileName = fileName;
    public DownloadTask(RomSource source, ResourceBundle resources){
        this.source = source;
        this.i18n = resources;
    }
    
    /**
     * @inheritDoc 
     * @TODO split below content in different method
     * @return Boolean object
     * @throws Exception 
     * @throws java.lang.Exception
     * @inheritDoc
     */
    @Override
    protected Boolean call() throws Exception {

        //build local filePath
        final String fileName = source.getFileName();
        final String localFilePath = AppConstants.getSourcesFolderPath()+fileName;
        
        //Check if already exist and integrity
        final String checksumFilePath = localFilePath+checkSumExtension;
        final String checksumFilePath = localFilePath+CHECKSUM_EXT;
        
        if(isCancelled()) return false;
        
        this.updateTitle("Downloading "+fileName+checkSumExtension);
        updateTitle("Downloading "+fileName+CHECKSUM_EXT);
        
        
        File checksumLmdFile = new File(AppConstants.getSourcesFolderPath()+"lmd."+fileName+checkSumExtension);
        File checksumLmdFile = new File(AppConstants.getSourcesFolderPath()+"lmd."+fileName+CHECKSUM_EXT);
        checksumLmdFile.createNewFile();
        
        // Download checksum. If file not downloaded return false and stop downloadin because integrity isn't guaranteed
        if( !downloadFile(targetUrl+checkSumExtension, checksumFilePath,checksumLmdFile) ){
        if( !downloadFile(source.getUrl()+CHECKSUM_EXT, checksumFilePath,checksumLmdFile) ){
            updateMessage(i18n.getString("download_lbl_cantcheckIntegrity"));
            return false;
        }
@@ -114,14 +116,14 @@ public class DownloadTask extends Task<Boolean>{

        if(isCancelled()) return false;

        this.updateTitle("Downloading "+fileName);
        updateTitle("Downloading "+fileName);

        final String tmpFilePath = AppConstants.getSourcesFolderPath()+"tmp."+fileName;
        
        File lmdFile = new File(AppConstants.getSourcesFolderPath()+"lmd."+fileName); //used to Store last modified Date of remote content
        lmdFile.createNewFile();
        
        if ( downloadFile(targetUrl, tmpFilePath, lmdFile) )//Download file
        if ( downloadFile(source.getUrl(), tmpFilePath, lmdFile) )//Download file
        {
            logger.debug("Downloaded succeed. Rename temp file to right fileName");
            File tmpFile = new File(tmpFilePath);
@@ -390,7 +392,7 @@ public class DownloadTask extends Task<Boolean>{
                        updateProgress(-1, 1);
                        updateMessage(i18n.getString("download_lbl_connectionLost"));
                    }
                }catch(Exception e){
                }catch(InterruptedException e){
                    stop = true;
                    logger.error("TimeoutThread crashed: "+e.toString());
                }