Search in sources :

Example 11 with FileDownloadModel

use of com.liulishuo.filedownloader.model.FileDownloadModel in project FileDownloader by lingochamp.

the class SqliteDatabaseImpl method createFromCursor.

private static FileDownloadModel createFromCursor(Cursor c) {
    final FileDownloadModel model = new FileDownloadModel();
    model.setId(c.getInt(c.getColumnIndex(FileDownloadModel.ID)));
    model.setUrl(c.getString(c.getColumnIndex(FileDownloadModel.URL)));
    model.setPath(c.getString(c.getColumnIndex(FileDownloadModel.PATH)), c.getShort(c.getColumnIndex(FileDownloadModel.PATH_AS_DIRECTORY)) == 1);
    model.setStatus((byte) c.getShort(c.getColumnIndex(FileDownloadModel.STATUS)));
    model.setSoFar(c.getLong(c.getColumnIndex(FileDownloadModel.SOFAR)));
    model.setTotal(c.getLong(c.getColumnIndex(FileDownloadModel.TOTAL)));
    model.setErrMsg(c.getString(c.getColumnIndex(FileDownloadModel.ERR_MSG)));
    model.setETag(c.getString(c.getColumnIndex(FileDownloadModel.ETAG)));
    model.setFilename(c.getString(c.getColumnIndex(FileDownloadModel.FILENAME)));
    model.setConnectionCount(c.getInt(c.getColumnIndex(FileDownloadModel.CONNECTION_COUNT)));
    return model;
}
Also used : FileDownloadModel(com.liulishuo.filedownloader.model.FileDownloadModel)

Example 12 with FileDownloadModel

use of com.liulishuo.filedownloader.model.FileDownloadModel in project FileDownloader by lingochamp.

the class CustomComponentHolder method maintainDatabase.

private static void maintainDatabase(FileDownloadDatabase.Maintainer maintainer) {
    final Iterator<FileDownloadModel> iterator = maintainer.iterator();
    long refreshDataCount = 0;
    long removedDataCount = 0;
    long resetIdCount = 0;
    final FileDownloadHelper.IdGenerator idGenerator = getImpl().getIdGeneratorInstance();
    final long startTimestamp = System.currentTimeMillis();
    try {
        while (iterator.hasNext()) {
            boolean isInvalid = false;
            final FileDownloadModel model = iterator.next();
            do {
                if (model.getStatus() == FileDownloadStatus.progress || model.getStatus() == FileDownloadStatus.connected || model.getStatus() == FileDownloadStatus.error || (model.getStatus() == FileDownloadStatus.pending && model.getSoFar() > 0)) {
                    // Ensure can be covered by RESUME FROM BREAKPOINT.
                    model.setStatus(FileDownloadStatus.paused);
                }
                final String targetFilePath = model.getTargetFilePath();
                if (targetFilePath == null) {
                    // no target file path, can't used to resume from breakpoint.
                    isInvalid = true;
                    break;
                }
                final File targetFile = new File(targetFilePath);
                // sync
                if (model.getStatus() == FileDownloadStatus.paused && FileDownloadUtils.isBreakpointAvailable(model.getId(), model, model.getPath(), null)) {
                    // can be reused in the old mechanism(no-temp-file).
                    final File tempFile = new File(model.getTempFilePath());
                    if (!tempFile.exists() && targetFile.exists()) {
                        final boolean successRename = targetFile.renameTo(tempFile);
                        if (FileDownloadLog.NEED_LOG) {
                            FileDownloadLog.d(FileDownloadDatabase.class, "resume from the old no-temp-file architecture " + "[%B], [%s]->[%s]", successRename, targetFile.getPath(), tempFile.getPath());
                        }
                    }
                }
                /**
                 * Remove {@code model} from DB if it can't used for judging whether the
                 * old-downloaded file is valid for reused & it can't used for resuming from
                 * BREAKPOINT, In other words, {@code model} is no use anymore for
                 * FileDownloader.
                 */
                if (model.getStatus() == FileDownloadStatus.pending && model.getSoFar() <= 0) {
                    // This model is redundant.
                    isInvalid = true;
                    break;
                }
                if (!FileDownloadUtils.isBreakpointAvailable(model.getId(), model)) {
                    // It can't used to resuming from breakpoint.
                    isInvalid = true;
                    break;
                }
                if (targetFile.exists()) {
                    // It has already completed downloading.
                    isInvalid = true;
                    break;
                }
            } while (false);
            if (isInvalid) {
                iterator.remove();
                maintainer.onRemovedInvalidData(model);
                removedDataCount++;
            } else {
                final int oldId = model.getId();
                final int newId = idGenerator.transOldId(oldId, model.getUrl(), model.getPath(), model.isPathAsDirectory());
                if (newId != oldId) {
                    if (FileDownloadLog.NEED_LOG) {
                        FileDownloadLog.d(FileDownloadDatabase.class, "the id is changed on restoring from db:" + " old[%d] -> new[%d]", oldId, newId);
                    }
                    model.setId(newId);
                    maintainer.changeFileDownloadModelId(oldId, model);
                    resetIdCount++;
                }
                maintainer.onRefreshedValidData(model);
                refreshDataCount++;
            }
        }
    } finally {
        FileDownloadUtils.markConverted(FileDownloadHelper.getAppContext());
        maintainer.onFinishMaintain();
        // 566 data consumes about 140ms
        if (FileDownloadLog.NEED_LOG) {
            FileDownloadLog.d(FileDownloadDatabase.class, "refreshed data count: %d , delete data count: %d, reset id count:" + " %d. consume %d", refreshDataCount, removedDataCount, resetIdCount, System.currentTimeMillis() - startTimestamp);
        }
    }
}
Also used : FileDownloadHelper(com.liulishuo.filedownloader.util.FileDownloadHelper) FileDownloadModel(com.liulishuo.filedownloader.model.FileDownloadModel) File(java.io.File)

