Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 34 additions & 34 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<!-- umbraco site -->
<ItemGroup>
<PackageVersion Include="Our.Umbraco.ConditionalDisplayers" Version="16.6.0" />
<PackageVersion Include="Umbraco.Cms" Version="17.3.0" />
<PackageVersion Include="Microsoft.ICU.ICU4C.Runtime" Version="72.1.0.3" />
<PackageVersion Include="Umbraco.Cms.DevelopmentMode.Backoffice" Version="17.3.0" />
<PackageVersion Include="Clean.Core" Version="7.0.5" />
</ItemGroup>
<!-- umbraco libraries for packages -->
<ItemGroup>
<PackageVersion Include="Umbraco.Cms.Core" Version="17.3.0" />
<PackageVersion Include="Umbraco.Cms.Web.Website" Version="17.3.0" />
<PackageVersion Include="Umbraco.Cms.Api.Management" Version="17.3.0" />
</ItemGroup>
<!-- source link -->
<ItemGroup>
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="10.0.201" />
</ItemGroup>
<!-- testing -->
<ItemGroup>
<PackageVersion Include="Umbraco.Cms.Tests" Version="17.3.0" />
<PackageVersion Include="Umbraco.Cms.Tests.Integration" Version="17.3.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="18.4.0" />
<PackageVersion Include="NUnit3TestAdapter" Version="6.2.0" />
<PackageVersion Include="coverlet.collector" Version="8.0.1" />
</ItemGroup>
<!-- command line -->
<ItemGroup>
<PackageVersion Include="NJsonSchema" Version="11.5.2" />
<PackageVersion Include="CommandLineParser" Version="2.9.1" />
</ItemGroup>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<!-- umbraco site -->
<ItemGroup>
<PackageVersion Include="Umbraco.Cms" Version="17.4.2" />
<PackageVersion Include="Umbraco.Cms.DevelopmentMode.Backoffice" Version="17.4.2" />
<PackageVersion Include="Our.Umbraco.ConditionalDisplayers" Version="16.6.0" />
<PackageVersion Include="Microsoft.ICU.ICU4C.Runtime" Version="72.1.0.3" />
<PackageVersion Include="Clean.Core" Version="7.0.5" />
</ItemGroup>
<!-- umbraco libraries for packages -->
<ItemGroup>
<PackageVersion Include="Umbraco.Cms.Core" Version="17.3.0" />
<PackageVersion Include="Umbraco.Cms.Web.Website" Version="17.3.0" />
<PackageVersion Include="Umbraco.Cms.Api.Management" Version="17.3.0" />
</ItemGroup>
<!-- source link -->
<ItemGroup>
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="10.0.201" />
</ItemGroup>
<!-- testing -->
<ItemGroup>
<PackageVersion Include="Umbraco.Cms.Tests" Version="17.3.0" />
<PackageVersion Include="Umbraco.Cms.Tests.Integration" Version="17.3.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="18.4.0" />
<PackageVersion Include="NUnit3TestAdapter" Version="6.2.0" />
<PackageVersion Include="coverlet.collector" Version="8.0.1" />
</ItemGroup>
<!-- command line -->
<ItemGroup>
<PackageVersion Include="NJsonSchema" Version="11.5.2" />
<PackageVersion Include="CommandLineParser" Version="2.9.1" />
</ItemGroup>
</Project>
4 changes: 3 additions & 1 deletion uSync.BackOffice/Extensions/uSyncActionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ public static void UpdateActions<TObject>(this List<uSyncAction> actions, Guid k
public static bool RequiresSave<TObject>(this SyncAttempt<TObject> attempt)
=> attempt.Success && attempt.Change > Core.ChangeType.NoChange && !attempt.Saved && attempt.Item != null;


/// <summary>
/// return the uSyncAction as an ActionView (used in the controllers)
/// </summary>
public static uSyncActionView AsActionView(this uSyncAction action)
{
var msg = string.IsNullOrWhiteSpace(action.Message) is false
Expand Down
12 changes: 12 additions & 0 deletions uSync.BackOffice/Services/ISyncVersionFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@

namespace uSync.BackOffice.Services;

/// <summary>
/// Controls the version file we write to disk on syncs (used to warn if sync is old)
/// </summary>
public interface ISyncVersionFileService
{
/// <summary>
/// get the Sync file version information for a folder.
/// </summary>
Task<SyncFileVersionCheckResult> GetSyncFileInfo(string folder);

/// <summary>
/// write the version information to disk.
/// </summary>
/// <param name="folder"></param>
/// <returns></returns>
Task WriteVersionFileAsync(string folder);
}
14 changes: 14 additions & 0 deletions uSync.BackOffice/Services/SyncVersionFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,23 @@ private bool HmacValuesMatch(XElement node)
}
}

