src/BloomExe/web/controllers/PageTemplatesApi.cs
/// <summary>
/// Usually we expect that a file at the same path but with extension .svg will
/// be found and returned. Failing this we try for one ending in .png. If this still fails we
/// start a process to generate an image from the template page content.
/// </summary>
/// <returns>Should always return a valid image path, unless we really can't come up with an image at all.</returns>
Usually we expect that a file at the same path but with extension .svg will
be found and returned. Failing this we try for one ending in .png. If this still fails we
start a process to generate an image from the template page content.
Usually we expect that a file at the same path but with extension . svg will be found and returned . Failing this we try for one ending in . png . If this still fails we start a process to generate an image from the template page content .
private string FindOrGenerateThumbnail(string expectedPathOfThumbnailImage)
		{
			var localPath = AdjustPossibleLocalHostPathToFilePath(expectedPathOfThumbnailImage);
			var svgpath = Path.ChangeExtension(localPath, "svg");
			if (RobustFile.Exists(svgpath))
			{
				return svgpath;
			}
			var pngpath = Path.ChangeExtension(localPath, "png");
			bool mustRegenerate = false;
			if (RobustFile.Exists(pngpath))
			{
				var f = new FileInfo(pngpath);
				if (f.IsReadOnly)
					return pngpath; 
				if (!IsPageTypeFromCurrentBook(localPath))
					return pngpath;
				mustRegenerate = true; 
			}
			var altpath = GetAlternativeWritablePath(pngpath);
			if (RobustFile.Exists(altpath))
			{
				if (!IsPageTypeFromCurrentBook(localPath))
					return altpath;
				mustRegenerate = true; 
			}
			var templatesDirectoryInTemplateBook = Path.GetDirectoryName(expectedPathOfThumbnailImage);
			var bookPath = Path.GetDirectoryName(templatesDirectoryInTemplateBook);
			var templateBook = _bookFactory(new BookInfo(bookPath,false), _storageFactory(bookPath));
			var caption = Path.GetFileNameWithoutExtension(expectedPathOfThumbnailImage).Trim();
			var isLandscape = caption.EndsWith("-landscape"); 
			if (isLandscape)
				caption = caption.Substring(0, caption.Length - "-landscape".Length);
			var isSquare = caption.EndsWith("-square");
			if (isSquare)
				caption = caption.Substring(0, caption.Length - "-square".Length);
			IPage templatePage = templateBook.GetPages().FirstOrDefault(page => page.Caption.Replace("&", "+").Trim() == caption);
			if (templatePage == null)
				templatePage = templateBook.GetPages().FirstOrDefault(); 
			Image thumbnail = _thumbNailer.GetThumbnailForPage(templateBook, templatePage, isLandscape, isSquare, mustRegenerate);
			var resultPath = "";
			Bitmap clone;
			lock (thumbnail)
			{
				clone = new Bitmap((Image)thumbnail.Clone());
			}
			using (clone)
			{
				try
				{
					Directory.CreateDirectory(templatesDirectoryInTemplateBook);
					clone.Save(pngpath);
					resultPath = pngpath;
				}
				catch (Exception)
				{
					var folder = Path.GetDirectoryName(altpath);
					Directory.CreateDirectory(folder);
					clone.Save(altpath);
					resultPath = altpath;
				}
			}
			return resultPath;
		}
private string FindOrGenerateThumbnail ( string expectedPathOfThumbnailImage ) { var localPath = AdjustPossibleLocalHostPathToFilePath ( expectedPathOfThumbnailImage ) ; var svgpath = Path . ChangeExtension ( localPath , " " ) ; if ( RobustFile . Exists ( svgpath ) ) { return svgpath ; } var pngpath = Path . ChangeExtension ( localPath , " " ) ; bool mustRegenerate = false ; if ( RobustFile . Exists ( pngpath ) ) { var f = new FileInfo ( pngpath ) ; if ( f . IsReadOnly ) return pngpath ; if ( ! IsPageTypeFromCurrentBook ( localPath ) ) return pngpath ; mustRegenerate = true ; } var altpath = GetAlternativeWritablePath ( pngpath ) ; if ( RobustFile . Exists ( altpath ) ) { if ( ! IsPageTypeFromCurrentBook ( localPath ) ) return altpath ; mustRegenerate = true ; } var templatesDirectoryInTemplateBook = Path . GetDirectoryName ( expectedPathOfThumbnailImage ) ; var bookPath = Path . GetDirectoryName ( templatesDirectoryInTemplateBook ) ; var templateBook = _bookFactory ( new BookInfo ( bookPath , false ) , _storageFactory ( bookPath ) ) ; var caption = Path . GetFileNameWithoutExtension ( expectedPathOfThumbnailImage ) . Trim ( ) ; var isLandscape = caption . EndsWith ( " " ) ; if ( isLandscape ) caption = caption . Substring ( 0 , caption . Length - " " . Length ) ; var isSquare = caption . EndsWith ( " " ) ; if ( isSquare ) caption = caption . Substring ( 0 , caption . Length - " " . Length ) ; IPage templatePage = templateBook . GetPages ( ) . FirstOrDefault ( page => page . Caption . Replace ( " " , " " ) . Trim ( ) == caption ) ; if ( templatePage == null ) templatePage = templateBook . GetPages ( ) . FirstOrDefault ( ) ; Image thumbnail = _thumbNailer . GetThumbnailForPage ( templateBook , templatePage , isLandscape , isSquare , mustRegenerate ) ; var resultPath = " " ; Bitmap clone ; lock ( thumbnail ) { clone = new Bitmap ( ( Image ) thumbnail . Clone ( ) ) ; } using ( clone ) { try { Directory . CreateDirectory ( templatesDirectoryInTemplateBook ) ; clone . Save ( pngpath ) ; resultPath = pngpath ; } catch ( Exception ) { var folder = Path . GetDirectoryName ( altpath ) ; Directory . CreateDirectory ( folder ) ; clone . Save ( altpath ) ; resultPath = altpath ; } } return resultPath ; }
Usually we expect that a file at the same path but with extension .svg will
be found and returned.
Usually we expect that a file at the same path but with extension . svg will be found and returned .
// it's locked, don't try and replace it // prevent thumbnailer using cached (obsolete) image // prevent thumbnailer using cached (obsolete) image // We don't have an image, or we want to make a fresh one //note: the caption is used here as a key to find the template page. // matches string in page-chooser.ts // The Replace of & with + corresponds to a replacement made in page-chooser.ts method loadPagesFromCollection. // The Trim is needed because template may now be created by users editing the pageLabel div, and those // labels typically include a trailing newline. // may get something useful?? or throw?? // lock to avoid BL-3781 where we got a "Object is currently in use elsewhere" while doing the Clone() below. // Note: it would appear that the clone isn't even needed, since it was added in the past to overcome this // same contention problem (but, in hindsight, only partially, see?). But for some reason if we just lock the image // until it is saved, we get all gray rectangles. So for now, we just quickly do the clone and unlock. // Review: the coarse lock(SyncObj) in BloomServer.ProcessRequest() may have removed the need for this finer grained lock. //if the directory doesn't exist in the template's directory, make it (i.e. "templates/"). //save this thumbnail so that we don't have to generate it next time
Should always return a valid image path, unless we really can't come up with an image at all.
Should always return a valid image path unless we really can ' t come up with an image at all .
expectedPathOfThumbnailImage
string