public bool WriteHeaderDescription(EditFileInfo editFileInfo) { string searchString = string.Empty; if (string.IsNullOrEmpty(editFileInfo.Path)) { searchString = editFileInfo.FileName; } else { searchString = Path.Combine(editFileInfo.Path, editFileInfo.FileName); } using (FamilyArchiveContext db = new FamilyArchiveContext()) { Photos photo = db.Photos.Where(p => p.Name == searchString).FirstOrDefault(); if (photo != null) { photo.Header = editFileInfo.NewHeader; photo.Description = editFileInfo.NewDescription; db.SaveChanges(); return(true); } else { return(false); } } }
public void DeletePhotoFromDb(DeleteFileInfo deleteFileInfo) { using (FamilyArchiveContext db = new FamilyArchiveContext()) { string searchString = string.Empty; if (string.IsNullOrEmpty(deleteFileInfo.Path)) { searchString = deleteFileInfo.PhotoName; } else { searchString = Path.Combine(deleteFileInfo.Path, deleteFileInfo.PhotoName); } List <Photos> photos = db.Photos.Where(p => p.Name == searchString).ToList(); db.Photos.RemoveRange(photos); db.SaveChanges(); } }
public async Task <IActionResult> UploadPhotos(string path, [FromForm] IList <IFormFile> photos) { List <UploadedPhoto> uploadedPhotos = new List <UploadedPhoto>(); for (int i = 0; i < photos.Count; i++) { string fileName = string.Empty; if (string.IsNullOrEmpty(path)) { fileName = photos[i].FileName; } else { fileName = Path.Combine(path, photos[i].FileName); } MemoryStream memoryStream = new MemoryStream(); photos[i].CopyTo(memoryStream); bool binaryCheck = false; if (photos[i].FileName.EndsWith(".mp4")) { binaryCheck = memoryStream.IsMp4(); } else if (photos[i].FileName.EndsWith(".jpg") || photos[i].FileName.EndsWith(".jpeg")) { binaryCheck = memoryStream.IsJpg(); } else if (photos[i].FileName.EndsWith(".png")) { binaryCheck = memoryStream.IsPng(); } if (!binaryCheck) { continue; } UploadedPhoto uploadedPhoto = new UploadedPhoto { filename = fileName, fileContent = memoryStream.ToArray() }; uploadedPhotos.Add(uploadedPhoto); string FullPath = _pathHelper.GetFullPathToFile(fileName); if (!System.IO.File.Exists(FullPath)) { await System.IO.File.WriteAllBytesAsync(FullPath, uploadedPhoto.fileContent); } else { uploadedPhotos.Remove(uploadedPhoto); } } Task task = Task.Factory.StartNew(() => { using (FamilyArchiveContext db = new FamilyArchiveContext()) { for (int i = 0; i < uploadedPhotos.Count; i++) { if (uploadedPhotos[i].filename.EndsWith(".png") || uploadedPhotos[i].filename.EndsWith(".jpg") || uploadedPhotos[i].filename.EndsWith(".jpeg")) { _dbService.AddPhotoToDb(uploadedPhotos[i], db); } else if (uploadedPhotos[i].filename.EndsWith(".mp4")) { string FullPathToVideo = _pathHelper.GetFullPathToFile(uploadedPhotos[i].filename); _dbService.GenerateThumbnailForVideo(uploadedPhotos[i].filename, FullPathToVideo, db); } } db.SaveChanges(); } }); return(Ok()); }