/// <summary>
/// results of a check of the version file
/// </summary>
public class SyncFileVersionCheckResult
{
/// <summary>
/// the sync on disk is current to the current format we are writing.
/// </summary>
public bool IsCurrent { get; set; }

/// <summary>
/// the version we are writing to disk
/// </summary>
public string? FormatVersion { get; set; }

/// <summary>
/// the hmac value for the folders matches. (reserved)
/// </summary>
public bool HmacMatch { get; set; }
}
3 changes: 3 additions & 0 deletions uSync.BackOffice/uSyncBackOffice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ namespace uSync.BackOffice;
/// </summary>
public class uSync
{
/// <summary>
/// assembly version for uSync
/// </summary>
public static Version Version => typeof(uSync).Assembly.GetName().Version ?? new Version(15, 0, 0);

/// <summary>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"homepage": "https://jumoo.co.uk/uSync",
"license": "MPL-2.0",
"type": "module",
"version": "17.3.2",
"version": "17.3.3",
"main": "./dist/usync.js",
"types": "./dist/index.d.ts",
"module": "./dist/usync.js",
Expand Down
150 changes: 8 additions & 142 deletions uSync.Core/Mapping/Mappers/ImagePathMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

using System.Text.RegularExpressions;

using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Media;
using Umbraco.Cms.Core.PropertyEditors.ValueConverters;
using Umbraco.Cms.Core.Services;
using Umbraco.Extensions;

using uSync.Core.Dependency;
using uSync.Core.Extensions;
using uSync.Core.Serialization;

Expand All @@ -28,49 +25,33 @@ namespace uSync.Core.Mapping;
/// becomes
/// {"src":"/media/2cud1lzo/15656993711_ccd199b83e_k.jpg","crops":null}
/// </remarks>
public class ImagePathMapper : SyncValueMapperBase, ISyncMapper
public class ImagePathMapper : ImagePathMapperBase, ISyncMapper
{
private const string _genericMediaPath = "/media";

private readonly string _siteRoot;
private string? _mediaFolder;
private readonly ILogger<ImagePathMapper> _logger;
private readonly IConfiguration _configuration;
private readonly IImageUrlGenerator _imageUrlGenerator;

public ImagePathMapper(
IConfiguration configuration,
IOptionsMonitor<GlobalSettings> _globalOptions,
IEntityService entityService,
ILogger<ImagePathMapper> logger,
IImageUrlGenerator imageUrlGenerator) : base(entityService)
IConfiguration configuration,
IOptionsMonitor<GlobalSettings> globalOptions,
IImageUrlGenerator imageUrlGenerator) : base(entityService, logger, configuration, globalOptions)
{
_logger = logger;
_configuration = configuration;

// todo: site root might need us to include extra NuGet.
_siteRoot = "";

_mediaFolder = GetMediaFolderSetting(_globalOptions.CurrentValue.UmbracoMediaPath.TrimStart('~'));
_globalOptions.OnChange(x => _mediaFolder = GetMediaFolderSetting(x.UmbracoMediaPath.TrimStart('~')));

if (logger.IsEnabled(LogLevel.Debug))
logger.LogDebug("Media Folders: [{media}]", _mediaFolder ?? "(Blank)");

_imageUrlGenerator = imageUrlGenerator;
}

public override string Name => "ImageCropper Mapper";

public override string[] Editors => [
Constants.PropertyEditors.Aliases.ImageCropper,
Constants.PropertyEditors.Aliases.UploadField
Constants.PropertyEditors.Aliases.ImageCropper
];

public override Task<string?> GetExportValueAsync(object value, string editorAlias)
{
return uSyncTaskHelper.FromResultOf(() =>
{
if (_logger.IsEnabled(LogLevel.Debug))
_logger.LogDebug("Getting export value for ImageCropper with value {Value}", value);

var stringValue = value?.ToString();
if (string.IsNullOrWhiteSpace(stringValue)) return stringValue;

Expand Down Expand Up @@ -107,76 +88,6 @@ public ImagePathMapper(
});
}

private string StripSitePath(string filePath)
{
var path = filePath;
if (_siteRoot.Length > 0 && !string.IsNullOrWhiteSpace(filePath) && filePath.InvariantStartsWith(_siteRoot))
path = filePath.Substring(_siteRoot.Length);

return ReplacePath(path, _mediaFolder, _genericMediaPath);
}

private string PrePendSitePath(string filePath)
{
var path = filePath;
if (_siteRoot.Length > 0 && !string.IsNullOrEmpty(filePath))
path = $"{_siteRoot}{filePath}";

return ReplacePath(path, _genericMediaPath, _mediaFolder);
}


/// <summary>
/// makes a specific media path generic.
/// </summary>
/// <remarks>
/// sometimes paths may be defined by umbraco settings, (especially blob settings)
/// that mean they are not stored as /media
///
/// for the sake of generic importing we want the folder stored to be /media.
/// so we re-write the setting on import and export
///
/// assumes you have a app setting in the web.config
///
/// <add key="uSync.mediaFolder">/someFolder</add>
///
/// </remarks>
/// <returns></returns>
private static string ReplacePath(string filePath, string? currentPath, string? targetPath)
{
if (!string.IsNullOrWhiteSpace(targetPath)
&& !string.IsNullOrWhiteSpace(currentPath)
&& !currentPath.Equals(targetPath))
{
return Regex.Replace(filePath, $"^{currentPath}", targetPath, RegexOptions.IgnoreCase);
}

return filePath;
}

/// <summary>
/// Get the media rewrite folder
/// </summary>
/// <remarks>
/// looks in appSettings for uSync:mediaFolder
///
/// <add key="uSync.mediaFolder" value="/something" />
///
/// or in uSync8.config for media setting
///
/// <backoffice>
/// <media>
/// <folder>/someFolder</folder>
/// </media>
/// </backoffice>
/// </remarks>
private string GetMediaFolderSetting(string umbracoMediaPath)
{
var folder = this._configuration.GetValue<string>("uSync:MediaFolder", string.Empty);
if (!string.IsNullOrEmpty(folder)) return folder;

return umbracoMediaPath;
}

public override Task<string?> GetImportValueAsync(string value, string editorAlias, SyncSerializerOptions options)
{
Expand All @@ -202,49 +113,4 @@ private string GetMediaFolderSetting(string umbracoMediaPath)
return json.SerializeJsonNode(true);
});
}

