private ActionResult Download(DatabaseFile thisFile) { if (thisFile is null) { throw new ArgumentNullException(nameof(thisFile)); } string Extension = thisFile.FileName.FromLast("."); string MimeType = MimeMappings.GetMimeType(Extension); if (thisFile.Data.Length == 0) { if (MimeType.StartsWith("text/", StringComparison.OrdinalIgnoreCase)) { return(this.Content(System.IO.File.ReadAllText(thisFile.FullName))); } else { FileStream fileStream = new FileStream(thisFile.FullName, FileMode.Open, FileAccess.Read); FileStreamResult fsResult = new FileStreamResult(fileStream, MimeType) { EnableRangeProcessing = true, FileDownloadName = Path.GetFileName(thisFile.FullName) }; return(fsResult); } } else { return(this.File(thisFile.Data, MimeType, thisFile.FileName)); } }
private static async Task ReturnFile(HttpContext context, DatabaseFile databaseFile) { string Extension = databaseFile.FileName.FromLast("."); string MimeType = MimeMappings.GetMimeType(Extension); context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.ContentType = MimeType; if (string.IsNullOrEmpty(context.Request.Headers["Range"])) { byte[] fileData; if (databaseFile.Data.Length != 0) { fileData = databaseFile.Data; } else { fileData = File.ReadAllBytes(databaseFile.FullName); } context.Response.ContentLength = fileData.Length; using (Stream stream = context.Response.Body) { await stream.WriteAsync(fileData, 0, fileData.Length); await stream.FlushAsync(); } } else { await RangeDownload(databaseFile.FullName, context); } }
public ActionResult Download(DatabaseFile thisFile) { if (thisFile is null) { throw new ArgumentNullException(nameof(thisFile)); } if (!this.SecurityProvider.CheckAccess(thisFile, PermissionTypes.Read)) { throw new UnauthorizedAccessException(); } string Extension = thisFile.FileName.FromLast("."); string MimeType = MimeMappings.GetMimeType(Extension); if (thisFile.Data.Length == 0) { if (MimeType.StartsWith("text/", StringComparison.OrdinalIgnoreCase)) { return(this.Content(System.IO.File.ReadAllText(thisFile.FullName))); } else { return(this.File(System.IO.File.ReadAllBytes(thisFile.FullName), MimeType)); } } else { return(this.File(thisFile.Data, MimeType, thisFile.FileName)); } }
public void ImportImage(DatabaseFile target) { if (target is null) { throw new System.ArgumentNullException(nameof(target)); } if (this.DatabaseFileRepository is null) { return; } if (MimeMappings.GetMimeType(System.IO.Path.GetExtension(target.FullName)).StartsWith("image/", System.StringComparison.OrdinalIgnoreCase)) { Image newImage = this.ImageRepository.GetByUri(target.FullName).FirstOrDefault() ?? new Image(target.FullName); newImage.Refresh(); this.SecurityProvider.ClonePermissions(target, newImage); this.ImageRepository.AddOrUpdate(newImage); } }