public HttpResponseMessage Move([FromBody] MovePhotosViewModel viewModel) { if (viewModel == null || string.IsNullOrEmpty(viewModel.AlbumName) || !viewModel.PhotosId.Any()) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Uknown error")); } var uploadFileInfos = new List <UploadResultViewModel>(); try { int albumId; try { albumId = _albumService.GetAlbumId(User.Id, viewModel.AlbumName); } catch (AlbumNotFoundException) { albumId = _albumService.CreateAlbum(User.Id, viewModel.AlbumName).Id; } // Get temporary album Id int tempAlbumId = _albumService.GetAlbumId(User.Id, "Temporary"); // Get path to the temporary album folder string pathToTempAlbum = _pathUtil.BuildAbsoluteAlbumPath(User.Id, tempAlbumId); // Get path to the destination album folder string pathToDestAlbum = _pathUtil.BuildAbsoluteAlbumPath(User.Id, albumId); if (!_directoryWrapper.Exists(pathToDestAlbum)) { _directoryWrapper.CreateDirectory(pathToDestAlbum); } foreach (int photoId in viewModel.PhotosId) { PhotoModel photoModel = _photoService.GetPhoto(User.Id, photoId); string fileInTempAlbum = string.Format("{0}\\{1}.{2}", pathToTempAlbum, photoModel.Id, photoModel.Format); string fileInDestAlbum = string.Format("{0}\\{1}.{2}", pathToDestAlbum, photoModel.Id, photoModel.Format); bool fileExist = _fileWrapper.Exists(fileInTempAlbum); if (fileExist) { try { _fileWrapper.Move(fileInTempAlbum, fileInDestAlbum); } catch (Exception) { uploadFileInfos.Add(new UploadResultViewModel { Id = photoId, IsAccepted = false, Error = "Can't save photo to selected album" }); continue; } photoModel.AlbumId = albumId; _photoService.UpdatePhoto(photoModel); // Create thumbnails for photo _photoProcessor.CreateThumbnails(User.Id, albumId, photoModel.Id, photoModel.Format); uploadFileInfos.Add(new UploadResultViewModel { Id = photoId, IsAccepted = true }); } else { uploadFileInfos.Add(new UploadResultViewModel { Id = photoId, IsAccepted = false, Error = "Photo is not found in temporary album" }); } } // Create collage for album _collageProcessor.CreateCollage(User.Id, albumId); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message)); } return(Request.CreateResponse(HttpStatusCode.OK, uploadFileInfos, new JsonMediaTypeFormatter())); }
public string GetAlbumPath(AlbumModel album, IUnitOfWork unitOfWork) { UserModel user = unitOfWork.Users.Find(album.OwnerId); return(pathUtil.BuildAbsoluteAlbumPath(user.Id, album.Id)); }