private CopyFilesViewModel CreateCopier(double megabytesPerFile, int fileCount) { _sourceAndDestinationPathPairs = new List <KeyValuePair <string, string> >(); for (int i = 0; i < fileCount; i++) { var source = _sourceFolder.GetPathForNewTempFile(false); var dest = _destinationFolder.GetPathForNewTempFile(false); WriteRandomFile(source, megabytesPerFile); _sourceAndDestinationPathPairs.Add(new KeyValuePair <string, string>(source, dest)); } return(new CopyFilesViewModel(_sourceAndDestinationPathPairs)); }
public void NewEntry_ByEntry_TriggersModifiedEntryAdded() { using (var f = new TemporaryFolder("eventTests")) { using (var r = new LiftLexEntryRepository(f.GetPathForNewTempFile(true))) { r.AfterEntryModified += OnEvent; LexEntry entry = r.CreateItem(); r.SaveItem(entry); Assert.IsTrue(_gotEventCall); } } }
public void ModifiedEntry_ByEntry_TriggersModifiedEntryAdded() { using (TemporaryFolder f = new TemporaryFolder("eventTests")) { using (LexEntryRepository r = new LexEntryRepository(f.GetPathForNewTempFile(true))) { LexEntry entry = r.CreateItem(); r.SaveItem(entry); r.AfterEntryModified += OnEvent; entry.Senses.Add(new LexSense()); r.SaveItem(entry); Assert.IsTrue(_gotEventCall); } } }
public void DeleteEntry_ByEntry_TriggersAfterEntryDeleted() { using (TemporaryFolder f = new TemporaryFolder("eventTests")) { using (LiftLexEntryRepository r = new LiftLexEntryRepository(f.GetPathForNewTempFile(true))) { r.AfterEntryDeleted += OnEvent; LexEntry entry = r.CreateItem(); r.SaveItem(entry); r.DeleteItem(entry); Assert.IsTrue(_gotEventCall); } } }
public string GetPathToResizedImage(string originalPath) { string resizedPath; if (_paths.TryGetValue(originalPath, out resizedPath)) { if (File.Exists(resizedPath) && new FileInfo(originalPath).LastWriteTimeUtc <= new FileInfo(resizedPath).LastWriteTimeUtc) { return(resizedPath); } else { _paths.Remove(originalPath); } } var original = Image.FromFile(originalPath); try { if (original.Width > TargetDimension || original.Height > TargetDimension) { var maxDimension = Math.Max(original.Width, original.Height); double shrinkFactor = (TargetDimension / (double)maxDimension); var destWidth = (int)(shrinkFactor * original.Width); var destHeight = (int)(shrinkFactor * original.Height); using (var b = new Bitmap(destWidth, destHeight)) { using (Graphics g = Graphics.FromImage((Image)b)) { //in version 1.0, we used .NearestNeighbor. But if there is a border line down the right size (as is common for thumbnails that, //are, for example, re-inserted into Teacher's Guides), then the line gets cut off. So I switched it to HighQualityBicubic g.InterpolationMode = InterpolationMode.HighQualityBicubic; //.NearestNeighbor;//or smooth it: HighQualityBicubic g.DrawImage(original, 0, 0, destWidth, destHeight); } var temp = _cacheFolder.GetPathForNewTempFile(false, Path.GetExtension(originalPath)); //Hatton July 2012: //Once or twice I saw a GDI+ error on the Save below, when the app 1st launched. //I verified that if there is an IO error, that's what it you get (a GDI+ error). //I looked once, and the %temp%/Bloom directory wasn't there, so that's what I think caused the error. //It's not clear why the temp/bloom directory isn't there... possibly it was there a moment ago //but then some startup thread cleared and deleted it? (we are now running on a thread responding to the http request) Exception error = null; for (int i = 0; i < 5; i++) //try up to five times, a second apart { try { error = null; if (!Directory.Exists(Path.GetDirectoryName(temp))) { Directory.CreateDirectory(Path.GetDirectoryName(temp)); } b.Save(temp, original.RawFormat); break; } catch (Exception e) { Logger.WriteEvent("Error in LowResImage while trying to write image."); Logger.WriteEvent(e.Message); error = e; Thread.Sleep(1000); //wait a second before trying again } } if (error != null) { //NB: this will be on a non-UI thread, so it probably won't work well! ErrorReport.NotifyUserOfProblem(error, "Bloom is having problem saving a low-res version to your temp directory, at " + temp + "\r\n\r\nYou might want to quit and restart Bloom. In the meantime, Bloom will try to use the full-res images."); return(originalPath); } try { _paths.Add(originalPath, temp); //remember it so we can reuse if they show it again, and later delete } catch (Exception) { // it happens sometimes that though it wasn't in the _paths when we entered, it is now // I haven't tracked it down... possibly we get a new request for the image while we're busy compressing it? } return(temp); } } else { return(originalPath); } } finally { original.Dispose(); } }