/
FileSystemMediaProcessor.cs
150 lines (117 loc) · 5.15 KB
/
FileSystemMediaProcessor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright (c) Source Tree Solutions, LLC. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Author: Joe Audette
// Created: 2016-02-12
// Last Modified: 2019-09-02
//
using cloudscribe.SimpleContent.Models;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Threading.Tasks;
namespace cloudscribe.SimpleContent.Services
{
public class FileSystemMediaProcessor : IMediaProcessor
{
public FileSystemMediaProcessor(
ILogger<FileSystemMediaProcessor> logger,
IWebHostEnvironment env)
{
Hosting = env;
Log = logger;
}
protected ILogger Log;
protected IWebHostEnvironment Hosting;
public virtual Task<string> ResolveMediaUrl(string mediaVirtualPath, string fileName)
{
return Task.FromResult(mediaVirtualPath + fileName);
}
public virtual Task SaveMedia(string mediaVirtualPath, string fileName, byte[] bytes)
{
EnsureFsPath(mediaVirtualPath);
var newUrl = mediaVirtualPath + fileName;
var fsPath = Hosting.WebRootPath + newUrl.Replace('/', Path.DirectorySeparatorChar);
File.WriteAllBytes(fsPath, bytes);
return Task.FromResult(0);
}
protected void EnsureFolderPaths(string existingPath, string[] folderNameSegments)
{
if (string.IsNullOrEmpty(existingPath)) return;
if (folderNameSegments.Length == 0) return;
if (!Directory.Exists(existingPath)) return;
var partial = existingPath;
for (var i = 0; i < folderNameSegments.Length; i++)
{
partial = Path.Combine(partial, folderNameSegments[i]);
if (!Directory.Exists(partial))
{
try
{
Directory.CreateDirectory(partial);
}
catch (Exception ex)
{
Log.LogError($"failed to create folder {partial}", ex);
}
}
}
}
private void EnsureFsPath(string mediaVirtualPath)
{
var fsPath = Hosting.WebRootPath + mediaVirtualPath.Replace('/', Path.DirectorySeparatorChar);
if (Directory.Exists(fsPath)) return; //nothing to do
var segments = mediaVirtualPath.Split('/');
EnsureFolderPaths(Hosting.WebRootPath, segments);
}
//private byte[] ConvertToBytes(string base64)
//{
// int index = base64.IndexOf("base64,", StringComparison.Ordinal) + 7;
// return Convert.FromBase64String(base64.Substring(index));
//}
//public virtual Task<string> ConvertBase64EmbeddedImagesToFilesWithUrls(
// string mediaVirtualPath,
// string content)
//{
// if (string.IsNullOrEmpty(content)) { return Task.FromResult(""); }
// // process base64 encoded images into the file system and replace with relative urls
// foreach (Match match in Regex.Matches(content, "(src|href)=\"(data:([^\"]+))\"(>.*?</a>)?"))
// {
// string extension = string.Empty;
// string filename = Guid.NewGuid().ToString();
// // Image
// if (match.Groups[1].Value == "src")
// {
// extension = Regex.Match(match.Value, "data:([^/]+)/([a-z]+);base64").Groups[2].Value;
// }
// // Other file type
// else
// {
// // Entire filename
// extension = Regex.Match(match.Value, "data:([^/]+)/([a-z0-9+-.]+);base64.*\">(.*)</a>").Groups[3].Value;
// }
// if (string.IsNullOrWhiteSpace(extension))
// extension = ".bin";
// else
// extension = "." + extension.Trim('.');
// try
// {
// byte[] bytes = ConvertToBytes(match.Groups[2].Value);
// var newUrl = mediaVirtualPath + filename + extension;
// var fsPath = hosting.WebRootPath + newUrl.Replace('/', Path.DirectorySeparatorChar);
// File.WriteAllBytes(fsPath, bytes);
// string value = string.Format("src=\"{0}\" alt=\"\" ", newUrl);
// if (match.Groups[1].Value == "href")
// value = string.Format("href=\"{0}\"", newUrl);
// Match m = Regex.Match(match.Value, "(src|href)=\"(data:([^\"]+))\"");
// content = content.Replace(m.Value, value);
// }
// catch (Exception ex)
// {
// log.LogError("something went wrong while trying to process media from base64 to the file system", ex);
// }
// }
// return Task.FromResult(content);
//}
}
}