Skip to content

Commit

Permalink
# Changes
Browse files Browse the repository at this point in the history
+ Add custom search capability
+ Refactor some configuration methods
  • Loading branch information
trannamtrung1st committed Apr 30, 2022
1 parent 36c5d5f commit 258ca55
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
//const appCmds = ['open', ...];
const appCmds = elFinder.prototype._options.commands;
elFinder.prototype.i18.en.messages.TextArea = "Edit";
elFinder.prototype.i18.en.messages.btnSearchFolder = "Folder only";
elFinder.prototype.i18.en.messages.searchFolder = "Search for folder only";
// If you integrate with Quota management plugin
elFinder.prototype.i18.en.messages.errQuota = "Exceeding quota limits.<br/>Maximum: $1<br/>Usage: $2<br/>"
Expand Down Expand Up @@ -54,6 +56,23 @@
lang: 'vi',
requestType: 'post',
uploadMaxChunkSize: 1024 * 1024 * 10,
commandsOptions: {
search: {
// Additional search types
searchTypes: {
// "SearchMime" is implemented in default
SearchMime: { // The key is search type that send to the connector
name: 'btnMime', // Button text to be processed in i18n()
title: 'searchMime' // Button title to be processed in i18n()
},
// This is custom search method for a function `searchMatchContents()` of your customized volume driver
FolderOnly: {
name: 'btnSearchFolder',
title: 'searchFolder.'
}
}
}
}
//onlyMimes: ["image", "text/plain"] // Get files of requested mime types only
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using elFinder.Net.Core;
using elFinder.Net.Core.Extensions;
using elFinder.Net.Core.Models.Command;
using elFinder.Net.Core.Models.Response;
using elFinder.Net.Core.Services;
using elFinder.Net.Core.Services.Drawing;
using elFinder.Net.Drivers.FileSystem;
using elFinder.Net.Drivers.FileSystem.Services;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace elFinder.Net.AdvancedDemo.Services
{
public class ApplicationFileSystemDriver : FileSystemDriver
{
public ApplicationFileSystemDriver(
IPathParser pathParser,
IPictureEditor pictureEditor,
IVideoEditor videoEditor,
IZipDownloadPathProvider zipDownloadPathProvider,
IZipFileArchiver zipFileArchiver,
IThumbnailBackgroundGenerator thumbnailBackgroundGenerator,
ICryptographyProvider cryptographyProvider,
IConnector connector,
IConnectorManager connectorManager,
ITempFileCleaner tempFileCleaner)
: base(pathParser, pictureEditor, videoEditor,
zipDownloadPathProvider, zipFileArchiver,
thumbnailBackgroundGenerator, cryptographyProvider,
connector, connectorManager, tempFileCleaner)
{
}

public async Task<SearchResponse> SearchMatchFolderOnly(SearchCommand cmd, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();

var searchResp = new SearchResponse();
var targetPath = cmd.TargetPath;
var volume = targetPath.Volume;

foreach (var item in await targetPath.Directory.GetDirectoriesAsync(cmd.Q,
searchOption: SearchOption.AllDirectories, cancellationToken: cancellationToken))
{
var hash = item.GetHash(volume, pathParser);
var parentHash = item.Parent.Equals(targetPath.Directory) ? targetPath.HashedTarget :
item.GetParentHash(volume, pathParser);
searchResp.files.Add(await item.ToFileInfoAsync(hash, parentHash, volume, connector.Options, cancellationToken: cancellationToken));
}

return searchResp;
}
}
}
12 changes: 7 additions & 5 deletions elFinder.Net.Core/Demos/elFinder.Net.AdvancedDemo/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ public void ConfigureServices(IServiceCollection services)
var pluginCollection = new PluginCollection();

