Ejemplo n.º 1
0
        public async Task AddOrUpdateImageAsync(int id, string bucketName, byte[] imageData)
        {
            var employee = await Uow.Employees.GetByIdAsync(id);

            if (employee == null)
            {
                throw new ArgumentException($"Employee with employeeId: '{id}' not found", nameof(id));
            }

            var fileName = string.Empty;

            try
            {
                fileName = $"{id.ToString()}_avatar";

                if (!string.IsNullOrEmpty(employee.ImageUrl))
                {
                    await _blobStorageProvider.TryRemoveFileAsync(bucketName, fileName);
                }

                var imageUrl = await _blobStorageProvider.UploadFileAsync(bucketName, fileName, imageData);

                employee.ImageUrl = imageUrl;
                Uow.Employees.Edit(employee);
                await Uow.CommitAsync();
            }
            catch (Exception e)
            {
                await _blobStorageProvider.TryRemoveFileAsync(bucketName, fileName);

                Logger.LogError(e, $"Error while uploading employee's photo with EmployeeId: '{id}'");
                throw;
            }
        }