Search in sources :

Example 1 with MessageDataStorage

use of net.iGap.controllers.MessageDataStorage in project iGap-Android by KianIranian-STDG.

the class CdnDownloader method downloadCompleted.

private void downloadCompleted(DownloadObject file) {
    MessageDataStorage storage = MessageDataStorage.getInstance(currentAccount);
    String path = null;
    try {
        if (file.selector == FILE_VALUE) {
            AndroidUtils.cutFromTemp(file.tempFile.getAbsolutePath(), file.destFile.getAbsolutePath());
            storage.setAttachmentFilePath(file.mainCacheId, path = file.destFile.getAbsolutePath(), false);
        } else if (file.selector == SMALL_THUMBNAIL_VALUE || file.selector == LARGE_THUMBNAIL_VALUE) {
            storage.setAttachmentFilePath(file.mainCacheId, path = file.tempFile.getAbsolutePath(), true);
        }
        file.notifyObservers(Resource.success(new HttpRequest.Progress(file, 100, path, file.fileToken, file.selector)));
        requestedDownload.remove(file.key);
        // must be change!
        Downloader.getInstance(currentAccount).onCdnDownloadComplete(file.mainCacheId);
    } catch (IOException e) {
        handleError(file, e.getMessage());
    }
}
Also used : Progress(com.downloader.Progress) IOException(java.io.IOException) MessageDataStorage(net.iGap.controllers.MessageDataStorage)

Example 2 with MessageDataStorage

use of net.iGap.controllers.MessageDataStorage in project iGap-Android by KianIranian-STDG.

the class HttpRequest method moveTempToDownloadedDir.

@WorkerThread
private void moveTempToDownloadedDir() throws IOException {
    MessageDataStorage storage = MessageDataStorage.getInstance(currentAccount);
    switch(selector) {
        case Selector.FILE_VALUE:
            AndroidUtils.globalQueue.postRunnable(() -> {
                File cutTo = new File(fileObject.destFile.getAbsolutePath());
                if (!cutTo.exists()) {
                    try {
                        cutTo.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                File cutForm = new File(fileObject.tempFile.getAbsolutePath());
                try (InputStream in = new FileInputStream(cutForm);
                    OutputStream out = new FileOutputStream(cutTo)) {
                    // Transfer bytes from in to out
                    byte[] buf = new byte[1024];
                    int len;
                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                    AndroidUtils.deleteFile(cutForm);
                    G.runOnUiThread(() -> {
                        notifyObservers(Resource.success(new Progress(fileObject, fileObject.progress, selector == Selector.FILE_VALUE ? fileObject.destFile.getAbsolutePath() : fileObject.tempFile.getAbsolutePath(), fileObject.fileToken, selector)));
                        notifyDownloadStatus(HttpDownloader.DownloadStatus.DOWNLOADED);
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            storage.setAttachmentFilePath(fileObject.mainCacheId, fileObject.destFile.getAbsolutePath(), false);
            break;
        case Selector.SMALL_THUMBNAIL_VALUE:
        case Selector.LARGE_THUMBNAIL_VALUE:
            storage.setAttachmentFilePath(fileObject.mainCacheId, fileObject.tempFile.getAbsolutePath(), true);
            notifyObservers(Resource.success(new Progress(fileObject, fileObject.progress, selector == Selector.FILE_VALUE ? fileObject.destFile.getAbsolutePath() : fileObject.tempFile.getAbsolutePath(), fileObject.fileToken, selector)));
            notifyDownloadStatus(HttpDownloader.DownloadStatus.DOWNLOADED);
            break;
    }
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) MessageDataStorage(net.iGap.controllers.MessageDataStorage) FileInputStream(java.io.FileInputStream) WorkerThread(androidx.annotation.WorkerThread)

Aggregations

IOException (java.io.IOException)2 MessageDataStorage (net.iGap.controllers.MessageDataStorage)2 WorkerThread (androidx.annotation.WorkerThread)1 Progress (com.downloader.Progress)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 CipherInputStream (javax.crypto.CipherInputStream)1