public void TestExists() { string path = Path.GetTempFileName(); MediaFile mf = new MediaFile(); try { Assert.IsFalse(mf.Exists()); mf.FilePath = path; Assert.IsTrue(mf.Exists()); } finally { File.Delete(path); } }
void UpdateFile() { bool clear_visible = false; if (mediaFile != null) { fileentry.Text = System.IO.Path.GetFileName(mediaFile.FilePath); fileentry.TooltipText = mediaFile.FilePath; if (mediaFile.Exists()) { fileentry.ModifyText(Gtk.StateType.Normal, Helpers.Misc.ToGdkColor(App.Current.Style.TextBase)); } else { fileentry.ModifyText(Gtk.StateType.Normal, Helpers.Misc.ToGdkColor(App.Current.Style.ColorAccentError)); } clear_visible = true; } else if (path != null) { fileentry.Text = System.IO.Path.GetFileName(path); fileentry.TooltipText = path; clear_visible = true; } else { if (FileChooserMode == FileChooserMode.Directory) { fileentry.Text = Catalog.GetString("Select folder..."); } else { fileentry.Text = Catalog.GetString("Select file..."); } fileentry.TooltipText = fileentry.Text; } clearbutton.Sensitive = clear_visible; }
private static void Archive(String file, DateTime createTime, Archive archive) { String ext = Path.GetExtension(file); String folder = createTime.GetFolderName(); String targetDir = Path.Combine(archive.DestinationFullPath, folder); String targetFile = Path.Combine(targetDir, $"{createTime.ToString("MM-dd-yyyy")}_{DateTime.UtcNow.Ticks.ToString()}{ext}"); MD5 hasher = MD5.Create(); // Copy the file if it doesnt already exist. try { // We might need to create the directory. CreateDirectory(targetDir); String hash = null; using (FileStream stream = File.OpenRead(file)) { hash = Convert.ToBase64String(hasher.ComputeHash(stream)); } // Does this file already exist in the databse? if (!MediaFile.Exists(hash)) { using (OrganizerDatabaseContext context = new OrganizerDatabaseContext()) { MediaFile mediaFile = new MediaFile(); mediaFile.ArchiveDateTime = mediaFile.CreatedDateTime = DateTime.UtcNow; mediaFile.ContentHash = hash; mediaFile.OriginalFileName = file; mediaFile.TargetFileName = targetFile; context.MediaFiles.Add(mediaFile); context.SaveChanges(); } } else { // Overwrite it if we can. MediaFile existingFile = MediaFile.GetByHash(hash); if (existingFile != null) // Should never be the case. { if (!File.Exists(existingFile.TargetFileName)) { targetFile = existingFile.TargetFileName; } else { Console.WriteLine($"Skipping: {file}."); } } } // Copy it over. try { Console.WriteLine($"Archiving: {file}."); File.Copy(file, targetFile); } catch { // Delete the media file if we couldnt copy it over. MediaFile.DeleteByHash(hash); } } catch (Exception e) { } }