Search in sources :

Example 1 with VideoCacheFileNameGenerator

use of com.dante.utils.VideoCacheFileNameGenerator in project 91Pop by DanteAndroid.

the class DownloadPresenter method copyCacheFile.

/**
 * 直接拷贝缓存好的视频即可
 *
 * @param unLimit91PornItem
 */
private void copyCacheFile(final File videoCacheDir, final UnLimit91PornItem unLimit91PornItem, final DownloadListener downloadListener) throws IOException {
    Observable.create(new ObservableOnSubscribe<File>() {

        @Override
        public void subscribe(ObservableEmitter<File> e) throws Exception {
            VideoCacheFileNameGenerator myFileNameGenerator = new VideoCacheFileNameGenerator();
            String cacheFileName = myFileNameGenerator.generate(unLimit91PornItem.getVideoResult().getVideoUrl());
            File fromFile = new File(videoCacheDir, cacheFileName);
            if (!fromFile.exists() || fromFile.length() <= 0) {
                e.onError(new Exception("缓存文件错误,无法拷贝"));
            }
            e.onNext(fromFile);
            e.onComplete();
        }
    }).map(new Function<File, UnLimit91PornItem>() {

        @Override
        public UnLimit91PornItem apply(File fromFile) throws Exception {
            File toFile = new File(unLimit91PornItem.getDownLoadPath(dataManager));
            if (toFile.exists() && toFile.length() > 0) {
                throw new Exception("已经下载过了");
            } else {
                if (!toFile.createNewFile()) {
                    throw new Exception("创建文件失败");
                }
            }
            FileUtils.copyFile(fromFile, toFile);
            unLimit91PornItem.setTotalFarBytes((int) fromFile.length());
            unLimit91PornItem.setSoFarBytes((int) fromFile.length());
            return unLimit91PornItem;
        }
    }).map(new Function<UnLimit91PornItem, String>() {

        @Override
        public String apply(UnLimit91PornItem unLimit91PornItem) throws Exception {
            unLimit91PornItem.setStatus(FileDownloadStatus.completed);
            unLimit91PornItem.setProgress(100);
            unLimit91PornItem.setFinishedDownloadDate(new Date());
            unLimit91PornItem.setDownloadId(FileDownloadUtils.generateId(unLimit91PornItem.getVideoResult().getVideoUrl(), unLimit91PornItem.getDownLoadPath(dataManager)));
            dataManager.updateUnLimit91PornItem(unLimit91PornItem);
            return "下载完成";
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).compose(provider.<String>bindUntilEvent(Lifecycle.Event.ON_DESTROY)).subscribe(new CallBackWrapper<String>() {

        @Override
        public void onBegin(Disposable d) {
        }

        @Override
        public void onSuccess(final String s) {
            if (downloadListener != null) {
                downloadListener.onSuccess(s);
            } else {
                ifViewAttached(new ViewAction<DownloadView>() {

                    @Override
                    public void run(@NonNull DownloadView view) {
                        view.showMessage(s, TastyToast.SUCCESS);
                    }
                });
            }
        }

        @Override
        public void onError(final String msg, int code) {
            if (downloadListener != null) {
                downloadListener.onError(msg);
            } else {
                ifViewAttached(new ViewAction<DownloadView>() {

                    @Override
                    public void run(@NonNull DownloadView view) {
                        view.showMessage(msg, TastyToast.ERROR);
                    }
                });
            }
        }
    });
}
Also used : Disposable(io.reactivex.disposables.Disposable) VideoCacheFileNameGenerator(com.dante.utils.VideoCacheFileNameGenerator) IOException(java.io.IOException) Date(java.util.Date) Function(io.reactivex.functions.Function) NonNull(android.support.annotation.NonNull) File(java.io.File) UnLimit91PornItem(com.dante.data.model.UnLimit91PornItem)

Aggregations

NonNull (android.support.annotation.NonNull)1 UnLimit91PornItem (com.dante.data.model.UnLimit91PornItem)1 VideoCacheFileNameGenerator (com.dante.utils.VideoCacheFileNameGenerator)1 Disposable (io.reactivex.disposables.Disposable)1 Function (io.reactivex.functions.Function)1 File (java.io.File)1 IOException (java.io.IOException)1 Date (java.util.Date)1