public void DeleteImage(int imageId, string userId) { if (string.IsNullOrEmpty(userId)) { throw new ArgumentNullException(); } Image imageToDelete = Database.ImageRepository.Find(imageId); if (imageToDelete == null) { throw new ArgumentNullException("Invalid image id"); } if (imageToDelete.UserId != userId) { throw new ArgumentNullException("UserId of image does not coincide with userId in parameters"); } var fullImagePath = Path.Combine(PhysicalDirectory.CurrentServerLocation, imageToDelete.LocalPath); try { Database.ImageRepository.Delete(imageToDelete); Database.Commit(); PhysicalDirectory.DeleleImageFromServerPhysically(fullImagePath); } catch (Exception) { throw; } }
public void UpdateUserAvatar(byte[] buffer, string fileName, string userId) { string fileExtension = Path.GetExtension(fileName); fileName = Guid.NewGuid() + fileExtension; //should be information of registered user UserProfile user = Database.UserRepository.Find(userId); if (user == null) { throw new NullReferenceException("Invalid User id"); } var localpath = Path.Combine(PhysicalDirectory.ImagesStoreFolder, user.Id, fileName); //path to image location on server var path = Path.Combine(PhysicalDirectory.CurrentServerLocation, localpath); try { PhysicalDirectory.WriteImageOnServerPhysically(buffer, path); //deletes old image if exists if (!string.IsNullOrEmpty(user.AvatarUrl)) { var oldpath = Path.Combine(PhysicalDirectory.CurrentServerLocation, user.Id, user.AvatarUrl); PhysicalDirectory.DeleleImageFromServerPhysically(oldpath); } user.AvatarUrl = localpath; Database.UserRepository.Update(user); Database.Commit(); } catch (Exception) { throw; } }
public void AddImage(UploadedImage image) { string fileExtension = Path.GetExtension(image.FileName); string fileName = Guid.NewGuid() + fileExtension; UserProfile user = Database.UserRepository.Find(image.UserId); if (user == null) { throw new NullReferenceException("Invalid User id"); } //will be written into photo data var localpath = Path.Combine(PhysicalDirectory.ImagesStoreFolder, user.Id, fileName); var fullImagePath = Path.Combine(PhysicalDirectory.CurrentServerLocation, localpath); PhysicalDirectory.WriteImageOnServerPhysically(image.Buffer, fullImagePath); Image newImage = new Image { User = user, LocalPath = localpath, ImageName = fileName, Description = image.Description, }; newImage.Tags = AddTags(image.Tags, newImage); try { Database.ImageRepository.Add(newImage); Database.Commit(); } catch (Exception) { //Rolling Back happend an error PhysicalDirectory.DeleleImageFromServerPhysically(fullImagePath); throw; } }