//******************************************************************************************************
// DirectoryBrowserOperations.cs - Gbtc
//
// Copyright © 2016, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
// file except in compliance with the License. You may obtain a copy of the License at:
//
// http://opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 08/08/2016 - J. Ritchie Carroll
// Generated original version of source code.
//
//******************************************************************************************************
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using GSF.IO;
namespace GSF.Web.Model.HubOperations
{
///
/// Defines an interface for to make sure all needed methods
/// are implemented in a hub when using the DirectoryBrowserOperations.cshtml view.
///
public interface IDirectoryBrowserOperations
{
///
/// Retrieves list of directories from specified .
///
/// Folder to load directories from.
/// true to show hidden directories; otherwise, false.
/// List of directories from specified .
IEnumerable LoadDirectories(string rootFolder, bool showHidden);
///
/// Determines if the specified is a logical drive, e.g., C:\.
///
/// Path to test.
/// true if the specified is a logical drive; otherwise, false.
bool IsLogicalDrive(string path);
///
/// Resolves the specified to an absolute full path and expands any environmental variables.
///
/// Path to resolve.
///
string ResolvePath(string path);
///
/// Combines the specified and strings into a single path.
///
/// The first path to combine.
/// The second path to combine.
/// The combined paths.
string CombinePath(string path1, string path2);
///
/// Creates all directories and subdirectories in the specified unless they already exist.
///
/// The directory to create.
void CreatePath(string path);
}
///
/// Represents hub based operations for the DirectoryBrowserOperations.cshtml view.
///
public static class DirectoryBrowserOperations
{
///
/// Retrieves list of directories from specified .
///
/// Folder to load directories from.
/// true to show hidden directories; otherwise, false.
/// List of directories from specified .
public static IEnumerable LoadDirectories(string rootFolder, bool showHidden)
{
if (string.IsNullOrWhiteSpace(rootFolder))
return Directory.GetLogicalDrives();
IEnumerable directories = Directory.GetDirectories(rootFolder);
if (!showHidden)
directories = directories.Where(path => !new DirectoryInfo(path).Attributes.HasFlag(FileAttributes.Hidden));
return new[] { "..\\" }.Concat(directories.Select(path => FilePath.AddPathSuffix(FilePath.GetLastDirectoryName(path))));
}
///
/// Determines if the specified is a logical drive, e.g., C:\.
///
/// Path to test.
/// true if the specified is a logical drive; otherwise, false.
public static bool IsLogicalDrive(string path)
{
if (string.IsNullOrWhiteSpace(path))
return false;
DirectoryInfo info = new DirectoryInfo(path);
return info.FullName == info.Root.FullName;
}
///
/// Resolves the specified to an absolute full path and expands any environmental variables.
///
/// Path to resolve.
///
public static string ResolvePath(string path)
{
if (IsLogicalDrive(path) && Path.GetFullPath(path) == path)
return path;
return Path.GetFullPath(FilePath.GetAbsolutePath(Environment.ExpandEnvironmentVariables(path)));
}
///
/// Combines the specified and strings into a single path.
///
/// The first path to combine.
/// The second path to combine.
/// The combined paths.
public static string CombinePath(string path1, string path2)
{
return Path.Combine(path1, path2);
}
///
/// Creates all directories and subdirectories in the specified unless they already exist.
///
/// The directory to create.
public static void CreatePath(string path)
{
Directory.CreateDirectory(path);
}
}
}