Skip to content

Commit

Permalink
Strip / prepend siteroot when in subfolder #62
Browse files Browse the repository at this point in the history
Removes/Adds the subfolder path from the media file location, when exporting/importing images.
  • Loading branch information
Kevin Jump committed Oct 20, 2019
1 parent 9b0b818 commit bc292f0
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 1 deletion.
113 changes: 113 additions & 0 deletions uSync8.ContentEdition/Mapping/Mappers/ImagePathMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.IO;
using Umbraco.Core.Services;

namespace uSync8.ContentEdition.Mapping.Mappers
{
/// <summary>
/// Mapper for images in an image cropper
/// </summary>
/// <remarks>
/// this image cropper, removes / adds any virtual folder properties
/// to a site (so if you have your umbraco install in virtual folder paths)
///
/// {"src":"/media/2cud1lzo/15656993711_ccd199b83e_k.jpg","crops":null}
/// </remarks>
public class ImagePathMapper : SyncValueMapperBase, ISyncMapper
{
private readonly string siteRoot;

public ImagePathMapper(IEntityService entityService) : base(entityService)
{
siteRoot = SystemDirectories.Root;
}

public override string Name => "ImageCropper Mapper";

public override string[] Editors => new string[]
{
"Umbraco.ImageCropper",
"Umbraco.UploadField"
};

public override string GetExportValue(object value, string editorAlias)
{
var stringValue = value?.ToString();
if (string.IsNullOrWhiteSpace(stringValue)) return stringValue;

if (stringValue.DetectIsJson())
{
// json,
var json = JsonConvert.DeserializeObject<JObject>(stringValue);
if (json != null)
{
var source = json.Value<string>("src");
if (!string.IsNullOrWhiteSpace(source))
{
// strip any virtual directory stuff from it.
json["src"] = StripSitePath(source);
return JsonConvert.SerializeObject(json);
}
}
}
else
{
return StripSitePath(stringValue);
}

return stringValue;
}

private string StripSitePath(string filepath)
{
if (siteRoot.Length > 0 && !string.IsNullOrWhiteSpace(filepath) && filepath.InvariantStartsWith(siteRoot))
return filepath.Substring(siteRoot.Length);

return filepath;
}

private string PrePendSitePath(string filepath)
{
if (siteRoot.Length > 0 && !string.IsNullOrEmpty(filepath))
return $"{siteRoot}{filepath}";

return filepath;
}

public override string GetImportValue(string value, string editorAlias)
{
var stringValue = value?.ToString();
if (string.IsNullOrWhiteSpace(stringValue)) return stringValue;

if (stringValue.DetectIsJson())
{
// json,
var json = JsonConvert.DeserializeObject<JObject>(stringValue);
if (json != null)
{
var source = json.Value<string>("src");
if (!string.IsNullOrWhiteSpace(source))
{
// strip any virtual directory stuff from it.
json["src"] = PrePendSitePath(source);
return JsonConvert.SerializeObject(json);
}
}
}
else
{
return PrePendSitePath(stringValue);
}

return stringValue;
}
}
}
1 change: 1 addition & 0 deletions uSync8.ContentEdition/uSync8.ContentEdition.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@
<Compile Include="Mapping\ISyncMapper.cs" />
<Compile Include="Mapping\Mappers\GridImageMapper.cs" />
<Compile Include="Mapping\Mappers\GridMapper.cs" />
<Compile Include="Mapping\Mappers\ImagePathMapper.cs" />
<Compile Include="Mapping\Mappers\MacroMapper.cs" />
<Compile Include="Mapping\Mappers\MultiUrlMapper.cs" />
<Compile Include="Mapping\Mappers\NestedContentMapper.cs" />
Expand Down
2 changes: 1 addition & 1 deletion uSync8.Site/Web.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration> <!-- usync8 -->
<configuration> <!-- usync8 -->
<!--
Define the web.config template, which is used when creating the initial web.config,
and then transforms from web.Template.[Debug|Release].config are applied. Documentation
Expand Down
4 changes: 4 additions & 0 deletions uSync8.Site/config/logviewer.searches.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
[
{
"name": "All uSync Log Entries",
"query": "StartsWith(SourceContext, 'uSync8')"
},
{
"name": "Find all logs where the Level is NOT Verbose and NOT Debug",
"query": "Not(@Level='Verbose') and Not(@Level='Debug')"
Expand Down

0 comments on commit bc292f0

Please sign in to comment.