Пример #1
0
        /// <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()));
        }
Пример #2
0
        /// <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
            });
        }
Пример #3
0
 /// <summary>
 /// Read a blob as a base 64 string from the storage.
 /// </summary>
 /// <param name="UserBlobs">The user blob to search</param>
 /// <returns></returns>
 public static async Task <string> ReadBlobAsBase64Async(this BaseDbContext <AuditTrails, ExceptionLogs, UserBlobs> dbContext, UserBlobs userBlobs, IBlobStorage blobStorage, string userId, bool ignoreBlobOwner = false, string controllerName = null, string actionName = null, string remoteIpAddress = null)
 {
     return(Convert.ToBase64String(await dbContext.ReadBlobAsByteArrayAsync(userBlobs, blobStorage, userId, ignoreBlobOwner, controllerName, actionName, remoteIpAddress)));
 }
Пример #4
0
        /// <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
            });
        }
Пример #5
0
        /// <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
            });
        }
Пример #6
0
        /// <summary>
        /// Deletes a blob from the storage
        /// </summary>
        /// <param name="UserBlobs">The user blob to delete</param>
        /// <returns></returns>
        public static async Task <IApiResponse <UserBlobs> > DeleteForcedUserBlobsAsync(this BaseDbContext <AuditTrails, ExceptionLogs, UserBlobs> dbContext, UserBlobs userBlobs, IBlobStorage blobStorage, string userId, string controllerName = null, string actionName = null, string remoteIpAddress = null)
        {
            await blobStorage.DeleteAsync(userBlobs.FilePath);

            await dbContext.RemoveAuditedAsync(userBlobs, userId, controllerName, actionName, remoteIpAddress);

            var result = await dbContext.SaveChangesAsync();

            return(result.TransactionResultApiResponse(userBlobs));
        }
Пример #7
0
        /// <summary>
        /// Deletes a blob from the storage
        /// </summary>
        /// <param name="UserBlobs">The user blob to delete</param>
        /// <returns></returns>
        public static async Task <IApiResponse <UserBlobs> > DeleteUserBlobsAsync(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 <UserBlobs>()
                {
                    Status = ApiResponseStatus.Failed, Message = isBlobOwnerAndFileExists.Message
                });
            }
            await blobStorage.DeleteAsync(userBlobs.FilePath);

            await dbContext.RemoveAuditedAsync(userBlobs, userId, controllerName, actionName, remoteIpAddress);

            var result = await dbContext.SaveChangesAsync();

            return(result.TransactionResultApiResponse(userBlobs));
        }
Пример #8
0
        /// <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
            });
        }
Пример #9
0
        /// <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>
        /// 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)
            });
        }
Пример #11
0
        /// <summary>
        /// This is a helper method which tryies to upload a file and bind the userblob to the model property name
        /// </summary>
        /// <param name="model">The model to bind the result</param>
        /// <param name="propertyName">The property name of the model to bind</param>
        /// <param name="predicate">The form file field name</param>
        /// <param name="formFileRules">The rules for the files</param>
        /// <returns></returns>
        public static async Task <IApiResponse <Tuple <UserBlobs, UserBlobs> > > UpdateRangeAndBindUserBlobsAsync(this BaseDbContext <AuditTrails, ExceptionLogs, UserBlobs> dbContext, object model, string propertyName, UserBlobs userBlobs, IFormFileCollection files, Func <IFormFile, bool> predicate, FormFileRules formFileRules, IBlobStorage blobStorage, string userId, bool ignoreBlobOwner = false, string controllerName = null, string actionName = null, string remoteIpAddress = null)
        {
            var blob = await dbContext.UpdateUserBlobsAsync(userBlobs, files, predicate, formFileRules, blobStorage, userId, ignoreBlobOwner, controllerName, actionName, remoteIpAddress);

            model.GetType().GetProperty(propertyName).SetValue(model, JsonConvert.SerializeObject(blob.Data.Item1));
            return(blob);
        }
Пример #12
0
        /// <summary>
        /// Updates a blob from the storage. Delete the provide userBlob and upload the new file from the http request.
        /// </summary>
        /// <param name="UserBlobs">The user blob to delete</param>
        /// <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 <Tuple <UserBlobs, UserBlobs> > > UpdateUserBlobsAsync(this BaseDbContext <AuditTrails, ExceptionLogs, UserBlobs> dbContext, UserBlobs userBlobs, IFormFileCollection files, Func <IFormFile, bool> predicate, FormFileRules formFileRules, IBlobStorage blobStorage, string userId, bool ignoreBlobOwner = false, string controllerName = null, string actionName = null, string remoteIpAddress = null)
        {
            var addHttpBlobApiResponse = await dbContext.CreateUserBlobsAsync(files, predicate, formFileRules, blobStorage, userId, userBlobs.TableName, controllerName, actionName, remoteIpAddress);

            if (addHttpBlobApiResponse.Status == ApiResponseStatus.Failed)
            {
                return(new ApiResponse <Tuple <UserBlobs, UserBlobs> >()
                {
                    Data = Tuple.Create(addHttpBlobApiResponse.Data, new UserBlobs()), Message = addHttpBlobApiResponse.Message, Status = ApiResponseStatus.Failed
                });
            }
            var deleteBlobApiResponse = await dbContext.DeleteUserBlobsAsync(userBlobs, blobStorage, userId, ignoreBlobOwner, controllerName, actionName, remoteIpAddress);

            if (addHttpBlobApiResponse.Status == ApiResponseStatus.Failed)
            {
                return(new ApiResponse <Tuple <UserBlobs, UserBlobs> >()
                {
                    Data = Tuple.Create(addHttpBlobApiResponse.Data, deleteBlobApiResponse.Data), Message = deleteBlobApiResponse.Message, Status = ApiResponseStatus.Failed
                });
            }
            return(new ApiResponse <Tuple <UserBlobs, UserBlobs> > {
                Data = Tuple.Create(addHttpBlobApiResponse.Data, deleteBlobApiResponse.Data), Status = ApiResponseStatus.Succeeded
            });
        }