public ObjectResult Revert([FromBody] DeletionInfo[] items) { if (items == null || items.Length == 0) { return(NullResponse()); } var reverted = new List <DeletionInfo>(); var failed = new List <FailedItem>(); foreach (var item in items) { try { var revert = db.PendingDeletions.Where(x => x.MediaId == item.MediaId && x.Version == item.Version).SingleOrDefault(); if (revert != null) { db.PendingDeletions.Remove(revert); reverted.Add(item); } else { failed.Add(FailedItem.Generate(item, "Not found")); } } catch (Exception ex) { log.LogWarning($"Failed to revert deletion of item {item?.MediaId} v{item?.Version} Ex: {ex.ToString()}"); failed.Add(FailedItem.Generate(item, $"Unknown error. Exception: {ex.Message}")); } } db.SaveChanges(); log.LogInformation($"Reverted deleting items {string.Join(';', reverted.Select(x => $"{x.MediaId},{x.Version}"))}"); return(GenerateOverallStatus(reverted, failed, "reverted")); }
public ObjectResult Confirm([FromBody] DeletionInfo[] items) { if (items == null || items.Length == 0) { return(NullResponse()); } var deleted = new List <DeletionInfo>(); var failed = new List <FailedItem>(); Dictionary <string, FileManagement> managerCache = new Dictionary <string, FileManagement>(); foreach (var item in items) { try { var mediaItem = db.Media.Where(x => x.MediaId == item.MediaId).SingleOrDefault(); FileManagement fileManager = null; try { // Retrieve a file manager for this library. if (!managerCache.TryGetValue(mediaItem.LibraryName, out fileManager)) { var library = settings.Libraries.Where(x => x.Name == mediaItem.LibraryName).SingleOrDefault(); if (library != null && library.StorageBackend != null) { fileManager = new FileManagement(library.StorageBackend.RetrieveBackend(), log); managerCache[mediaItem.LibraryName] = fileManager; } else { failed.Add(FailedItem.Generate(item, $"Library '{mediaItem.LibraryName}' does not exist in vsettings.")); continue; } } } catch (Exception ex) { string msg = $"Failed to generate storage backend: {ex.Message}"; log.LogError(msg); failed.Add(FailedItem.Generate(item, msg)); continue; } // Discover the files to delete. IEnumerable <MediaFile> deleteEntries; if (item.Version == -1) { deleteEntries = db.MediaFiles.Where(x => x.MediaId == item.MediaId); } else { deleteEntries = db.MediaFiles.Where(x => x.MediaId == item.MediaId && x.Version == item.Version); } // Delete files. foreach (var file in deleteEntries) { fileManager.DeleteFromBackend(file.Filename); log.LogInformation($"Deleted {file.Filename}"); } // Update database db.PendingDeletions.RemoveRange(db.PendingDeletions.Where(x => x.MediaId == item.MediaId && x.Version == x.Version)); if (item.Version == -1 || mediaItem.Version == item.Version) { db.Media.Remove(db.Media.Where(x => x.MediaId == item.MediaId).SingleOrDefault()); } else { db.MediaFiles.RemoveRange(db.MediaFiles.Where(x => x.MediaId == item.MediaId && x.Version == item.Version)); } db.SaveChanges(); deleted.Add(item); } catch (Exception ex) { log.LogError($"Failed to delete item {item.MediaId} version {item.Version}: {ex.ToString()}"); failed.Add(FailedItem.Generate(item, $"Unknown error. Exception: {ex.Message}")); } } return(GenerateOverallStatus(deleted, failed, "deleted")); }
public ObjectResult Schedule([FromBody] DeletionInfo[] items) { if (items == null || items.Length == 0) { return(NullResponse()); } var scheduled = new List <DeletionInfo>(); var failed = new List <FailedItem>(); foreach (var item in items) { try { var delete = db.Media.Where(x => x.MediaId == item.MediaId).SingleOrDefault(); if (delete != null) { var existsScheduled = db.PendingDeletions.Where(x => x.MediaId == item.MediaId); bool skip = false; foreach (var existingScheduled in existsScheduled) { if (existingScheduled.Version == -1 || existingScheduled.Version == item.Version) { log.LogWarning($"Failed to schedule deletion of item {item?.MediaId} v{item?.Version} (already scheduled)"); failed.Add(FailedItem.Generate(item, "Already scheduled")); skip = true; break; } } if (skip) { continue; } db.PendingDeletions.Add(new PendingDeletion() { MediaId = delete.MediaId, Version = item.Version, Requestor = DeleteRequester.User }); scheduled.Add(item); } else { failed.Add(FailedItem.Generate(item, "Not found")); } } catch (Exception ex) { log.LogWarning($"Failed to schedule deletion of item {item?.MediaId} v{item?.Version} Ex: {ex.ToString()}"); failed.Add(FailedItem.Generate(item, $"Unknown error. Exception: {ex.Message}")); } } if (scheduled.Count > 0) { db.SaveChanges(); log.LogInformation($"Scheduled deletion of items: {string.Join(';', scheduled.Select(x => $"{x.MediaId},{x.Version}"))}"); } else { log.LogInformation($"A call was made to schedule deletion, but no items could be scheduled."); } return(GenerateOverallStatus(scheduled, failed, "scheduled")); }