Skip to content

Commit

Permalink
enable file info to have file with more than one point
Browse files Browse the repository at this point in the history
Signed-off-by: Chan Jun Wei <chanjunweimy@gmail.com>
  • Loading branch information
chanjunweimy committed Oct 20, 2017
1 parent 1f9817d commit 9f7db13
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public List<FileContentResult> Download(string pluginName)

private FileContentResult LoadFileFromPath(IFileInfo fileEntry)
{
var filename = fileEntry.Name;
var filename = fileEntry.PhysicalPath + "/" + fileEntry.Name;
var stream = fileEntry.CreateReadStream();
return LoadFileFromStream(stream, filename);
}
Expand All @@ -63,7 +63,7 @@ private FileContentResult LoadFileFromStream(Stream stream, string filename)
private FileContentResult LoadFileFromByteArray(string filename, byte[] fileBytes)
{
var file = File(fileBytes, GetContentType(filename), filename);
System.IO.File.WriteAllBytes(file.FileDownloadName, file.FileContents);
//System.IO.File.WriteAllBytes(file.FileDownloadName, file.FileContents);
return file;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

using System;

namespace Todo.MainProject.Web.Host.Services.Dto
{
public class FilenameObject
{
public FilenameObject(string filePath)
{
if (!filePath.Contains("/"))
{
FileName = filePath;
PhysicalPath = "";
}
else
{
var splitIndex = filePath.LastIndexOf("/", StringComparison.Ordinal);
FileName = filePath.Substring(splitIndex + 1);
PhysicalPath = filePath.Substring(0, splitIndex);
}
}

public string FileName { get; set; }
public string PhysicalPath { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using Abp.Resources.Embedded;

namespace Todo.MainProject.Web.Host.Services
{
internal class EmbeddedResourceItemWrapper
{
public bool Exists { get; set; }
public string PhysicalPath { get; set; }
public string Name { get; set; }
public DateTimeOffset LastModified { get; set; }
public bool IsDirectory { get; set; }

public byte[] Content { get; set; }

public EmbeddedResourceItemWrapper(EmbeddedResourceItem resourceItem)
{
InitializeObject(resourceItem, null);
}

public EmbeddedResourceItemWrapper(EmbeddedResourceItem resourceItem,
string filepath)
{
InitializeObject(resourceItem, filepath);
}

private void InitializeObject(EmbeddedResourceItem resourceItem,
string filepath)
{
Exists = true;
Content = resourceItem.Content;
LastModified = resourceItem.LastModifiedUtc;
IsDirectory = false;

if (filepath == null)
{
PhysicalPath = null;
Name = resourceItem.FileName;
}
else
{
var obj = PathHelper.SplitPathAndName(filepath);
PhysicalPath = obj.PhysicalPath;
Name = obj.FileName;
}
}
}
}
64 changes: 64 additions & 0 deletions aspnet-core/src/Todo.MainProject.Web.Host/Services/PathHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Linq;
using System.Text;
using Abp.Collections.Extensions;
using Abp.Resources.Embedded;
using Todo.MainProject.Web.Host.Services.Dto;

namespace Todo.MainProject.Web.Host.Services
{
public class PathHelper
{
public static string ConvertToRelativePath(EmbeddedResourceSet source, string resourceName)
{
resourceName = resourceName.Substring(source.ResourceNamespace.Length + 1);

var pathParts = resourceName.Split('.');
if (pathParts.Length <= 2)
{
return resourceName;
}

string folder;
string fileName;
if (resourceName.Contains(".module") ||
resourceName.Contains(".component") ||
resourceName.Contains(".routing") ||
resourceName.Contains(".directive") ||
resourceName.Contains(".service"))
{
folder = pathParts.Take(pathParts.Length - 3).Select(NormalizeFolderName).JoinAsString("/");
fileName = pathParts[pathParts.Length - 3] + "." +
pathParts[pathParts.Length - 2] + "." +
pathParts[pathParts.Length - 1];
}
else
{
folder = pathParts.Take(pathParts.Length - 2).Select(NormalizeFolderName).JoinAsString("/");
fileName = pathParts[pathParts.Length - 2] + "." + pathParts[pathParts.Length - 1];
}
return folder + "/" + fileName;
}

public static string NormalizeFolderName(string pathPart)
{
//TODO: Implement all rules of .NET
return pathPart.Replace('-', '_');
}

public static string CalculateFileName(string filePath)
{
if (!filePath.Contains("/"))
{
return filePath;
}

return filePath.Substring(filePath.LastIndexOf("/", StringComparison.Ordinal) + 1);
}

public static FilenameObject SplitPathAndName(string filePath)
{
return new FilenameObject(filePath);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Abp.AspNetCore.EmbeddedResources;
using Abp.Collections.Extensions;
using Abp.Dependency;
using Abp.Resources.Embedded;
using Microsoft.Extensions.FileProviders;
Expand Down Expand Up @@ -45,7 +43,7 @@ public IFileInfo GetFileInfo(string subpath)
return new NotFoundFileInfo(subpath);
}

return new EmbeddedResourceItemFileInfo(resource);
return new PluginEmbeddedResourceItemFileInfo(resource, subpath);
}

private EmbeddedResourceItem GetFileEmbeddedResourceItem(string subpath)
Expand Down Expand Up @@ -117,49 +115,12 @@ private IDirectoryContents GetPluginDirectoryContents(string pluginName)
{
continue;
}
var filePath = source.RootPath + ConvertToRelativePath(source, resourceName);
var filePath = source.RootPath + PathHelper.ConvertToRelativePath(source, resourceName);
var fileInfo = GetFileInfo(filePath);
fileInfos.Add(fileInfo);
}
IDirectoryContents directoryContents = new PluginEmbeddedDirectoryContents(fileInfos.GetEnumerator());
return directoryContents;
}

private string ConvertToRelativePath(EmbeddedResourceSet source, string resourceName)
{
resourceName = resourceName.Substring(source.ResourceNamespace.Length + 1);

var pathParts = resourceName.Split('.');
if (pathParts.Length <= 2)
{
return resourceName;
}

string folder;
string fileName;
if (resourceName.Contains(".module") ||
resourceName.Contains(".component") ||
resourceName.Contains(".routing") ||
resourceName.Contains(".directive") ||
resourceName.Contains(".service"))
{
folder = pathParts.Take(pathParts.Length - 3).Select(NormalizeFolderName).JoinAsString("/");
fileName = pathParts[pathParts.Length - 3] + "." +
pathParts[pathParts.Length - 2] + "." +
pathParts[pathParts.Length - 1];
}
else
{
folder = pathParts.Take(pathParts.Length - 2).Select(NormalizeFolderName).JoinAsString("/");
fileName = pathParts[pathParts.Length - 2] + "." + pathParts[pathParts.Length - 1];
}
return folder + "/" + fileName;
}

private static string NormalizeFolderName(string pathPart)
{
//TODO: Implement all rules of .NET
return pathPart.Replace('-', '_');
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.IO;
using Abp.Resources.Embedded;
using Microsoft.Extensions.FileProviders;

namespace Todo.MainProject.Web.Host.Services
{
public class PluginEmbeddedResourceItemFileInfo : IFileInfo
{
public bool Exists => _embeddedResourceItemWrapper.Exists;
public long Length => _embeddedResourceItemWrapper.Content.Length;
public string PhysicalPath => _embeddedResourceItemWrapper.PhysicalPath;
public string Name => _embeddedResourceItemWrapper.Name;
public DateTimeOffset LastModified => _embeddedResourceItemWrapper.LastModified;
public bool IsDirectory => _embeddedResourceItemWrapper.IsDirectory;

private readonly EmbeddedResourceItemWrapper _embeddedResourceItemWrapper;

public PluginEmbeddedResourceItemFileInfo(EmbeddedResourceItem resourceItem)
{
_embeddedResourceItemWrapper = new EmbeddedResourceItemWrapper(resourceItem);
}

public PluginEmbeddedResourceItemFileInfo(EmbeddedResourceItem resourceItem,
string filepath)
{
_embeddedResourceItemWrapper = new EmbeddedResourceItemWrapper(resourceItem,
filepath);
}

public Stream CreateReadStream()
{
return new MemoryStream(_embeddedResourceItemWrapper.Content);
}
}
}

0 comments on commit 9f7db13

Please sign in to comment.