Click or drag to resize

IFolder Interface

IT Hit User File System
Represents a folder in the user file system.

Namespace:  ITHit.FileSystem
Assembly:  ITHit.FileSystem (in ITHit.FileSystem.dll) Version: 8.1.26727.0-Beta2
Syntax
public interface IFolder : IFileSystemItem

The IFolder type exposes the following members.

Methods
  NameDescription
Public methodCreateFileAsync
Creates a new file in this folder in the remote storage.
Public methodCreateFolderAsync
Creates a new folder in the remote storage.
Public methodCode exampleDeleteAsync
Called before a file or a folder is being deleted from the user file system.
(Inherited from IFileSystemItem.)
Public methodGetChildrenAsync
Gets list of files and folders contained in this folder that match a search pattern.
Public methodCode exampleGetPropertiesAsync
Gets list of custom properties.
(Inherited from IFileSystemItem.)
Public methodCode exampleGetThumbnailAsync
Called when thumbnail is requested by the platform.
(Inherited from IFileSystemItem.)
Public methodCode exampleMoveToAsync
Called before a file or a folder is moved to a new location or renamed.
(Inherited from IFileSystemItem.)
Public methodWriteAsync
Updates this folder info in the remote storage.
Top
Remarks
Examples

The code below is part of 'VirtualFileSystem' C# sample provided with the SDK.

C#
public class VirtualFolder : VirtualFileSystemItem, IFolder
{
    public VirtualFolder(IMapping mapping, string path, ILogger logger) : base(mapping, path, logger)
    {

    }

    public async Task<IFileMetadata> CreateFileAsync(IFileMetadata fileMetadata, Stream content = null, IOperationContext operationContext = null, IInSyncResultContext inSyncResultContext = null, CancellationToken cancellationToken = default)
    {
        string userFileSystemNewItemPath = Path.Combine(UserFileSystemPath, fileMetadata.Name);
        Logger.LogMessage($"{nameof(IFolder)}.{nameof(CreateFileAsync)}()", userFileSystemNewItemPath);

        FileInfo remoteStorageNewItem = new FileInfo(Path.Combine(RemoteStoragePath, fileMetadata.Name));

        // Create remote storage file.
        using (FileStream remoteStorageStream = remoteStorageNewItem.Open(FileMode.CreateNew, FileAccess.Write, FileShare.Delete))
        {
            // Upload content. Note that if the file is blocked - content parameter is null.
            if (content != null)
            {
                try
                {
                    await content.CopyToAsync(remoteStorageStream);
                }
                catch (OperationCanceledException)
                {
                    // Operation was canceled by the calling Engine.StopAsync() or the operation timeout occurred.
                    Logger.LogMessage($"{nameof(IFolder)}.{nameof(CreateFileAsync)}() canceled", userFileSystemNewItemPath, default);
                }
                remoteStorageStream.SetLength(content.Length);
            }
        }

        // Update remote storage file metadata.
        remoteStorageNewItem.Attributes = fileMetadata.Attributes & ~FileAttributes.ReadOnly;
        remoteStorageNewItem.CreationTimeUtc = fileMetadata.CreationTime.UtcDateTime;
        remoteStorageNewItem.LastWriteTimeUtc = fileMetadata.LastWriteTime.UtcDateTime;
        remoteStorageNewItem.LastAccessTimeUtc = fileMetadata.LastAccessTime.UtcDateTime;
        remoteStorageNewItem.Attributes = fileMetadata.Attributes;

        // Typically you must return IFileMetadata with a remote storage item ID, content eTag and metadata eTag.
        // The ID will be passed later into IEngine.GetFileSystemItemAsync() method.
        // However, becuse we can not read the ID for the network path we return null.
        return null; 
    }