services.AddElFinderAspNetCore()
.AddFileSystemDriver(tempFileCleanerConfig: (opt) =>
{
opt.ScanFolders.Add(TempPath, TempFileCleanerOptions.DefaultUnmanagedLifeTime);
})
.AddFileSystemQuotaManagement(pluginCollection)
.AddFileSystemDriver(typeof(ApplicationFileSystemDriver),
tempFileCleanerConfig: (opt) =>
{
opt.ScanFolders.Add(TempPath, TempFileCleanerOptions.DefaultUnmanagedLifeTime);
})
.AddFileSystemQuotaManagement(pluginCollection,
fileSystemDriverImplType: typeof(ApplicationFileSystemDriver))
.AddElFinderLoggingExample(pluginCollection)
.AddElFinderPlugins(pluginCollection);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<RepositoryUrl>https://github.com/trannamtrung1st/elFinder.Net.Core</RepositoryUrl>
<RepositoryType>OSS</RepositoryType>
<PackageTags>elFinder, connector, file-manager, driver, plugins</PackageTags>
<Version>1.4.4</Version>
<Version>1.5.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<IsPackable>true</IsPackable>
<Description>This package enables ASP.NET Core 2.2 projects to easily integrate the elFinder.Net.Core connector. See the example project on Github for usage detail.</Description>
<PackageIcon>logo.png</PackageIcon>
<Version>1.4.4</Version>
<Version>1.5.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions elFinder.Net.Core/elFinder.Net.Core/Connector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ protected virtual async Task<ConnectorResult> ProcessCoreAsync(ConnectorCommand
searchCmd.TargetPath = await ParsePathAsync(searchCmd.Target, cancellationToken: cancellationToken);
searchCmd.Q = args.GetValueOrDefault(ConnectorCommand.Param_Q);
searchCmd.Mimes = args.GetValueOrDefault(ConnectorCommand.Param_MimesArr);
searchCmd.Type = args.GetValueOrDefault(ConnectorCommand.Param_Type);
cmd.CmdObject = searchCmd;

SearchResponse finalSearchResp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace elFinder.Net.Core.Models.Command
{
public class SearchCommand : TargetCommand
{
public string Type { get; set; }
public string Q { get; set; }
public StringValues Mimes { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
See the example project on Github for usage detail.
</Description>
<PackageIcon>logo.png</PackageIcon>
<Version>1.4.4</Version>
<Version>1.5.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ namespace elFinder.Net.Drivers.FileSystem.Extensions
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddFileSystemDriver(this IServiceCollection services,
Type driverType = null,
Action<TempFileCleanerOptions> tempFileCleanerConfig = null)
{
if (tempFileCleanerConfig == null)
tempFileCleanerConfig = (opt) => { };

return services.AddScoped<IDriver, FileSystemDriver>()
return services.AddScoped(typeof(IDriver), driverType ?? typeof(FileSystemDriver))
.AddSingleton<IZipDownloadPathProvider, TempZipDownloadPathProvider>()
.AddSingleton<IZipFileArchiver, ZipFileArchiver>()
.AddSingleton<IThumbnailBackgroundGenerator, DefaultThumbnailBackgroundGenerator>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class FileSystemDriver : IDriver
{
public const string ChunkingFolderPrefix = "_uploading_";
public const string DefaultThumbExt = ".png";
public const string CustomSearchMethodPrefix = "SearchMatch";
private static readonly char[] InvalidFileNameChars = Path.GetInvalidFileNameChars();

protected readonly IPathParser pathParser;
Expand Down Expand Up @@ -1320,27 +1321,51 @@ public virtual async Task<SearchResponse> SearchAsync(SearchCommand cmd, Cancell

if (!targetPath.Directory.ObjectAttribute.Read) throw new PermissionDeniedException();

foreach (var item in await targetPath.Directory.GetFilesAsync(cmd.Q, cmd.Mimes,
searchOption: SearchOption.AllDirectories, cancellationToken: cancellationToken))
if (!string.IsNullOrWhiteSpace(cmd.Type))
{
var parentHash = item.Parent.Equals(targetPath.Directory) ? targetPath.HashedTarget :
item.GetParentHash(volume, pathParser);
searchResp.files.Add(await item.ToFileInfoAsync(parentHash, volume, pathParser, pictureEditor, videoEditor, cancellationToken: cancellationToken));
}
var method = GetType().GetMethod($"{CustomSearchMethodPrefix}{cmd.Type}",
new Type[] { typeof(SearchCommand), typeof(CancellationToken) });

if (cmd.Mimes.Count == 0)
if (method == null)
{
throw new KeyNotFoundException("Custom search function not found");
}

if (method.ReturnType != typeof(Task<SearchResponse>))
{
throw new InvalidCastException("Invalid return type");
}

Task<SearchResponse> task = method.Invoke(this, new object[] { cmd, cancellationToken }) as Task<SearchResponse>;

return task != null
? await task
: throw new InvalidOperationException("Invalid response from custom search function");
}
else
{
foreach (var item in await targetPath.Directory.GetDirectoriesAsync(cmd.Q,
foreach (var item in await targetPath.Directory.GetFilesAsync(cmd.Q, cmd.Mimes,
searchOption: SearchOption.AllDirectories, cancellationToken: cancellationToken))
{
var hash = item.GetHash(volume, pathParser);
var parentHash = item.Parent.Equals(targetPath.Directory) ? targetPath.HashedTarget :
item.GetParentHash(volume, pathParser);
searchResp.files.Add(await item.ToFileInfoAsync(hash, parentHash, volume, connector.Options, cancellationToken: cancellationToken));
searchResp.files.Add(await item.ToFileInfoAsync(parentHash, volume, pathParser, pictureEditor, videoEditor, cancellationToken: cancellationToken));
}

if (cmd.Mimes.Count == 0)
{
foreach (var item in await targetPath.Directory.GetDirectoriesAsync(cmd.Q,
searchOption: SearchOption.AllDirectories, cancellationToken: cancellationToken))
{
var hash = item.GetHash(volume, pathParser);
var parentHash = item.Parent.Equals(targetPath.Directory) ? targetPath.HashedTarget :
item.GetParentHash(volume, pathParser);
searchResp.files.Add(await item.ToFileInfoAsync(hash, parentHash, volume, connector.Options, cancellationToken: cancellationToken));
}
}
}

return searchResp;
return searchResp;
}
}

public virtual async Task<ArchiveResponse> ArchiveAsync(ArchiveCommand cmd, CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<IsPackable>true</IsPackable>
<Description>elFinder.Net.Core Local File System driver.</Description>
<PackageIcon>logo.png</PackageIcon>
<Version>1.4.4</Version>
<Version>1.5.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 258ca55

Please sign in to comment.