/// <summary>Tries to get file with the specified fullpath.</summary> /// <param name="fullPath">The full path.</param> /// <param name="checkExtension">if set to <c>true</c> check if the file exists with another extension.</param> /// <param name="file">The file.</param> /// <returns>Returns true on success, false otherwise</returns> public bool TryGetFile(string fullPath, bool checkExtension, out MDBFile file) { MDBFolder folder = Folders.TryGetStruct(nameof(MDBFolder.Name), Path.GetDirectoryName(fullPath)); if (folder.ID > 0) { Search search = Search.FieldEquals(nameof(MDBFile.FolderID), folder.ID) & Search.FieldEquals(nameof(MDBFile.Name), Path.GetFileNameWithoutExtension(fullPath)); if (checkExtension) { search &= Search.FieldEquals(nameof(MDBFile.Extension), Path.GetExtension(fullPath)); } var files = Files.GetStructs(search); for (int i = 1; i < files.Count; i++) { this.LogWarning("Removing duplicate file registration. <red>{0}", files[i]); Files.Delete(files[i].ID); } if (files.Count > 0) { file = files.First(); return(true); } } file = default(MDBFile); return(false); }
/// <summary>Removes the file.</summary> /// <param name="mdbFile">The MDB file.</param> public void RemoveFile(MDBFile mdbFile) { if (Files.Exist(mdbFile.ID)) { Files.Delete(mdbFile.ID); } this.LogInfo("Removed file '<red>{0}<default>'", mdbFile.Name); if (mdbFile.IsImage) { foreach (MDBImage imageFile in Images.GetStructs(nameof(MDBImage.FileID), mdbFile.ID)) { Images.Delete(imageFile.FileID); this.LogInfo("Removed image file <red>{0}", imageFile); } } else { foreach (MDBAudioFile audioFile in AudioFiles.GetStructs(nameof(MDBAudioFile.FileID), mdbFile.ID)) { AudioFiles.Delete(audioFile.FileID); this.LogInfo("Removed audio file <red>{0}", audioFile); } } }
public void GetImage(WebData webData, long imageID, bool thumb = false) { try { MDBImage img = mdb.Images.TryGetStruct(imageID); MDBFile file = mdb.Files.TryGetStruct(img.FileID); if (file.ID > 0) { string fullPath = file.GetFullPath(mdb); if (thumb) { if (TryGetThumb(webData, img, fullPath)) { return; } } webData.Answer = WebAnswer.Raw(webData.Request, WebMessage.Create(fullPath, "/mdb/image/get"), File.ReadAllBytes(fullPath), img.MimeType); webData.Answer.AllowCompression = false; webData.Answer.SetCacheTime(TimeSpan.FromHours(1)); return; } } catch { } webData.Result.AddMessage(webData.Method, WebError.NotFound, "Image {0} cannot be found!", imageID); }
/// <summary>Initializes a new instance of the <see cref="MDBFileIndexedEventArgs"/> class.</summary> /// <param name="updateType">Type of the update.</param> /// <param name="file">The file.</param> public MDBFileIndexedEventArgs(MDBUpdateType updateType, MDBFile file) { UpdateType = updateType; File = file; }
/// <summary>Registers the file.</summary> /// <param name="mdbFolder">The folder.</param> /// <param name="fileName">Name of the file.</param> /// <param name="mdbFile">The MDB file.</param> /// <returns></returns> public MDBUpdateType RegisterFile(MDBFolder mdbFolder, string fileName, out MDBFile mdbFile) { mdbFile = default(MDBFile); string name = Path.GetFileNameWithoutExtension(fileName); string extension = Path.GetExtension(fileName); MDBFileType fileType = GetFileType(extension); if (fileType == MDBFileType.unknown) { return(MDBUpdateType.Ignored); } foreach (MDBFile f in Files.GetStructs( Search.FieldEquals(nameof(MDBFile.Name), name) & Search.FieldEquals(nameof(MDBFile.Extension), extension) & Search.FieldEquals(nameof(MDBFile.FileType), fileType) & Search.FieldEquals(nameof(MDBFile.FolderID), mdbFolder.ID))) { if (mdbFile.ID == 0) { mdbFile = f; } else { this.LogWarning("Delete duplicate file {0}", f); Files.Delete(f.ID); } } string fullPath = mdbFolder.GetFullPath(this, fileName); var fileInfo = new FileInfo(fullPath); long fileSize = fileInfo.Length; DateTime fileLastWriteTime = fileInfo.LastWriteTimeUtc; bool replaceDataset = false; if (mdbFile.Name != name) { replaceDataset = true; this.LogDebug("File new {0}", name); } else if (mdbFile.Extension != extension) { replaceDataset = true; this.LogDebug("File new {0}", name); } else if (mdbFile.DateTime.ToUniversalTime().Ticks != fileLastWriteTime.ToUniversalTime().Ticks) { replaceDataset = true; this.LogDebug("File datetime changed {0}", name); } else if (mdbFile.Size != fileSize) { replaceDataset = true; this.LogDebug("File size changed {0}", name); } else if (mdbFile.FileType != fileType) { replaceDataset = true; this.LogDebug("File type changed {0}", name); } else if (mdbFile.FolderID != mdbFolder.ID) { replaceDataset = true; this.LogDebug("File FolderID changed {0}", name); } if (replaceDataset) { if (mdbFile.ID > 0) { this.LogDebug("File changed? Scanning whole file again: {0}", fullPath); } mdbFile.FileType = fileType; mdbFile.FolderID = mdbFolder.ID; mdbFile.DateTime = fileLastWriteTime; mdbFile.Size = fileSize; mdbFile.Name = name; mdbFile.Extension = extension; } if (mdbFile.ID <= 0) { this.LogInfo("New file <cyan>{0}", fullPath); mdbFile.ID = Files.Insert(mdbFile); return(MDBUpdateType.New); } else if (replaceDataset) { this.LogInfo("Update file <cyan>{0}", fullPath); Files.Replace(mdbFile); return(MDBUpdateType.Updated); } return(MDBUpdateType.NoChange); }
bool SaveImage(byte[] data, MDBFolder mdbFolder, string name, ref MDBImage image, object obj) { string fullPath; var file = new MDBFile() { FolderID = mdbFolder.ID, Name = name, }; ImageType imgType; switch (image.Type) { case MDBImageType.ArtistMusicBanner: case MDBImageType.ArtistMusicLogo: case MDBImageType.ArtistMusicLogoHD: case MDBImageType.AlbumCDArt: fullPath = mdbFolder.GetFullPath(this, name + ".png"); file.Extension = ".png"; file.FileType = MDBFileType.png; imgType = ImageType.Png; image.MimeType = "image/png"; break; default: fullPath = mdbFolder.GetFullPath(this, name + ".jpg"); file.Extension = ".jpg"; imgType = ImageType.Jpeg; file.FileType = MDBFileType.jpeg; image.MimeType = "image/jpg"; break; } int width, height; using (var img = Bitmap32.Create(data)) { width = img.Width; height = img.Height; //save if not present at disk if (!File.Exists(fullPath)) { using (var ms = new MemoryStream()) { img.Save(ms, imgType, 99); data = ms.ToArray(); } } } bool writeFile = true; //find old dataset (check for replace) { if (TryGetFile(fullPath, false, out MDBFile mdbFile)) { file.ID = mdbFile.ID; if (mdbFile.GetFullPath(this) == fullPath) { writeFile = false; } else { string oldPath = mdbFile.GetFullPath(this); File.Delete(oldPath); } } } //save image data if (writeFile) { foreach (string oldFile in Directory.GetFiles(Path.GetDirectoryName(fullPath), Path.GetFileNameWithoutExtension(fullPath) + ".*")) { File.Delete(oldFile); if (TryGetFile(oldFile, false, out MDBFile mdbFile)) { Files.Delete(mdbFile.ID); } this.LogInfo("Deleted old file {0}", oldFile); } File.WriteAllBytes(fullPath, data); this.LogInfo("Saved new image {0}", fullPath); } //get fileinfo var fileInfo = new FileInfo(fullPath); //create file dataset file.DateTime = fileInfo.LastWriteTimeUtc; file.Size = fileInfo.Length; if (file.ID > 0) { Files.Replace(file); } else { file.ID = Files.Insert(file); } //update image dataset image.Width = width; image.Height = height; image.FileID = file.ID; if (Images.Exist(file.ID)) { Images.Replace(image); this.LogNotice("<cyan>Update {0} image<default> dataset for <yellow>{1} <default>{2}", image, obj, mdbFolder); return(false); } else { Images.Insert(image); this.LogNotice("<green>New {0} image<default> dataset for <yellow>{1} <default>{2}", image, obj, mdbFolder); return(true); } }