package com.google.android.gms.internal.firebase_ml; import com.google.android.gms.common.internal.GmsLogger; import com.google.firebase.FirebaseApp; import com.google.firebase.ml.common.FirebaseMLException; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.nio.charset.Charset; import java.util.Iterator; import java.util.List; /* loaded from: classes2.dex */ public final class zzob implements zzoh { private static final GmsLogger zzaoz = new GmsLogger("AutoMLModelFileManager", ""); private final FirebaseApp zzapo; private final String zzaqg; /* JADX INFO: Access modifiers changed from: package-private */ public zzob(FirebaseApp firebaseApp, String str) { this.zzapo = firebaseApp; this.zzaqg = str; } @Override // com.google.android.gms.internal.firebase_ml.zzoh public final void zzlt() throws FirebaseMLException { File zzj = zzj(this.zzapo, this.zzaqg); if (zzoq.zze(zzj.getParentFile())) { return; } GmsLogger gmsLogger = zzaoz; String valueOf = String.valueOf(zzj.getParentFile().getAbsolutePath()); gmsLogger.e("AutoMLModelFileManager", valueOf.length() != 0 ? "Failed to delete the temp labels file directory: ".concat(valueOf) : new String("Failed to delete the temp labels file directory: ")); } @Override // com.google.android.gms.internal.firebase_ml.zzoh public final File zza(File file) throws FirebaseMLException { File zzb = zzoq.zzb(this.zzapo, this.zzaqg, zzok.AUTOML); File file2 = new File(new File(zzb, String.valueOf(zzoq.zzd(zzb) + 1)), "model.tflite"); File parentFile = file2.getParentFile(); if (!parentFile.exists()) { parentFile.mkdirs(); } File zzj = zzj(this.zzapo, this.zzaqg); File file3 = new File(parentFile, "labels.txt"); if (file.renameTo(file2) && zzj.renameTo(file3)) { zzaoz.d("AutoMLModelFileManager", "Rename to serving model successfully"); file2.setExecutable(false); file2.setWritable(false); file3.setExecutable(false); file3.setWritable(false); File file4 = new File(parentFile, "manifest.json"); final String format = String.format("{\n\t\"modelType\": \"%s\",\n\t\"modelFile\": \"%s\",\n\t\"labelsFile\": \"%s\"\n}", "IMAGE_LABELING", "model.tflite", "labels.txt"); try { zza(file4, new zzoe(format) { // from class: com.google.android.gms.internal.firebase_ml.zzoc private final String zzaqh; @Override // com.google.android.gms.internal.firebase_ml.zzoe public final void zza(BufferedWriter bufferedWriter) { bufferedWriter.write(this.zzaqh); } /* JADX INFO: Access modifiers changed from: package-private */ { this.zzaqh = format; } }); return file2.getParentFile(); } catch (IOException e) { String valueOf = String.valueOf(this.zzaqg); throw new FirebaseMLException(valueOf.length() != 0 ? "Failed to write manifest json for the AutoML model: ".concat(valueOf) : new String("Failed to write manifest json for the AutoML model: "), 13, e); } } GmsLogger gmsLogger = zzaoz; gmsLogger.d("AutoMLModelFileManager", "Rename to serving model failed, remove the temp file."); if (!file.delete()) { String valueOf2 = String.valueOf(file.getAbsolutePath()); gmsLogger.d("AutoMLModelFileManager", valueOf2.length() != 0 ? "Failed to delete the temp model file: ".concat(valueOf2) : new String("Failed to delete the temp model file: ")); } if (zzj.delete()) { return null; } String valueOf3 = String.valueOf(zzj.getAbsolutePath()); gmsLogger.d("AutoMLModelFileManager", valueOf3.length() != 0 ? "Failed to delete the temp labels file: ".concat(valueOf3) : new String("Failed to delete the temp labels file: ")); return null; } /* JADX INFO: Access modifiers changed from: package-private */ public static void zza(FirebaseApp firebaseApp, String str, final List list) throws FirebaseMLException { try { zza(zzj(firebaseApp, str), new zzoe(list) { // from class: com.google.android.gms.internal.firebase_ml.zzod private final List zzaqi; @Override // com.google.android.gms.internal.firebase_ml.zzoe public final void zza(BufferedWriter bufferedWriter) { zzob.zza(this.zzaqi, bufferedWriter); } /* JADX INFO: Access modifiers changed from: package-private */ { this.zzaqi = list; } }); } catch (IOException e) { String valueOf = String.valueOf(str); throw new FirebaseMLException(valueOf.length() != 0 ? "Failed to write labels file for the AutoML model: ".concat(valueOf) : new String("Failed to write labels file for the AutoML model: "), 13, e); } } private static File zzj(FirebaseApp firebaseApp, String str) throws FirebaseMLException { File zza = zzoq.zza(firebaseApp, str, zzok.AUTOML); if (zza.exists() && zza.isFile() && !zza.delete()) { String valueOf = String.valueOf(zza.getAbsolutePath()); throw new FirebaseMLException(valueOf.length() != 0 ? "Failed to delete the temp labels file: ".concat(valueOf) : new String("Failed to delete the temp labels file: "), 13); } if (!zza.exists()) { GmsLogger gmsLogger = zzaoz; String valueOf2 = String.valueOf(zza.getAbsolutePath()); gmsLogger.d("AutoMLModelFileManager", valueOf2.length() != 0 ? "Temp labels folder does not exist, creating one: ".concat(valueOf2) : new String("Temp labels folder does not exist, creating one: ")); if (!zza.mkdirs()) { throw new FirebaseMLException("Failed to create a directory to hold the AutoML model's labels file.", 13); } } return new File(zza, "labels.txt"); } private static void zza(File file, zzoe zzoeVar) throws IOException { BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), Charset.forName("UTF-8"))); try { zzoeVar.zza(bufferedWriter); bufferedWriter.close(); } catch (Throwable th) { try { throw th; } catch (Throwable th2) { try { bufferedWriter.close(); } catch (Throwable th3) { zzlx.zza(th, th3); } throw th2; } } } /* JADX INFO: Access modifiers changed from: package-private */ public static final /* synthetic */ void zza(List list, BufferedWriter bufferedWriter) throws IOException { Iterator it = list.iterator(); while (it.hasNext()) { bufferedWriter.write((String) it.next()); bufferedWriter.newLine(); } } }