-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enable file info to have file with more than one point
Signed-off-by: Chan Jun Wei <chanjunweimy@gmail.com>
- Loading branch information
1 parent
1f9817d
commit 9f7db13
Showing
6 changed files
with
178 additions
and
43 deletions.
There are no files selected for viewing
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
26 changes: 26 additions & 0 deletions
26
aspnet-core/src/Todo.MainProject.Web.Host/Services/Dto/FilenameObject.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,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; } | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
aspnet-core/src/Todo.MainProject.Web.Host/Services/EmbeddedResourceItemWrapper.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,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
64
aspnet-core/src/Todo.MainProject.Web.Host/Services/PathHelper.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,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); | ||
} | ||
} | ||
} |
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
36 changes: 36 additions & 0 deletions
36
aspnet-core/src/Todo.MainProject.Web.Host/Services/PluginEmbeddedResourceItemFileInfo.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,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); | ||
} | ||
} | ||
} |