Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix for: File picker is not working for virtual UNC directories + improvements #13

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ function filePickerController($scope, dialogService) {
};

function populate(data) {
var file = $scope.model.config.folder + data;
var file = data;

if ($scope.model.value.indexOf(file) === -1) {
$scope.model.value.push(file);
}
};
};

angular.module("umbraco").controller("Our.Umbraco.FilePickerController", filePickerController);

function folderPickerController($scope, dialogService) {
Expand All @@ -34,11 +36,12 @@ function folderPickerController($scope, dialogService) {
callback: populate
});
};

function populate(data) {
$scope.model.value = "/" + data;
$scope.model.value = data;
};

};

angular.module("umbraco").controller("Our.Umbraco.FolderPickerController", folderPickerController);

function filePickerDialogController($scope, dialogService) {
Expand All @@ -48,10 +51,12 @@ function filePickerDialogController($scope, dialogService) {
function nodeSelectHandler(ev, args) {
args.event.preventDefault();
args.event.stopPropagation();

if (args.node.icon !== "icon-folder")
$scope.submit(args.node.id);
};
};

angular.module("umbraco").controller("Our.Umbraco.FilePickerDialogController", filePickerDialogController);

function folderPickerDialogController($scope, dialogService) {
Expand All @@ -64,4 +69,5 @@ function folderPickerDialogController($scope, dialogService) {
$scope.submit(args.node.id);
};
};

angular.module("umbraco").controller("Our.Umbraco.FolderPickerDialogController", folderPickerDialogController);
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<ul class="unstyled" ng-show="model.value.length !== 0" ng-repeat="item in model.value">
<li>
<a href="#" prevent-default ng-click="remove(item)">
<i class="icon icon-delete red"></i> {{item}}
<i class="icon icon-delete red"></i> <i class="icon icon-document blue"></i> {{item}}
</a>
</li>
</ul>
<ul class="unstyled list-icons">
<li>
<i class="icon icon-add blue"></i>
<a href="#" ng-click="openPicker()" prevent-default="">
<i class="icon icon-add blue"></i>
<localize key="general_add">Add</localize>
</a>
</li>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<div class="umb-panel" ng-controller="Our.Umbraco.FilePickerDialogController">
<div class="umb-panel-body no-header">
<umb-tree section="dummy"
treealias="fileTree"
<umb-tree section="filePickerDialog"
treealias="filePickerTree"
eventhandler="dialogEventHandler"
hideoptions="true"
hideheader="true"
isdialog="true"
customtreeparams="startfolder={{dialogData.folder}}&amp;filter={{dialogData.filter}}&amp;files=true"
customtreeparams="startfolder={{dialogData.folder}}&amp;filter={{dialogData.filter}}"
/>
</div>
<div class="umb-panel-footer">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<div ng-controller="Our.Umbraco.FolderPickerController">

<ul class="unstyled">
<li>
{{model.value}}
Expand All @@ -9,5 +8,4 @@
</a>
</li>
</ul>

</div>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="umb-panel" ng-controller="Our.Umbraco.FolderPickerDialogController">
<div class="umb-panel-body no-header">
<umb-tree section="dummy"
treealias="fileTree"
<umb-tree section="filePickerDialog"
treealias="filePickerTree"
eventhandler="dialogEventHandler"
hideoptions="true"
hideheader="true"
Expand Down
39 changes: 0 additions & 39 deletions Src/Our.Umbraco.FilePicker/Controllers/FilePickerApiController.cs

This file was deleted.

140 changes: 140 additions & 0 deletions Src/Our.Umbraco.FilePicker/Controllers/FilePickerTreeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http.Formatting;
using Umbraco.Core;
using Umbraco.Core.IO;
using Umbraco.Web.Models.Trees;
using Umbraco.Web.Mvc;
using Umbraco.Web.Trees;

namespace Our.Umbraco.FilePicker.Controllers
{
[Tree("filePickerDialog", "filePickerTree", "Folders and files")]
[PluginController("FilePicker")]
public class FilePickerTreeController : TreeController
{
bool _isFolderPicker;

string _rootVirtualPath;
string _rootPath;

string _currentVirtualPath;
string _currentPath;

string[] _fileExtensionFilters;

FormDataCollection _queryStrings;

protected override TreeNodeCollection GetTreeNodes(string id, FormDataCollection queryStrings)
{
Inicjalize(id, queryStrings);

var treeNodes = GetFolderTreeNodes();

if (!_isFolderPicker)
{
var fileTreeNodes = GetFileTreeNodes();

treeNodes.AddRange(fileTreeNodes);
}

return treeNodes;
}

private void Inicjalize(string id, FormDataCollection queryStrings)
{
_queryStrings = queryStrings;

string qsStartFolder = _queryStrings.Get("startfolder");
string qsFilter = _queryStrings.Get("filter");

if (string.IsNullOrWhiteSpace(qsStartFolder))
{
_isFolderPicker = true;
_rootVirtualPath = id == "-1" ? "/" : id;
_currentVirtualPath = id == "-1" ? "/" : id;
}
else
{
_rootVirtualPath = qsStartFolder;
_currentVirtualPath = id == "-1" ? qsStartFolder : id;
}

_rootPath = IOHelper.MapPath("~" + _rootVirtualPath);
_currentPath = IOHelper.MapPath("~" + _currentVirtualPath);

_fileExtensionFilters = qsFilter.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(a => a.Trim().EnsureStartsWith(".")).ToArray();
}

private TreeNodeCollection GetFolderTreeNodes()
{
var treeNodes = new TreeNodeCollection();
var dirs = GetAllDirectories(_currentPath);

treeNodes.AddRange(dirs.Select(dir => CreateFolderTreeNode(dir)));

return treeNodes;
}

private TreeNodeCollection GetFileTreeNodes()
{
var treeNodes = new TreeNodeCollection();
var files = GetFiles(_currentPath, _fileExtensionFilters).OrderBy(f => f.Name);

treeNodes.AddRange(files.Select(file => CreateFileTreeNode(file)));

return treeNodes;
}

private TreeNode CreateFolderTreeNode(DirectoryInfo dir)
{
string virtualPath = GetVirtualPath(dir.FullName);
bool hasChildren = dir.EnumerateDirectories().Any() || !_isFolderPicker && GetFiles(dir, _fileExtensionFilters).Any();

return CreateTreeNode(virtualPath, _currentVirtualPath, _queryStrings, dir.Name, "icon-folder", hasChildren);
}

private TreeNode CreateFileTreeNode(FileInfo file)
{
string virtualPath = GetVirtualPath(file.FullName);

return CreateTreeNode(virtualPath, _currentVirtualPath, _queryStrings, file.Name, "icon-document", false);
}

protected override MenuItemCollection GetMenuForNode(string id, FormDataCollection queryStrings)
{
return null;
}

private string GetVirtualPath(string physicalPath)
{
return physicalPath.Replace(_rootPath, _rootVirtualPath).Replace("\\", "/");
}

public IEnumerable<DirectoryInfo> GetAllDirectories(string path)
{
var dir = new DirectoryInfo(path);

return dir.GetDirectories().OrderBy(d => d.Name);
}

public IEnumerable<FileInfo> GetFiles(string path, string[] filter)
{
var dir = new DirectoryInfo(path);

return GetFiles(dir, filter);
}

public IEnumerable<FileInfo> GetFiles(DirectoryInfo dir, string[] filter)
{
var files = dir.EnumerateFiles();

if (filter != null && filter.Any())
return files.Where(f => filter.Contains(f.Extension, StringComparer.OrdinalIgnoreCase));

return files;
}
}
}
70 changes: 0 additions & 70 deletions Src/Our.Umbraco.FilePicker/Controllers/FolderTreeController.cs

This file was deleted.

3 changes: 1 addition & 2 deletions Src/Our.Umbraco.FilePicker/Our.Umbraco.FilePicker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Controllers\FilePickerApiController.cs" />
<Compile Include="Controllers\FolderTreeController.cs" />
<Compile Include="Controllers\FilePickerTreeController.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down