Browse Source

优化文件缓存逻辑

zk 1 year ago
parent
commit
ac04761003

+ 49 - 5
app/src/main/java/com/atmob/voiceai/utils/DownloadUtils.java

@@ -30,15 +30,12 @@ public class DownloadUtils {
                 && Environment.getExternalStorageDirectory().canWrite()) {
             cacheDir = ContextUtil.getApplication().getExternalCacheDir();
         } else {
-            cacheDir = ContextUtil.getApplication().getCacheDir();
+            cacheDir = ContextUtil.getApplication().getExternalCacheDir();
+//            cacheDir = ContextUtil.getApplication().getCacheDir();
         }
         return cacheDir;
     }
 
-    public static void downLoad(String url, final File rootFile, final String fileName,
-                                final FileDownLoadObserver<File> fileDownLoadObserver) {
-        downLoad(new OkHttpClient(), url, rootFile, fileName, fileDownLoadObserver);
-    }
 
     public static void downLoad(OkHttpClient client, String url, final File rootFile, final String fileName,
                                 final FileDownLoadObserver<File> fileDownLoadObserver) {
@@ -77,4 +74,51 @@ public class DownloadUtils {
     }
 
 
+    public static void downLoadCompareBeforeLocal(OkHttpClient client, String url, final File localFile, final File rootFile, final String fileName,
+                                                  final FileDownLoadObserver<File> fileDownLoadObserver) {
+        Observable.create((ObservableOnSubscribe<Object>) emitter -> {
+                    Request request = new Request.Builder()
+                            .url(url)
+                            .build();
+                    try (Response response = client.newCall(request).execute()) {
+                        if (!response.isSuccessful()) {
+                            emitter.onError(new IOException("Failed to download audio: " + response));
+                            return;
+                        }
+                        ResponseBody body = response.body();
+                        if (body == null) {
+                            emitter.onError(new IOException("Response body is null"));
+                            return;
+                        }
+//                        AtmobLog.d("zk", String.format("文件类型为%s,文件大小为%d", response.body().contentType().toString(), response.body().contentLength()));
+                        if (localFile == null || !localFile.exists() || localFile.length() != body.contentLength()) {
+                            emitter.onNext(body);
+                        } else {
+                            emitter.onNext(localFile);
+                        }
+                        emitter.onComplete();
+                    } catch (IOException e) {
+                        emitter.onError(e);
+                    }
+                })
+                .map(data -> {
+                    if (data instanceof File) {
+                        return (File) data;
+                    } else {
+                        ResponseBody responseBody = (ResponseBody) data;
+                        return fileDownLoadObserver.saveFile(responseBody,
+                                rootFile.getPath(), fileName);
+                    }
+                })
+                .subscribeOn(Schedulers.io())
+                .observeOn(Schedulers.io())
+                .map(file -> {
+                    if (file == null) {
+                        throw new IOException("file is null");
+                    }
+                    return file;
+                })
+                .subscribe(fileDownLoadObserver);
+    }
+
 }

+ 18 - 22
app/src/main/java/com/atmob/voiceai/utils/VoiceFileUtil.java

@@ -22,28 +22,24 @@ public class VoiceFileUtil {
      */
     public static Single<File> getVoiceFile(OkHttpClient okHttpClient, @NonNull String url) {
         return Single.create((SingleOnSubscribe<File>) emitter -> {
-                    File file = net2LocalFile(url);
-                    if (file.exists()) {
-                        emitter.onSuccess(file);
-                    } else {
-                        DownloadUtils.downLoad(okHttpClient, url, DownloadUtils.voiceFile, getFileName(url), new FileDownLoadObserver<File>() {
-                            @Override
-                            public void onDownLoadStart() {
-
-                            }
-
-                            @Override
-                            public void onDownLoadSuccess(File file) {
-                                emitter.onSuccess(file);
-                            }
-
-                            @Override
-                            public void onDownLoadFail(Throwable throwable) {
-                                emitter.onError(throwable);
-                            }
-
-                        });
-                    }
+                    File localFile = net2LocalFile(url);
+                    DownloadUtils.downLoadCompareBeforeLocal(okHttpClient, url, localFile, DownloadUtils.voiceFile, getFileName(url), new FileDownLoadObserver<File>() {
+                        @Override
+                        public void onDownLoadStart() {
+
+                        }
+
+                        @Override
+                        public void onDownLoadSuccess(File file) {
+                            emitter.onSuccess(file);
+                        }
+
+                        @Override
+                        public void onDownLoadFail(Throwable throwable) {
+                            emitter.onError(throwable);
+                        }
+
+                    });
                 })
                 .compose(RxJavaUtil.SingleSchedule.io2Main());
     }