private CollectionFile FromDbFile(CollectionFSFile dbFile) { string filePath = GetFilePath(dbFile); return(new CollectionFile { Id = dbFile.Id, ElementId = dbFile.ElementId, Path = filePath }); }
/// <inheritdoc /> public bool DeleteById(int id) { try { CollectionFSFile dbFile = DbFiles.FindById(id); if (dbFile == null) { return(false); } File.Delete(GetFilePath(dbFile)); DbFiles.Delete(dbFile.Id); return(true); } catch (Exception ex) { LogTo.Error(ex, "CollectionFS: DeleteById threw an exception."); return(false); } }
private string GetFilePath(CollectionFSFile dbFile) { return(SMA.Collection.GetSMAElementsFilePath(dbFile.ElementId, $"{dbFile.Id}{GetFileExtension(dbFile.Extension)}")); }
public CollectionFile Create( [NotNull] ISMAPlugin requester, int elementId, [NotNull] Action <Stream> streamWriter, string extension, string crc32 = null) { if (elementId <= 0) { return(null); } CollectionFSFile dbFile = null; try { extension = extension?.TrimStart('.'); dbFile = new CollectionFSFile { ElementId = elementId, Extension = extension ?? string.Empty, PluginId = requester.Id }; dbFile.Id = DbFiles.Insert(dbFile).AsInt32; CollectionFile colFile = FromDbFile(dbFile); DirectoryEx.EnsureExists(Path.GetDirectoryName(colFile.Path)); using (var stream = File.Open(colFile.Path, System.IO.FileMode.Create, FileAccess.ReadWrite)) streamWriter(stream); if (crc32 != null) { var fsCrc32 = FileEx.GetCrc32(colFile.Path); if (fsCrc32 != crc32) { throw new IOException($"CRC32 did not match for file {colFile.Path}. Expected {crc32}, got {fsCrc32}"); } } return(colFile); } catch (Exception ex) { LogTo.Error(ex, "CollectionFS: Create threw an exception."); try { if (dbFile != null) { DbFiles.Delete(dbFile.Id); } } catch (Exception dbEx) { LogTo.Error(dbEx, "CollectionFS: Create threw an exception. Exception's DB cleanup code threw an exception"); } throw; } }