/// <summary>
/// Get the actual media file as a dependency.
/// </summary>
public override Task<IEnumerable<uSyncDependency>> GetDependenciesAsync(object value, string editorAlias, DependencyFlags flags)
{
return uSyncTaskHelper.FromResultOf<IEnumerable<uSyncDependency>>(() =>
{

var stringValue = value?.ToString();
if (string.IsNullOrWhiteSpace(stringValue))
return [];

var stringPath = GetImagePath(stringValue).TrimStart('/').ToLower();

if (!string.IsNullOrWhiteSpace(stringPath))
{
return [new uSyncDependency()
{
Name = $"File: {Path.GetFileName(stringPath)}",
Udi = Udi.Create(Constants.UdiEntityType.MediaFile, stringPath),
Flags = flags,
Order = DependencyOrders.OrderFromEntityType(Constants.UdiEntityType.MediaFile),
Level = 0
}];
}

return [];
});
}

private string GetImagePath(string stringValue)
{
if (stringValue.TryParseToJsonObject(out var json) is false || json is null)
return StripSitePath(stringValue);


if (json.TryGetPropertyValue("src", out var srcNode) is true)
{
var source = srcNode?.GetValue<string>() ?? string.Empty;
if (string.IsNullOrWhiteSpace(source) is false) return source;
}

return string.Empty;
}
}
Loading
Loading