From cfd8435d684a3f3e8f5841feb327245cefb537fe Mon Sep 17 00:00:00 2001 From: Stephan Date: Mon, 9 Jan 2017 17:49:12 +0100 Subject: [PATCH] deploy-150 - wire GetSize in services --- src/Umbraco.Core/IO/FileSystemExtensions.cs | 7 ++- .../Repositories/FileRepository.cs | 29 ++++++++--- .../Interfaces/IPartialViewRepository.cs | 1 + .../Interfaces/IScriptRepository.cs | 1 + .../Interfaces/IStylesheetRepository.cs | 1 + .../Interfaces/ITemplateRepository.cs | 2 + .../Interfaces/IXsltFileRepository.cs | 1 + .../Repositories/StylesheetRepository.cs | 14 ++++++ .../Repositories/TemplateRepository.cs | 5 ++ src/Umbraco.Core/Services/FileService.cs | 48 +++++++++++++++++++ src/Umbraco.Core/Services/IFileService.cs | 42 ++++++++++++++++ src/Umbraco.Core/Services/IMediaService.cs | 7 +++ src/Umbraco.Core/Services/MediaService.cs | 5 ++ 13 files changed, 152 insertions(+), 11 deletions(-) diff --git a/src/Umbraco.Core/IO/FileSystemExtensions.cs b/src/Umbraco.Core/IO/FileSystemExtensions.cs index aeb6f9a42a..be09f1e310 100644 --- a/src/Umbraco.Core/IO/FileSystemExtensions.cs +++ b/src/Umbraco.Core/IO/FileSystemExtensions.cs @@ -38,13 +38,12 @@ namespace Umbraco.Core.IO } // GetSize has been added to IFileSystem2 but not IFileSystem - // this is implementing GetSize for IFileSystem, the old way public static long GetSize(this IFileSystem fs, string path) { - // if we reach this point, fs is *not* IFileSystem2 - // so it's not FileSystemWrapper nor shadow nor anything we know - // so... fall back to the old & inefficient method + var fs2 = fs as IFileSystem2; + if (fs2 != null) return fs2.GetSize(path); + // this is implementing GetSize for IFileSystem, the old way using (var file = fs.OpenFile(path)) { return file.Length; diff --git a/src/Umbraco.Core/Persistence/Repositories/FileRepository.cs b/src/Umbraco.Core/Persistence/Repositories/FileRepository.cs index 7ce0d71097..03a8b7e824 100644 --- a/src/Umbraco.Core/Persistence/Repositories/FileRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/FileRepository.cs @@ -234,13 +234,28 @@ namespace Umbraco.Core.Persistence.Repositories } } - /// - /// Dispose any disposable properties - /// - /// - /// Dispose the unit of work - /// - protected override void DisposeResources() + public long GetFileSize(string filename) + { + if (FileSystem.FileExists(filename) == false) + return -1; + + try + { + return FileSystem.GetSize(filename); + } + catch + { + return -1; // deal with race conds + } + } + + /// + /// Dispose any disposable properties + /// + /// + /// Dispose the unit of work + /// + protected override void DisposeResources() { _work.DisposeIfDisposable(); } diff --git a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IPartialViewRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Interfaces/IPartialViewRepository.cs index 7c8391a77a..27342fe643 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IPartialViewRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Interfaces/IPartialViewRepository.cs @@ -10,5 +10,6 @@ namespace Umbraco.Core.Persistence.Repositories bool ValidatePartialView(IPartialView partialView); Stream GetFileContentStream(string filepath); void SetFileContent(string filepath, Stream content); + long GetFileSize(string filepath); } } \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IScriptRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Interfaces/IScriptRepository.cs index eacb818faa..c2c0a0ae84 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IScriptRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Interfaces/IScriptRepository.cs @@ -8,5 +8,6 @@ namespace Umbraco.Core.Persistence.Repositories bool ValidateScript(Script script); Stream GetFileContentStream(string filepath); void SetFileContent(string filepath, Stream content); + long GetFileSize(string filepath); } } \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IStylesheetRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Interfaces/IStylesheetRepository.cs index 323f11d339..5fadf7b01c 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IStylesheetRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Interfaces/IStylesheetRepository.cs @@ -8,5 +8,6 @@ namespace Umbraco.Core.Persistence.Repositories bool ValidateStylesheet(Stylesheet stylesheet); Stream GetFileContentStream(string filepath); void SetFileContent(string filepath, Stream content); + long GetFileSize(string filepath); } } \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Repositories/Interfaces/ITemplateRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Interfaces/ITemplateRepository.cs index 59845f53f0..fd04e21d75 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Interfaces/ITemplateRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Interfaces/ITemplateRepository.cs @@ -72,5 +72,7 @@ namespace Umbraco.Core.Persistence.Repositories /// The filesystem path to the template. /// The content of the template. void SetFileContent(string filepath, Stream content); + + long GetFileSize(string filepath); } } \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IXsltFileRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Interfaces/IXsltFileRepository.cs index 8bff985400..4c56b6eb18 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IXsltFileRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Interfaces/IXsltFileRepository.cs @@ -8,5 +8,6 @@ namespace Umbraco.Core.Persistence.Repositories bool ValidateXsltFile(XsltFile xsltFile); Stream GetFileContentStream(string filepath); void SetFileContent(string filepath, Stream content); + long GetFileSize(string filepath); } } \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Repositories/StylesheetRepository.cs b/src/Umbraco.Core/Persistence/Repositories/StylesheetRepository.cs index 45fff2e43b..7515b8513a 100644 --- a/src/Umbraco.Core/Persistence/Repositories/StylesheetRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/StylesheetRepository.cs @@ -144,6 +144,20 @@ namespace Umbraco.Core.Persistence.Repositories FileSystem.AddFile(filepath, content, true); } + public long GetFileSize(string filepath) + { + if (FileSystem.FileExists(filepath) == false) return -1; + + try + { + return FileSystem.GetSize(filepath); + } + catch + { + return -1; // deal with race conds + } + } + #endregion } } \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Repositories/TemplateRepository.cs b/src/Umbraco.Core/Persistence/Repositories/TemplateRepository.cs index 620ad10e7d..41743601fb 100644 --- a/src/Umbraco.Core/Persistence/Repositories/TemplateRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/TemplateRepository.cs @@ -513,6 +513,11 @@ namespace Umbraco.Core.Persistence.Repositories GetFileSystem(filepath).AddFile(filepath, content, true); } + public long GetFileSize(string filepath) + { + return GetFileSystem(filepath).GetSize(filepath); + } + private IFileSystem GetFileSystem(string filepath) { var ext = Path.GetExtension(filepath); diff --git a/src/Umbraco.Core/Services/FileService.cs b/src/Umbraco.Core/Services/FileService.cs index aa722e0133..6360ad8988 100644 --- a/src/Umbraco.Core/Services/FileService.cs +++ b/src/Umbraco.Core/Services/FileService.cs @@ -569,6 +569,14 @@ namespace Umbraco.Core.Services } } + public long GetTemplateFileSize(string filepath) + { + using (var repository = RepositoryFactory.CreateTemplateRepository(UowProvider.GetUnitOfWork())) + { + return repository.GetFileSize(filepath); + } + } + #endregion public Stream GetStylesheetFileContentStream(string filepath) @@ -587,6 +595,14 @@ namespace Umbraco.Core.Services } } + public long GetStylesheetFileSize(string filepath) + { + using (var repository = RepositoryFactory.CreateStylesheetRepository(UowProvider.GetUnitOfWork())) + { + return repository.GetFileSize(filepath); + } + } + public Stream GetScriptFileContentStream(string filepath) { using (var repository = RepositoryFactory.CreateScriptRepository(UowProvider.GetUnitOfWork())) @@ -603,6 +619,14 @@ namespace Umbraco.Core.Services } } + public long GetScriptFileSize(string filepath) + { + using (var repository = RepositoryFactory.CreateScriptRepository(UowProvider.GetUnitOfWork())) + { + return repository.GetFileSize(filepath); + } + } + public Stream GetXsltFileContentStream(string filepath) { using (var repository = RepositoryFactory.CreateXsltFileRepository(UowProvider.GetUnitOfWork())) @@ -619,6 +643,14 @@ namespace Umbraco.Core.Services } } + public long GetXsltFileSize(string filepath) + { + using (var repository = RepositoryFactory.CreateXsltFileRepository(UowProvider.GetUnitOfWork())) + { + return repository.GetFileSize(filepath); + } + } + #region Partial Views public IEnumerable GetPartialViewSnippetNames(params string[] filterNames) @@ -890,6 +922,14 @@ namespace Umbraco.Core.Services } } + public long GetPartialViewMacroFileSize(string filepath) + { + using (var repository = GetPartialViewRepository(PartialViewType.PartialViewMacro, UowProvider.GetUnitOfWork())) + { + return repository.GetFileSize(filepath); + } + } + public Stream GetPartialViewFileContentStream(string filepath) { using (var repository = GetPartialViewRepository(PartialViewType.PartialView, UowProvider.GetUnitOfWork())) @@ -906,6 +946,14 @@ namespace Umbraco.Core.Services } } + public long GetPartialViewFileSize(string filepath) + { + using (var repository = GetPartialViewRepository(PartialViewType.PartialView, UowProvider.GetUnitOfWork())) + { + return repository.GetFileSize(filepath); + } + } + #endregion private void Audit(AuditType type, string message, int userId, int objectId) diff --git a/src/Umbraco.Core/Services/IFileService.cs b/src/Umbraco.Core/Services/IFileService.cs index 7d266ba64c..5115a25087 100644 --- a/src/Umbraco.Core/Services/IFileService.cs +++ b/src/Umbraco.Core/Services/IFileService.cs @@ -254,6 +254,13 @@ namespace Umbraco.Core.Services /// The content of the template. void SetTemplateFileContent(string filepath, Stream content); + /// + /// Gets the size of a template. + /// + /// The filesystem path to the template. + /// The size of the template. + long GetTemplateFileSize(string filepath); + /// /// Gets the content of a stylesheet as a stream. /// @@ -268,6 +275,13 @@ namespace Umbraco.Core.Services /// The content of the stylesheet. void SetStylesheetFileContent(string filepath, Stream content); + /// + /// Gets the size of a stylesheet. + /// + /// The filesystem path to the stylesheet. + /// The size of the stylesheet. + long GetStylesheetFileSize(string filepath); + /// /// Gets the content of a script file as a stream. /// @@ -282,6 +296,13 @@ namespace Umbraco.Core.Services /// The content of the script file. void SetScriptFileContent(string filepath, Stream content); + /// + /// Gets the size of a script file. + /// + /// The filesystem path to the script file. + /// The size of the script file. + long GetScriptFileSize(string filepath); + /// /// Gets the content of a XSLT file as a stream. /// @@ -296,6 +317,13 @@ namespace Umbraco.Core.Services /// The content of the XSLT file. void SetXsltFileContent(string filepath, Stream content); + /// + /// Gets the size of a XSLT file. + /// + /// The filesystem path to the XSLT file. + /// The size of the XSLT file. + long GetXsltFileSize(string filepath); + /// /// Gets the content of a macro partial view as a stream. /// @@ -310,6 +338,13 @@ namespace Umbraco.Core.Services /// The content of the macro partial view. void SetPartialViewMacroFileContent(string filepath, Stream content); + /// + /// Gets the size of a macro partial view. + /// + /// The filesystem path to the macro partial view. + /// The size of the macro partial view. + long GetPartialViewMacroFileSize(string filepath); + /// /// Gets the content of a partial view as a stream. /// @@ -323,5 +358,12 @@ namespace Umbraco.Core.Services /// The filesystem path to the partial view. /// The content of the partial view. void SetPartialViewFileContent(string filepath, Stream content); + + /// + /// Gets the size of a partial view. + /// + /// The filesystem path to the partial view. + /// The size of the partial view. + long GetPartialViewFileSize(string filepath); } } \ No newline at end of file diff --git a/src/Umbraco.Core/Services/IMediaService.cs b/src/Umbraco.Core/Services/IMediaService.cs index 257ee10d9c..510a1e26ca 100644 --- a/src/Umbraco.Core/Services/IMediaService.cs +++ b/src/Umbraco.Core/Services/IMediaService.cs @@ -430,6 +430,13 @@ namespace Umbraco.Core.Services /// The content of the media. void SetMediaFileContent(string filepath, Stream content); + /// + /// Gets the size of a media. + /// + /// The filesystem path to the media. + /// The size of the media. + long GetMediaFileSize(string filepath); + /// /// Deletes a media file and all thumbnails. /// diff --git a/src/Umbraco.Core/Services/MediaService.cs b/src/Umbraco.Core/Services/MediaService.cs index b7cc002d96..f582ef1eaa 100644 --- a/src/Umbraco.Core/Services/MediaService.cs +++ b/src/Umbraco.Core/Services/MediaService.cs @@ -1361,6 +1361,11 @@ namespace Umbraco.Core.Services _mediaFileSystem.AddFile(filepath, stream, true); } + public long GetMediaFileSize(string filepath) + { + return _mediaFileSystem.GetSize(filepath); + } + public void DeleteMediaFile(string filepath) { _mediaFileSystem.DeleteFile(filepath, true);