Example 13 with FileDownloadModel

use of com.liulishuo.filedownloader.model.FileDownloadModel in project FileDownloader by lingochamp.

the class DownloadLaunchRunnable method checkupAfterGetFilename.

private void checkupAfterGetFilename() throws RetryDirectly, DiscardSafely {
    final int id = model.getId();
    if (model.isPathAsDirectory()) {
        // this scope for caring about the case of there is another task is provided
        // the same path to store file and the same url.
        final String targetFilePath = model.getTargetFilePath();
        // get the ID after got the filename.
        final int fileCaseId = FileDownloadUtils.generateId(model.getUrl(), targetFilePath);
        // whether the file with the filename has been existed.
        if (FileDownloadHelper.inspectAndInflowDownloaded(id, targetFilePath, isForceReDownload, false)) {
            database.remove(id);
            database.removeConnections(id);
            throw new DiscardSafely();
        }
        final FileDownloadModel fileCaseModel = database.find(fileCaseId);
        if (fileCaseModel != null) {
            // whether the another task with the same file and url is downloading.
            if (FileDownloadHelper.inspectAndInflowDownloading(id, fileCaseModel, threadPoolMonitor, false)) {
                // it has been post to upper layer the 'warn' message, so the current
                // task no need to continue download.
                database.remove(id);
                database.removeConnections(id);
                throw new DiscardSafely();
            }
            final List<ConnectionModel> connectionModelList = database.findConnectionModel(fileCaseId);
            // the another task with the same file name and url is paused
            database.remove(fileCaseId);
            database.removeConnections(fileCaseId);
            FileDownloadUtils.deleteTargetFile(model.getTargetFilePath());
            if (FileDownloadUtils.isBreakpointAvailable(fileCaseId, fileCaseModel)) {
                model.setSoFar(fileCaseModel.getSoFar());
                model.setTotal(fileCaseModel.getTotal());
                model.setETag(fileCaseModel.getETag());
                model.setConnectionCount(fileCaseModel.getConnectionCount());
                database.update(model);
                // re connect to resume from breakpoint.
                if (connectionModelList != null) {
                    for (ConnectionModel connectionModel : connectionModelList) {
                        connectionModel.setId(id);
                        database.insertConnectionModel(connectionModel);
                    }
                }
                // retry
                throw new RetryDirectly();
            }
        }
        // whether there is an another running task with the same target-file-path.
        if (FileDownloadHelper.inspectAndInflowConflictPath(id, model.getSoFar(), model.getTempFilePath(), targetFilePath, threadPoolMonitor)) {
            database.remove(id);
            database.removeConnections(id);
            throw new DiscardSafely();
        }
    }
}
Also used : FileDownloadModel(com.liulishuo.filedownloader.model.FileDownloadModel) ConnectionModel(com.liulishuo.filedownloader.model.ConnectionModel)

Aggregations

FileDownloadModel (com.liulishuo.filedownloader.model.FileDownloadModel)13 ConnectionModel (com.liulishuo.filedownloader.model.ConnectionModel)3 File (java.io.File)2 Application (android.app.Application)1 Cursor (android.database.Cursor)1 IThreadPoolMonitor (com.liulishuo.filedownloader.IThreadPoolMonitor)1 FileDownloadConnection (com.liulishuo.filedownloader.connection.FileDownloadConnection)1 FileDownloadDatabase (com.liulishuo.filedownloader.database.FileDownloadDatabase)1 DownloadLaunchRunnable (com.liulishuo.filedownloader.download.DownloadLaunchRunnable)1 FileDownloadGiveUpRetryException (com.liulishuo.filedownloader.exception.FileDownloadGiveUpRetryException)1 FileDownloadHttpException (com.liulishuo.filedownloader.exception.FileDownloadHttpException)1 FileDownloadNetworkPolicyException (com.liulishuo.filedownloader.exception.FileDownloadNetworkPolicyException)1 FileDownloadHeader (com.liulishuo.filedownloader.model.FileDownloadHeader)1 FileDownloadHelper (com.liulishuo.filedownloader.util.FileDownloadHelper)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Test (org.junit.Test)1