-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Strip / prepend siteroot when in subfolder #62
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
Showing
4 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
uSync8.ContentEdition/Mapping/Mappers/ImagePathMapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters