private static void copyAssets(Context context) { AssetManager assetManager = context.Assets; String appFileDirectory = context.FilesDir.Path; String executableFilePath = appFileDirectory + "/ffmpeg"; try { var fIn = assetManager.Open("ffmpeg"); Java.IO.File outFile = new Java.IO.File(executableFilePath); OutputStream fOut = null; fOut = new FileOutputStream(outFile); byte[] buffer = new byte[1024]; int read; while ((read = fIn.Read(buffer, 0, 1024)) > 0) { fOut.Write(buffer, 0, read); } fIn.Close(); fIn = null; fOut.Flush(); fOut.Close(); fOut = null; Java.IO.File execFile = new Java.IO.File(executableFilePath); execFile.SetExecutable(true); } catch (Exception e) { //Log.e(TAG, "Failed to copy asset file: " + filename, e); } }
/// <summary> /// /// </summary> /// <param name="context"></param> /// <returns></returns> public async Task Init(Context context, string cdn = null, string downloadTitle = null, string downloadMessage = null) { if (_initialized) { return; } var filesDir = context.FilesDir; _ffmpegFile = new Java.IO.File(filesDir + "/ffmpeg"); if (_ffmpegFile.Exists() && _ffmpegFile.CanExecute()) { _initialized = true; return; } copyAssets(context); if (_ffmpegFile.Exists()) { _initialized = true; } return; if (cdn != null) { CDNHost = cdn; } // do all initialization... _ffmpegFile = new Java.IO.File(filesDir + "/ffmpeg"); FFMpegSource source = FFMpegSource.Get(); await Task.Run(() => { if (_ffmpegFile.Exists() && _ffmpegFile.Length() > 15413150) { try { //if (source.IsHashMatch(System.IO.File.ReadAllBytes(_ffmpegFile.CanonicalPath))) { if (!_ffmpegFile.CanExecute()) { _ffmpegFile.SetExecutable(true); } _initialized = true; return; } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($" Error validating file {ex}"); } // file is not same... // delete the file... if (_ffmpegFile.CanExecute()) { _ffmpegFile.SetExecutable(false); } _ffmpegFile.Delete(); System.Diagnostics.Debug.WriteLine($"ffmpeg file deleted at {_ffmpegFile.AbsolutePath}"); } }); if (_initialized) { // ffmpeg file exists... return; } if (_ffmpegFile.Exists()) { _ffmpegFile.Delete(); } // lets try to download var dialog = new ProgressDialog(context); dialog.SetTitle(downloadMessage ?? "Downloading Video Converter"); //dlg.SetMessage(downloadMessage ?? "Downloading Video Converter"); dialog.Indeterminate = false; dialog.SetProgressStyle(ProgressDialogStyle.Horizontal); dialog.SetCancelable(false); dialog.CancelEvent += (s, e) => { }; dialog.SetCanceledOnTouchOutside(false); dialog.Show(); using (var c = new System.Net.Http.HttpClient()) { using (var fout = System.IO.File.OpenWrite(_ffmpegFile.AbsolutePath)) { string url = source.Url; var g = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, url); var h = await c.SendAsync(g, System.Net.Http.HttpCompletionOption.ResponseHeadersRead); var buffer = new byte[51200]; var s = await h.Content.ReadAsStreamAsync(); long total = h.Content.Headers.ContentLength.GetValueOrDefault(); IEnumerable <string> sl; if (h.Headers.TryGetValues("Content-Length", out sl)) { if (total == 0 && sl.Any()) { long.TryParse(sl.FirstOrDefault(), out total); } } int count = 0; int progress = 0; dialog.Max = (int)total; while ((count = await s.ReadAsync(buffer, 0, buffer.Length)) > 0) { await fout.WriteAsync(buffer, 0, count); progress += count; //System.Diagnostics.Debug.WriteLine($"Downloaded {progress} of {total} from {url}"); dialog.Progress = progress; } dialog.Hide(); } } System.Diagnostics.Debug.WriteLine($"ffmpeg file copied at {_ffmpegFile.AbsolutePath}"); if (!_ffmpegFile.CanExecute()) { _ffmpegFile.SetExecutable(true); System.Diagnostics.Debug.WriteLine($"ffmpeg file made executable"); } _initialized = true; }
static public void CopyBinaries() { string folder = Android.App.Application.Context.GetDir("Tor", FileCreationMode.WorldWriteable).AbsolutePath; string path = folder + "/tor"; if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } FileStream TorDest = System.IO.File.Create(path); using (Stream fs = MainActivity.assets.Open("tor")) { fs.CopyTo(TorDest); } TorDest.Close(); Java.IO.File fileTor = new Java.IO.File(folder + "/tor"); bool result = fileTor.SetExecutable(true); result = fileTor.SetReadable(true); result = fileTor.SetWritable(true); //Android.OS.file //Runtime.GetRuntime().Exec("chmod 755 "+path); //it works! //torrc path = folder + "/torrc"; // if (!System.IO.File.Exists(path)) //{ FileStream TorrcDest = System.IO.File.Create(path); using (Stream fs = MainActivity.assets.Open("torrc")) { fs.CopyTo(TorrcDest); } TorrcDest.Close(); // } Java.IO.File torrcfile = new Java.IO.File(folder + "/torrc"); result = torrcfile.SetReadable(true); result = torrcfile.SetWritable(true); StringBuilder text = new StringBuilder(); BufferedReader br = new BufferedReader(new FileReader(path)); System.String line; while ((line = br.ReadLine()) != null) { text.Append(line); text.Append('\n'); } br.Close(); if (text.IndexOf("/data/data/com.thanksoft.masksurfpro/Tor/data") != -1) { string Temp = text.ToString().Replace("/data/data/com.thanksoft.masksurfpro/Tor/data", folder + "/data"); text.Delete(0, text.Length()); text.Append(Temp); } if (text.IndexOf("/data/data/com.thanksoft.masksurfpro/Tor/geoip") != -1) { string Temp = text.ToString().Replace("/data/data/com.thanksoft.masksurfpro/Tor/geoip", folder + "/data"); text.Delete(0, text.Length()); text.Append(Temp); } if (text.IndexOf("/data/data/com.thanksoft.masksurfpro/Tor/geoip6") != -1) { string Temp = text.ToString().Replace("/data/data/com.thanksoft.masksurfpro/Tor/geoip6", folder + "/geoip6"); text.Delete(0, text.Length()); text.Append(Temp); } BufferedWriter bw = new BufferedWriter(new FileWriter(path)); bw.Write(text.ToString()); bw.Close(); //geoip path = folder + "/geoip"; if (!System.IO.File.Exists(path)) { FileStream GeoIPDest = System.IO.File.Create(path); using (Stream fs = MainActivity.assets.Open("geoip")) { fs.CopyTo(GeoIPDest); } GeoIPDest.Close(); } //geoip path = folder + "/geoip6"; if (!System.IO.File.Exists(path)) { FileStream GeoIP6Dest = System.IO.File.Create(path); using (Stream fs = MainActivity.assets.Open("geoip6")) { fs.CopyTo(GeoIP6Dest); } GeoIP6Dest.Close(); } //data folder path = folder + "data"; if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } //Polipo path = folder + "/polipo"; if (!System.IO.File.Exists(path)) { FileStream PolipoDest = System.IO.File.Create(path); using (Stream fs = MainActivity.assets.Open("polipo")) { fs.CopyTo(PolipoDest); } PolipoDest.Close(); Java.IO.File filePolipo = new Java.IO.File(path); filePolipo.SetExecutable(true); } path = folder + "/polipo.conf"; if (!System.IO.File.Exists(path)) { FileStream PolipoConfDest = System.IO.File.Create(path); using (Stream fs = MainActivity.assets.Open("polipo.conf")) { fs.CopyTo(PolipoConfDest); } PolipoConfDest.Close(); } }