    public async Task<IFolderMetadata> CreateFolderAsync(IFolderMetadata folderMetadata, IOperationContext operationContext, IInSyncResultContext inSyncResultContext, CancellationToken cancellationToken = default)
    {
        string userFileSystemNewItemPath = Path.Combine(UserFileSystemPath, folderMetadata.Name);
        Logger.LogMessage($"{nameof(IFolder)}.{nameof(CreateFolderAsync)}()", userFileSystemNewItemPath);

        DirectoryInfo remoteStorageNewItem = new DirectoryInfo(Path.Combine(RemoteStoragePath, folderMetadata.Name));
        remoteStorageNewItem.Create();

        // Update remote storage folder metadata.
        remoteStorageNewItem.Attributes = folderMetadata.Attributes & ~FileAttributes.ReadOnly;
        remoteStorageNewItem.CreationTimeUtc = folderMetadata.CreationTime.UtcDateTime;
        remoteStorageNewItem.LastWriteTimeUtc = folderMetadata.LastWriteTime.UtcDateTime;
        remoteStorageNewItem.LastAccessTimeUtc = folderMetadata.LastAccessTime.UtcDateTime;
        remoteStorageNewItem.Attributes = folderMetadata.Attributes;

        // Typically you must return IFileMetadata with a remote storage item ID and metadata eTag.
        // The ID will be passed later into IEngine.GetFileSystemItemAsync() method.
        // However, becuse we can not read the ID for the network path we return null.
        return null;
    }

    public async Task GetChildrenAsync(string pattern, IOperationContext operationContext, IFolderListingResultContext resultContext, CancellationToken cancellationToken)
    {
        // This method has a 60 sec timeout. 
        // To process longer requests and reset the timout timer call one of the following:
        // - resultContext.ReturnChildren() method.
        // - resultContext.ReportProgress() method.

        Logger.LogMessage($"{nameof(IFolder)}.{nameof(GetChildrenAsync)}({pattern})", UserFileSystemPath, default, operationContext);

        List<IFileSystemItemMetadata> children = new List<IFileSystemItemMetadata>();
        IEnumerable<FileSystemInfo> remoteStorageChildren = new DirectoryInfo(RemoteStoragePath).EnumerateFileSystemInfos(pattern);

        foreach (FileSystemInfo remoteStorageItem in remoteStorageChildren)
        {
            IFileSystemItemMetadata itemInfo = Mapping.GetUserFileSysteItemMetadata(remoteStorageItem);
            children.Add(itemInfo);
        }

        // To signal that the children enumeration is completed 
        // always call ReturnChildren(), even if the folder is empty.
        await resultContext.ReturnChildrenAsync(children.ToArray(), children.Count(), true, cancellationToken);
    }

    public async Task<IFolderMetadata> WriteAsync(IFileSystemBasicInfo fileBasicInfo, IOperationContext operationContext, IInSyncResultContext inSyncResultContext, CancellationToken cancellationToken = default)
    {
        Logger.LogMessage($"{nameof(IFolder)}.{nameof(WriteAsync)}()", UserFileSystemPath, default, operationContext);

        DirectoryInfo remoteStorageItem = new DirectoryInfo(RemoteStoragePath);

        // Update remote storage folder metadata.
        if (fileBasicInfo.Attributes.HasValue)
        {
            remoteStorageItem.Attributes = fileBasicInfo.Attributes.Value & ~FileAttributes.ReadOnly;
        }

        if (fileBasicInfo.CreationTime.HasValue)
        {
            remoteStorageItem.CreationTimeUtc = fileBasicInfo.CreationTime.Value.UtcDateTime;
        }

        if (fileBasicInfo.LastWriteTime.HasValue)
        {
            remoteStorageItem.LastWriteTimeUtc = fileBasicInfo.LastWriteTime.Value.UtcDateTime;
        }

        if (fileBasicInfo.LastAccessTime.HasValue)
        {
            remoteStorageItem.LastAccessTimeUtc = fileBasicInfo.LastAccessTime.Value.UtcDateTime;
        }

        if (fileBasicInfo.Attributes.HasValue)
        {
            remoteStorageItem.Attributes = fileBasicInfo.Attributes.Value;
        }

        return null;
    }
}
See Also