public static void ExtractToDirectory(string sourceArchiveFileName, string destinationDirectoryName, IProgress <double> progress) { using (ZipArchive archive = ZipFile.OpenRead(sourceArchiveFileName)) { double totalBytes = archive.Entries.Sum(e => e.Length); long currentBytes = 0; foreach (ZipArchiveEntry entry in archive.Entries) { string fileName = Path.Combine(destinationDirectoryName, entry.FullName); Directory.CreateDirectory(Path.GetDirectoryName(fileName)); using (Stream inputStream = entry.Open()) using (Stream outputStream = File.OpenWrite(fileName)) { Stream progressStream = new StreamWithProgress(outputStream, null, new BasicProgress <int>(i => { currentBytes += i; progress.Report(currentBytes / totalBytes); })); inputStream.CopyTo(progressStream); } File.SetLastWriteTime(fileName, entry.LastWriteTime.LocalDateTime); } } }
public static void CreateFromDirectory(string sourceDirectoryName, string destinationArchiveFileName, IProgress <double> progress) { sourceDirectoryName = Path.GetFullPath(sourceDirectoryName); FileInfo[] sourceFiles = new DirectoryInfo(sourceDirectoryName).GetFiles("*", SearchOption.AllDirectories); long totalBytes = sourceFiles.Sum(f => f.Length), currentBytes = 0; using (ZipArchive archive = ZipFile.Open(destinationArchiveFileName, ZipArchiveMode.Create)) { foreach (FileInfo file in sourceFiles) { // NOTE: naive method to get sub-path from file name, relative to // input directory. Production code should be more robust than this. // Either use Path class or similar to parse directory separators and // reconstruct output file name, or change this entire method to be // recursive so that it can follow the sub-directories and include them // in the entry name as they are processed. string entryName = file.FullName.Substring(sourceDirectoryName.Length + 1); ZipArchiveEntry entry = archive.CreateEntry(entryName); using (Stream inputStream = File.OpenRead(file.FullName)) using (Stream outputStream = entry.Open()) { Stream progressStream = new StreamWithProgress(inputStream, new BasicProgress <int>(i => { currentBytes += i; progress.Report((double)currentBytes / totalBytes); }), null); progressStream.CopyTo(outputStream); } } } }
static void AppendFolderToZip(string path, ZipArchive zip, Action <string> uiReporter, string zipPath = "") { string[] directories = Directory.GetDirectories(path); foreach (string subdirectory in directories) { AppendFolderToZip(subdirectory, zip, uiReporter, zipPath + subdirectory.Substring(subdirectory.LastIndexOf('\\') + 1) + '\\'); } string[] allFiles = Directory.GetFiles(path); foreach (string asset in allFiles) { if (!asset.EndsWith(".mkv") && !asset.EndsWith(".zip")) { string entryName = Path.GetFileName(asset); ZipArchiveEntry entry = zip.CreateEntry(zipPath + entryName); entry.LastWriteTime = DateTime.Now; using (Stream inputStream = File.OpenRead(asset)) using (Stream outputStream = entry.Open()) using (Stream progressStream = new StreamWithProgress(inputStream, new FileProgressDisplay(entryName, inputStream.Length, uiReporter), null)) progressStream.CopyTo(outputStream); } } }
async void Upload() { IFileTransferService proxy = fileTransferServiceFactory.CreateChannel(); try { string fileName = "f:\\xxx.mp4"; // get some info about the input file System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileName); // open input stream using (System.IO.FileStream stream = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { using (StreamWithProgress uploadStreamWithProgress = new StreamWithProgress(stream)) { uploadStreamWithProgress.ProgressChanged += uploadStreamWithProgress_ProgressChanged; // start service client // upload file proxy.UploadFileAsync(fileInfo.Name, fileInfo.Length, uploadStreamWithProgress); // close service client } } } catch (FaultException <SessionFault> sf) { string message = sf.Detail.SessionMessage; string opertaion = sf.Detail.SessionOperation; string reson = sf.Detail.SessionReason; } ((IClientChannel)proxy).Close(); }
private static void uploadDatabase(SyncServiceClient serviceClient) { string dbPath = Path.Combine(Constants.GetCurrentDirectoryPath, Constants.DOWNLOAD_FOLDER, Constants.REMOTE_DB_NAME); // get some info about the input file var fileInfo = new FileInfo(dbPath); FileStream stream = null; try { // open input stream stream = new FileStream(dbPath, FileMode.Open, FileAccess.Read); using (var uploadStreamWithProgress = new StreamWithProgress(stream)) { stream = null; uploadStreamWithProgress.ProgressChanged += onUploadStreamWithProgressProgressChanged; // upload file serviceClient.UploadFile(fileInfo.Name, fileInfo.Length, uploadStreamWithProgress); } } catch (Exception e) { MessageBox.Show(e.Message); } finally { if (stream != null) { stream.Dispose(); } } }
private void ProcessDownloadQueue() { if (_isProcessingDownload) { return; } _isProcessingDownload = true; while ((_currentAudioBook = GetNextDownloadItem()) != null) { if (_currentAudioBook == null) { _isProcessingDownload = false; return; } _cancel = false; var link = AsyncHelper.RunSync(() => AudioLibrary.Instance.GetAudioBookLink(_currentAudioBook.Book.Id)); if (link == null) { _currentAudioBook.StatusDescription = "Enlace de descarga no encontrado"; _currentAudioBook.StatusKey = STATUS_ERROR; _isProcessingDownload = false; // Remove download from the list int index = _audioBooks.FindIndex(f => f.Book.Id == _currentAudioBook.Book.Id); if (index > -1) { _audioBooks.RemoveAt(index); } AsyncHelper.RunSync(() => SaveBooks()); OnProgress?.Invoke(_currentAudioBook); return; } // download file var zipFile = $"{Session.Instance.GetDataDir()}/{_currentAudioBook.Book.Id}.zip"; if (File.Exists(zipFile)) { File.Delete(zipFile); } // Initialize web client var webClient = new WebClient(); webClient.DownloadProgressChanged += _webClient_DownloadProgressChanged; webClient.DownloadFileCompleted += _webClient_DownloadFileCompleted; webClient.DownloadFileAsync(new Uri(link), zipFile); while (webClient.IsBusy) { if (_cancel) { // Remove download from the list int index = _audioBooks.FindIndex(f => f.Book.Id == _currentAudioBook.Book.Id); if (index > -1) { _audioBooks.RemoveAt(index); } AsyncHelper.RunSync(() => SaveBooks()); webClient.CancelAsync(); break; } Thread.Sleep(100); } if (_cancel) { _cancel = false; } else { _currentAudioBook.StatusDescription = "Preparando audiolibro"; _currentAudioBook.StatusKey = STATUS_INSTALLING; _currentAudioBook.TmpFolder = $"{DateTime.Now.Ticks.ToString()}"; AsyncHelper.RunSync(() => SaveBooks()); OnProgress?.Invoke(_currentAudioBook); try { // Unzip using (ZipArchive archive = ZipFile.OpenRead(zipFile)) { double totalBytes = archive.Entries.Sum(e => e.Length); long currentBytes = 0; var targetPath = $"{Session.Instance.GetDataDir()}/{_currentAudioBook.Book.Id}"; // clean up if exists if (Directory.Exists(targetPath)) { Directory.Delete(targetPath, true); } Directory.CreateDirectory(targetPath); foreach (ZipArchiveEntry entry in archive.Entries) { if (!string.IsNullOrEmpty(entry.Name)) { string fileName = Path.Combine($"{targetPath}", Path.GetFileName(entry.FullName)); using (Stream inputStream = entry.Open()) using (Stream outputStream = File.OpenWrite(fileName)) { Stream progressStream = new StreamWithProgress(outputStream, null, new BasicProgress <int>(i => { currentBytes += i; _currentAudioBook.Progress = (int)(currentBytes / totalBytes); OnProgress?.Invoke(_currentAudioBook); }) ); inputStream.CopyTo(progressStream); } File.SetLastWriteTime(fileName, entry.LastWriteTime.LocalDateTime); } } } } catch { _isProcessingDownload = false; _currentAudioBook.StatusKey = STATUS_ERROR; _currentAudioBook.StatusDescription = "Error: no se ha podido extraer el contendido del audiolibro"; OnProgress?.Invoke(_currentAudioBook); AsyncHelper.RunSync(() => SaveBooks()); return; } // Read daisy format and generate a ncc.json file with all the book content prepared for the audio player var nccIndex = $"{Session.Instance.GetDataDir()}/{_currentAudioBook.Book.Id}/ncc.html"; if (!File.Exists(nccIndex)) { _isProcessingDownload = false; _currentAudioBook.StatusKey = STATUS_ERROR; _currentAudioBook.StatusDescription = "Error: formato del audiolibro incorrecto. Índice no encontrado."; OnProgress?.Invoke(_currentAudioBook); AsyncHelper.RunSync(() => SaveBooks()); return; } DaisyBook dbook = new DaisyBook(); dbook.Load(nccIndex); dbook.Id = _currentAudioBook.Book.Id; string dbookStr = JsonConvert.SerializeObject(dbook); File.WriteAllText($"{Session.Instance.GetDataDir()}/{_currentAudioBook.Book.Id}/ncc.json", dbookStr); // clean up .zip file File.Delete($"{Session.Instance.GetDataDir()}/{_currentAudioBook.Book.Id}.zip"); _currentAudioBook.StatusKey = STATUS_COMPLETED; _currentAudioBook.StatusDescription = "Completado"; OnProgress?.Invoke(_currentAudioBook); AsyncHelper.RunSync(() => SaveBooks()); } _isProcessingDownload = false; } }
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { using (var scope = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope()) { scope.ServiceProvider.GetService <ApplicationDbContext>().Database.Migrate(); } var pipeline = new AggregatedFilterPipeline(); app .UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto, KnownNetworks = { new IPNetwork(new IPAddress(new byte[] { 127, 0, 0, 1 }), 32) } }) .Use(async(context, next) => { var flareContext = new FlaggableFlareContext(new FlareContext() { request = new FlareRequest() { date = DateTimeOffset.Now, http_version = (int)(double.Parse(context.Request.Protocol.Substring(5)) * 10), identity = "-", ip = context.Connection.RemoteIpAddress.ToString(), method = context.Request.Method, path = context.Request.Path.Value, query_string = context.Request.QueryString.Value, userid = "-" }, response = null }); var domain = context.Request.Headers["host"]; // Flare CDN logic if (domain != "localhost:5000" && domain != "api.flare.wtf") { var db = context.RequestServices.GetRequiredService <ApplicationDbContext>(); var server = await db.servers .Where(a => a.proxy_active && a.domains.Any(b => b.domain == domain)) .FirstOrDefaultAsync(); if (server == null) { context.Response.StatusCode = 404; await context.Response.WriteAsync("The requested resource is not found. That's all we know."); return; } var ip = await context.RequestServices.GetRequiredService <GeoIpService>() .Query(flareContext.Context.request.ip); var db_request = new request { server_id = server.id, ip_id = ip.id, request_identity = flareContext.Context.request.identity, request_user_id = flareContext.Context.request.userid, request_date = flareContext.Context.request.date, request_method = flareContext.Context.request.method, request_path = flareContext.Context.request.path, request_query_string = flareContext.Context.request.query_string, request_http_version = flareContext.Context.request.http_version, // response_code = flareContext.Context.response?.status_code, // response_length = flareContext.Context.response?.bytes_sent, // flags = flareContext.Flags }; db.requests.Add(db_request); if (await pipeline.ProcessRequest(flareContext) && server.proxy_block_requests) { await db.SaveChangesAsync(); db_request.response_code = 418; db_request.flags = flareContext.Flags; await db.SaveChangesAsync(); var text = $"418 - I am a teapot. Your request #{db_request.id} is failed because of security checks. Contact the website owner with this number if you think this was a mistake."; db_request.response_length = text.Length; context.Response.StatusCode = 418; await context.Response.WriteAsync(text); return; } db_request.flags = flareContext.Flags; using (var httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Clear(); foreach (var header in context.Request.Headers .Where(a => !a.Key.StartsWith("X-"))) { httpClient.DefaultRequestHeaders.Add(header.Key, header.Value.ToArray()); } httpClient.DefaultRequestHeaders.Add("X-Forwarded-For", context.Connection.RemoteIpAddress.ToString()); using (var _req = new HttpRequestMessage( context.Request.Method == "GET" ? HttpMethod.Get : context.Request.Method == "POST" ? HttpMethod.Post : context.Request.Method == "DELETE" ? HttpMethod.Delete : context.Request.Method == "PUT" ? HttpMethod.Put : context.Request.Method == "PATCH" ? HttpMethod.Patch : context.Request.Method == "TRACE" ? HttpMethod.Trace : context.Request.Method == "OPTIONS" ? HttpMethod.Options : throw new Exception("invalid method") , $"http://{server.origin_ip}{context.Request.Path}{context.Request.QueryString}")) { _req.Headers.Host = domain; using (var response = await httpClient.SendAsync(_req)) { db_request.response_code = context.Response.StatusCode = (int)response.StatusCode; foreach (var header in response.Headers .Where(a => a.Key != "Transfer-Encoding")) { context.Response.Headers.Add(header.Key, new StringValues(header.Value.ToArray())); } foreach (var header in response.Content.Headers) { context.Response.Headers.Add(header.Key, new StringValues(header.Value.ToArray())); } using (var streamWithProgess = new StreamWithProgress(context.Response.Body)) { await response.Content.CopyToAsync(streamWithProgess); await context.Response.Body.FlushAsync(); context.Response.Body.Close(); db_request.response_length = (int)streamWithProgess.bytesTotal; await db.SaveChangesAsync(); } } } } } else { if (await pipeline.ProcessRequest(flareContext)) { var text = $"418 - I am a teapot. Your request is failed because of security checks. If you think it was a mistake contact [email protected]"; context.Response.StatusCode = 418; await context.Response.WriteAsync(text); return; } await next(); } }) .UseCors(policy => policy.SetPreflightMaxAge(TimeSpan.FromMinutes(10)).AllowAnyMethod().AllowAnyOrigin().AllowAnyHeader()) .UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "../uploads")), RequestPath = new PathString("/uploads") }) .Use(async(context, next) => { IHttpBufferingFeature bufferingFeature = context.Features.Get <IHttpBufferingFeature>(); bufferingFeature?.DisableRequestBuffering(); bufferingFeature?.DisableResponseBuffering(); try { await next(); } catch (Exception e) { context.Response.StatusCode = e is NotImplementedException ? 404 : e is UnauthorizedAccessException || e is SecurityTokenValidationException ? 401 : e is ArgumentException ? 400 : 500; context.Response.ContentType = "application/json; charset=utf-8"; string message = ""; Exception x = e; do { message += x.Message + "\r\n\r\n"; } while ((x = x.InnerException) != null); await context.Response.WriteAsync(JsonConvert.SerializeObject(new { code = -1, message = message.Substring(0, message.Length - 4), stacktrace = e.StackTrace })); } }) .Use(async(context, next) => { Token token = null; string strToken = context.Request.Query["token"]; if (strToken == null) { string authorization = context.Request.Headers["Authorization"]; if (authorization != null) { if (authorization.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase)) { strToken = authorization.Substring("Bearer ".Length).Trim(); } else if (authorization.StartsWith("Basic ")) { var encoding = Encoding.UTF8.GetString(Convert.FromBase64String(authorization.Substring("Basic ".Length))).Split(':'); if (encoding.Length != 2) { throw new UnauthorizedAccessException(); } var username = encoding[0]; var password = encoding[1]; token = await personal_token_cache.GetAsync(username + ":" + password, async() => { var db = context.RequestServices.GetRequiredService <ApplicationDbContext>(); var _code = AccountController.StringToByteArrayFastest(username); var id = new Guid(_code.ToArray()); var app2 = await db.pacs .Include(a => a.user) .SingleOrDefaultAsync(b => b.id == id); if (app2 == null || app2.password_hash == null || !app2.password_hash.SequenceEqual( KeyDerivation.Pbkdf2( password: password, salt: app2.password_salt, iterationCount: 10000, numBytesRequested: 256 / 8, prf: KeyDerivationPrf.HMACSHA1 ) )) { throw new UnauthorizedAccessException(); } return(await token_cache.GetAsync(app2.user.id.ToString(), () => Task.FromResult <Token>(new UserToken(app2.user.id, app2.user.name, app2.user.type)), TimeSpan.FromDays(3))); }, TimeSpan.FromDays(3)); } } } if (strToken != null) { var a = new System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler(); var claims = a.ValidateToken(strToken, new TokenValidationParameters { ValidateAudience = true, ValidIssuer = "Flare", ValidAudience = "Flare Users", IssuerSigningKey = _key }, out SecurityToken _); context.User = claims; var strID = context.User?.Claims?.FirstOrDefault(b => b.Type == "user_id")?.Value; if (Int32.TryParse(strID, out int dwID)) { token = await token_cache.GetAsync(strID, async() => { var db = context.RequestServices.GetRequiredService <ApplicationDbContext>(); var app2 = await db.users.SingleAsync(b => b.id == dwID); return(new UserToken(app2.id, app2.name, app2.type)); }, TimeSpan.FromDays(3)); } } context.AddScoped(() => { if (token == null) { throw new UnauthorizedAccessException(); } return(token); }); await next(); }) .UseMvc(); }