/// <summary> /// Read multiple blobs into a compressed zip folder as a file stream result from the storage /// </summary> /// <param name="UserBlobsViewModels">The user blob to search</param> /// <param name="zipFilename">The zip file name. (Remember include *.zip extension)</param> /// <returns></returns> public static async Task <Stream> ReadBlobRangeIntoZipFolderStreamAsync(this BaseDbContext <AuditTrails, ExceptionLogs, UserBlobs> dbContext, IEnumerable <UserBlobs> userBlobs, IBlobStorage blobStorage, string userId, bool ignoreBlobOwner = false, string controllerName = null, string actionName = null, string remoteIpAddress = null) { using (MemoryStream zipStream = new MemoryStream()) { using (ZipArchive zip = new ZipArchive(zipStream, ZipArchiveMode.Create, true)) { foreach (var userBlob in userBlobs) { var isBlobOwnerAndFileExists = await dbContext.IsBlobOwnerAndFileExistsAsync(userBlob, blobStorage, userId, ignoreBlobOwner, controllerName, actionName, remoteIpAddress); if (isBlobOwnerAndFileExists.Status == ApiResponseStatus.Failed || !isBlobOwnerAndFileExists.Data) { return(null); } var stream = await blobStorage.OpenReadAsync(userBlob.FilePath); var entry = zip.CreateEntry(userBlob.FileName); using (var entryStream = entry.Open()) { await stream.CopyToAsync(entryStream); } } } zipStream.Position = 0; await dbContext.CreateAuditrailsAsync(userBlobs, "A list of UserBlobs were readed into a compressed zip folder as stream result", userId, controllerName, actionName, remoteIpAddress); return(zipStream); } }
/// <summary> /// Check if a expected file is uploaded with specified file rules if any /// </summary> /// <param name="predicate"></param> /// <param name="formFileRules"></param> /// <returns></returns> public static async Task <IApiResponse <bool> > IsFormFileUploadedAsync(this BaseDbContext <AuditTrails, ExceptionLogs, UserBlobs> dbContext, IFormFileCollection files, Func <IFormFile, bool> predicate, FormFileRules formFileRules, string userId, string controllerName = null, string actionName = null, string remoteIpAddress = null) { var any = predicate == null?files.Any() : files.Any(predicate); if (!any) { await dbContext.CreateAuditrailsAsync(new UserBlobs(), "No file uploaded", userId, controllerName, actionName, remoteIpAddress); return(new ApiResponse <bool>() { Status = ApiResponseStatus.Failed, Message = "FileIsNullOrEmpty", Data = false }); } if (any && formFileRules != null) { var isValidFormFileApiResponse = files.IsValidFormFileApiResponse(predicate, formFileRules); if (isValidFormFileApiResponse.Status == ApiResponseStatus.Failed) { return(new ApiResponse <bool>() { Status = ApiResponseStatus.Failed, Message = isValidFormFileApiResponse.Message, Data = false }); } } return(new ApiResponse <bool>() { Status = ApiResponseStatus.Succeeded, Data = true }); }
/// <summary> /// Deletes blob/s from the storage /// </summary> /// <param name="UserBlobs">The user blobs to delete</param> /// <returns></returns> public static async Task <IApiResponse <IEnumerable <UserBlobs> > > DeleteRangeUserBlobsAsync(this BaseDbContext <AuditTrails, ExceptionLogs, UserBlobs> dbContext, IEnumerable <UserBlobs> userBlobs, IBlobStorage blobStorage, string userId, bool ignoreBlobOwner = false, string controllerName = null, string actionName = null, string remoteIpAddress = null) { if (userBlobs == null || !userBlobs.Any()) { await dbContext.CreateAuditrailsAsync(userBlobs, "UserBlobs is null or empty on method DeleteRange", userId, controllerName, actionName, remoteIpAddress); return(new ApiResponse <IEnumerable <UserBlobs> >() { Status = ApiResponseStatus.Failed, Message = "FileNotFound" }); } foreach (var userBlob in userBlobs) { var isBlobOwnerAndFileExists = await dbContext.IsBlobOwnerAndFileExistsAsync(userBlob, blobStorage, userId, ignoreBlobOwner, controllerName, actionName, remoteIpAddress); if (isBlobOwnerAndFileExists.Status == ApiResponseStatus.Failed || !isBlobOwnerAndFileExists.Data) { return(new ApiResponse <IEnumerable <UserBlobs> >() { Status = ApiResponseStatus.Failed, Message = isBlobOwnerAndFileExists.Message }); } await blobStorage.DeleteAsync(userBlob.FilePath); await dbContext.RemoveAuditedAsync(userBlob, userId, controllerName, actionName, remoteIpAddress); } var result = await dbContext.SaveChangesAsync(); return(result.TransactionResultApiResponse(userBlobs)); }
/// <summary> /// Read multiple blobs into a compressed zip folder as a file stream result from the storage. /// If no zipFilename is provided then the zip file name will be auto generated. /// </summary> /// <param name="UserBlobsViewModels">The user blob to search</param> /// <param name="zipFilename">The zip file name. (Remember include *.zip extension)</param> /// <returns></returns> public static async Task <IActionResult> ReadBlobRangeIntoZipFolderAsFileStreamResult(this BaseDbContext <AuditTrails, ExceptionLogs, UserBlobs> dbContext, IEnumerable <UserBlobs> userBlobs, IBlobStorage blobStorage, string userId, string zipFilename = null, bool ignoreBlobOwner = false, string controllerName = null, string actionName = null, string remoteIpAddress = null) { using (MemoryStream zipStream = new MemoryStream()) { using (ZipArchive zip = new ZipArchive(zipStream, ZipArchiveMode.Create, true)) { foreach (var userBlob in userBlobs) { var isBlobOwnerAndFileExists = await dbContext.IsBlobOwnerAndFileExistsAsync(userBlob, blobStorage, userId, ignoreBlobOwner, controllerName, actionName, remoteIpAddress); if (isBlobOwnerAndFileExists.Status == ApiResponseStatus.Failed || !isBlobOwnerAndFileExists.Data) { return(isBlobOwnerAndFileExists.ToJson()); } var stream = await blobStorage.OpenReadAsync(userBlob.FilePath); var entry = zip.CreateEntry(userBlob.FileName); using (var entryStream = entry.Open()) { await stream.CopyToAsync(entryStream); } } } zipStream.Position = 0; await dbContext.CreateAuditrailsAsync(userBlobs, "A list of UserBlobs were readed as stream into a compressed zip folder and downloaded as a file stream result", userId, controllerName, actionName, remoteIpAddress); zipFilename = string.IsNullOrWhiteSpace(zipFilename) ? $"{".zip".GenerateUniqueFileName()}" : zipFilename; return(new FileStreamResult(zipStream, MimeTypes.Application.Zip) { FileDownloadName = zipFilename }); } }
public static async Task <IApiResponse <TEntity> > GetOneAsync <TEntity>(this BaseDbContext <AuditTrails, ExceptionLogs, UserBlobs> dbContext, IIdentifier <string> identifier, string userId, string controllerName = null, string actionName = null, string remoteIpAddress = null) where TEntity : class, new() { if (identifier == null || string.IsNullOrWhiteSpace(identifier.Model)) { return(new TEntity().ToApiResponseFailed("RecordNotFound")); } var record = await dbContext.Set <TEntity>().FindAsync(identifier.Model);; if (record == null) { await dbContext.CreateAuditrailsAsync(record, "Not found", userId, controllerName, actionName, remoteIpAddress); return(record.ToApiResponseFailed("RecordNotFound")); } await dbContext.CreateAuditrailsAsync(record, "Record was found", userId, controllerName, actionName, remoteIpAddress); return(record.ToApiResponse()); }
/// <summary> /// Checks if the current user is the blob owner and the file exists /// </summary> /// <param name="userBlob"></param> /// <returns></returns> public static async Task <IApiResponse <bool> > IsBlobOwnerAndFileExistsAsync(this BaseDbContext <AuditTrails, ExceptionLogs, UserBlobs> dbContext, UserBlobs userBlobs, IBlobStorage blobStorage, string userId, bool ignoreBlobOwner = false, string controllerName = null, string actionName = null, string remoteIpAddress = null) { if (userBlobs == null) { await dbContext.CreateAuditrailsAsync(userBlobs, "Userblob is null on method IsBlobOwnerAndFileExistsAsync", userId, controllerName, actionName, remoteIpAddress); return(new ApiResponse <bool>() { Status = ApiResponseStatus.Failed, Message = "FileNotFound" }); } if (!ignoreBlobOwner) { var isBlobOwner = await dbContext.IsBlobOwnerAsync(userBlobs, userId, controllerName, actionName, remoteIpAddress); if (isBlobOwner.Status == ApiResponseStatus.Failed || !isBlobOwner.Data) { return(new ApiResponse <bool>() { Status = ApiResponseStatus.Failed, Message = isBlobOwner.Message }); } } var fileExist = await blobStorage.ExistsAsync(userBlobs.FilePath); if (!fileExist) { await dbContext.CreateAuditrailsAsync(userBlobs, "File Not found on method IsBlobOwnerAndFileExists", userId, controllerName, actionName, remoteIpAddress); return(new ApiResponse <bool>() { Status = ApiResponseStatus.Failed, Data = fileExist, Message = "FileNotFound" }); } await dbContext.CreateAuditrailsAsync(userBlobs, "File was found", userId, controllerName, actionName, remoteIpAddress); return(new ApiResponse <bool>() { Status = ApiResponseStatus.Succeeded, Data = true }); }
/// <summary> /// Read a blob as stream from the storage /// </summary> /// <param name="UserBlobs">The user blob to search</param> /// <returns></returns> public static async Task <Stream> ReadBlobAsStreamAsync(this BaseDbContext <AuditTrails, ExceptionLogs, UserBlobs> dbContext, UserBlobs userBlobs, IBlobStorage blobStorage, string userId, bool ignoreBlobOwner = false, string controllerName = null, string actionName = null, string remoteIpAddress = null) { var isBlobOwnerAndFileExists = await dbContext.IsBlobOwnerAndFileExistsAsync(userBlobs, blobStorage, userId, ignoreBlobOwner, controllerName, actionName, remoteIpAddress); if (isBlobOwnerAndFileExists.Status == ApiResponseStatus.Failed || !isBlobOwnerAndFileExists.Data) { return(null); } await dbContext.CreateAuditrailsAsync(userBlobs, "File was found and read as stream", userId, controllerName, actionName, remoteIpAddress); return(await blobStorage.OpenReadAsync(userBlobs.FilePath)); }
/// <summary> /// Checks is the current user is the blob owner /// </summary> /// <param name="UserBlobs"></param> /// <returns></returns> public static async Task <IApiResponse <bool> > IsBlobOwnerAsync(this BaseDbContext <AuditTrails, ExceptionLogs, UserBlobs> dbContext, UserBlobs userBlobs, string userId, string controllerName = null, string actionName = null, string remoteIpAddress = null) { if (userBlobs == null) { await dbContext.CreateAuditrailsAsync(userBlobs, "Userblob is null on method IsBlobOwner", userId, controllerName, actionName, remoteIpAddress); return(new ApiResponse <bool>() { Status = ApiResponseStatus.Failed, Message = "FileNotFound" }); } var result = (await dbContext.UserBlobs.FirstOrDefaultAsync(x => x.UserBlobId == userBlobs.UserBlobId && x.CreatedBy == userId)) != null; if (!result) { await dbContext.CreateAuditrailsAsync(userBlobs, "User is not the blob owner", userId, controllerName, actionName, remoteIpAddress); } return(new ApiResponse <bool>() { Status = ApiResponseStatus.Succeeded, Data = result }); }
/// <summary> /// Adds file/s from the http request /// </summary> /// <param name="predicate">Set this param if you want to read a file from a specific form field name.</param> /// <param name="formFileRules">The rules to apply to the file uploaded.</param> /// <returns></returns> public static async Task <IApiResponse <IEnumerable <UserBlobs> > > CreateRangeUserBlobsAsync(this BaseDbContext <AuditTrails, ExceptionLogs, UserBlobs> dbContext, IFormFileCollection files, Func <IFormFile, bool> predicate, FormFileRules formFileRules, IBlobStorage blobStorage, string userId, string tableName, string controllerName = null, string actionName = null, string remoteIpAddress = null) { var any = predicate == null?files.Any() : files.Any(predicate); if (any) { await dbContext.CreateAuditrailsAsync(new UserBlobs(), "No file/s uploaded", userId, controllerName, actionName, remoteIpAddress); return(new ApiResponse <IEnumerable <UserBlobs> >() { Status = ApiResponseStatus.Failed, Message = "FileIsNullOrEmpty" }); } if (any && formFileRules != null) { var isValidFormFileApiResponse = files.IsValidFormFileApiResponse(predicate, formFileRules); if (isValidFormFileApiResponse.Status == ApiResponseStatus.Failed) { return(new ApiResponse <IEnumerable <UserBlobs> >() { Status = ApiResponseStatus.Failed, Message = isValidFormFileApiResponse.Message }); } } var createdOn = DateTime.Now; IEnumerable <IFormFile> formFiles = predicate == null?files.ToList() : files.Where(predicate); IList <UserBlobs> uploadedUserBlobs = new List <UserBlobs>(); foreach (var file in formFiles) { var fileExtension = Path.GetExtension(file.FileName); var uniqueFileName = Guid.NewGuid().ToString("N") + string.Empty.NewNumericIdentifier().ToString(); var filename = $"{uniqueFileName}{fileExtension}"; await blobStorage.WriteAsync(filename, file.OpenReadStream()); var userBlob = new UserBlobs().AutoMap(file); userBlob.FilePath = filename; userBlob.CreatedBy = userId; userBlob.CreatedOn = createdOn; userBlob.TableName = tableName; uploadedUserBlobs.Add(userBlob); await dbContext.AddAuditedAsync(userBlob, "File was uploaded", userId, controllerName, actionName, remoteIpAddress); } var result = await dbContext.SaveChangesAsync(); return(result.TransactionResultApiResponse(uploadedUserBlobs.AsEnumerable())); }
/// <summary> /// Read a blob as a base 64 string api response from the storage /// </summary> /// <param name="UserBlobs">The user blob to search</param> /// <returns></returns> public static async Task <IEnumerable <IApiResponse <string> > > ReadBlobRangeAsBase64ApiResponseAsync(this BaseDbContext <AuditTrails, ExceptionLogs, UserBlobs> dbContext, IEnumerable <UserBlobs> userBlobs, IBlobStorage blobStorage, string userId, bool ignoreBlobOwner = false, string controllerName = null, string actionName = null, string remoteIpAddress = null) { if (userBlobs == null || !userBlobs.Any()) { await dbContext.CreateAuditrailsAsync(userBlobs, "Userblobs is null or empty on method ReadBlobAsBase64ApiResponseAsync", userId, controllerName, actionName, remoteIpAddress); return(Enumerable.Empty <IApiResponse <string> >()); } var base64Array = new List <IApiResponse <string> >(); foreach (var userBlob in userBlobs) { base64Array.Add(await dbContext.ReadBlobAsBase64ApiResponseAsync(userBlob, blobStorage, userId, ignoreBlobOwner, controllerName, actionName, remoteIpAddress)); } return(base64Array); }
/// <summary> /// Read a blob as a file stream result from the storage /// </summary> /// <param name="UserBlobs">The user blob to search</param> /// <returns></returns> public static async Task <IActionResult> ReadBlobAsFileStreamResult(this BaseDbContext <AuditTrails, ExceptionLogs, UserBlobs> dbContext, UserBlobs userBlobs, IBlobStorage blobStorage, string userId, bool ignoreBlobOwner = false, string controllerName = null, string actionName = null, string remoteIpAddress = null) { var isBlobOwnerAndFileExists = await dbContext.IsBlobOwnerAndFileExistsAsync(userBlobs, blobStorage, userId, ignoreBlobOwner, controllerName, actionName, remoteIpAddress); if (isBlobOwnerAndFileExists.Status == ApiResponseStatus.Failed || !isBlobOwnerAndFileExists.Data) { return(isBlobOwnerAndFileExists.ToJson()); } var stream = await blobStorage.OpenReadAsync(userBlobs.FilePath); await dbContext.CreateAuditrailsAsync(userBlobs, "UserBlob was readed as file stream result", userId, controllerName, actionName, remoteIpAddress); var contentType = !string.IsNullOrWhiteSpace(userBlobs.ContentType) ? userBlobs.ContentType : MimeTypes.Application.OctetStream; return(new FileStreamResult(stream, contentType) { FileDownloadName = Path.GetFileName(userBlobs.FileName) }); }
/// <summary> /// Read a blob as a base 64 string api response from the storage /// </summary> /// <param name="UserBlobs">The user blob to search</param> /// <returns></returns> public static async Task <IApiResponse <string> > ReadBlobAsBase64ApiResponseAsync(this BaseDbContext <AuditTrails, ExceptionLogs, UserBlobs> dbContext, UserBlobs userBlobs, IBlobStorage blobStorage, string userId, bool ignoreBlobOwner = false, string controllerName = null, string actionName = null, string remoteIpAddress = null) { var isBlobOwnerAndFileExists = await dbContext.IsBlobOwnerAndFileExistsAsync(userBlobs, blobStorage, userId, ignoreBlobOwner, controllerName, actionName, remoteIpAddress); if (isBlobOwnerAndFileExists.Status == ApiResponseStatus.Failed || !isBlobOwnerAndFileExists.Data) { return(new ApiResponse <string>() { Status = ApiResponseStatus.Failed, Message = isBlobOwnerAndFileExists.Message }); } await dbContext.CreateAuditrailsAsync(userBlobs, "File was found and readed as api response base 64 string", userId, controllerName, actionName, remoteIpAddress); var base64 = Convert.ToBase64String(await blobStorage.ReadBytesAsync(userBlobs.FilePath)); return(new ApiResponse <string>() { Data = base64, Status = ApiResponseStatus.Succeeded }); }
/// <summary> /// Read a blob as a stream api response from the storage /// </summary> /// <param name="UserBlobs">The user blob to search</param> /// <returns></returns> public static async Task <IApiResponse <Stream> > ReadBlobAsStreamApiResponseAsync(this BaseDbContext <AuditTrails, ExceptionLogs, UserBlobs> dbContext, UserBlobs userBlobs, IBlobStorage blobStorage, string userId, bool ignoreBlobOwner = false, string controllerName = null, string actionName = null, string remoteIpAddress = null) { var isBlobOwnerAndFileExists = await dbContext.IsBlobOwnerAndFileExistsAsync(userBlobs, blobStorage, userId, ignoreBlobOwner, controllerName, actionName, remoteIpAddress); if (isBlobOwnerAndFileExists.Status == ApiResponseStatus.Failed || !isBlobOwnerAndFileExists.Data) { return(new ApiResponse <Stream>() { Status = ApiResponseStatus.Failed, Message = isBlobOwnerAndFileExists.Message }); } await dbContext.CreateAuditrailsAsync(userBlobs, "UserBlob was readed as api response", userId, controllerName, actionName, remoteIpAddress); var stream = await blobStorage.OpenReadAsync(userBlobs.FilePath); return(new ApiResponse <Stream>() { Data = stream, Status = ApiResponseStatus.Succeeded }); }