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: 7.1.23701.0
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(string path, byte[] remoteStorageItemId, ILogger logger) : base(path, remoteStorageItemId, logger)
    {

    }

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

        if (!Mapping.TryGetRemoteStoragePathById(RemoteStorageItemId, out string remoteStoragePath))
        {
            // Possibly parent is deleted in the remote storage.
            Logger.LogMessage($"Can't create. Parent not found", userFileSystemNewItemPath, BitConverter.ToString(RemoteStorageItemId).Replace("-", ""));
            inSyncResultContext.SetInSync = false;
            return null;
        }
        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;

        // Return remote storage item ID. It will be passed later into IEngine.GetFileSystemItemAsync() method.
        return WindowsFileSystemItem.GetItemIdByPath(remoteStorageNewItem.FullName); 
    }

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

        if (!Mapping.TryGetRemoteStoragePathById(RemoteStorageItemId, out string remoteStoragePath))
        {
            // Possibly parent is deleted in the remote storage.
            Logger.LogMessage($"Can't create. Parent not found", userFileSystemNewItemPath, BitConverter.ToString(RemoteStorageItemId).Replace("-", ""));
            inSyncResultContext.SetInSync = false;
            return null;
        }

        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;

        // Return the remote storage item ID. It will be passed later into the IEngine.GetFileSystemItemAsync() method.
        return WindowsFileSystemItem.GetItemIdByPath(remoteStorageNewItem.FullName);
    }

    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> userFileSystemChildren = new List<IFileSystemItemMetadata>();
        if (Mapping.TryGetRemoteStoragePathById(RemoteStorageItemId, out string remoteStoragePath))
        {
            IEnumerable<FileSystemInfo> remoteStorageChildren = new DirectoryInfo(remoteStoragePath).EnumerateFileSystemInfos(pattern);

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

                string userFileSystemItemPath = Path.Combine(UserFileSystemPath, itemInfo.Name);

                // Filtering existing files/folders. This is only required to avoid extra errors in the log.
                if (!FsPath.Exists(userFileSystemItemPath))
                {
                    Logger.LogMessage("Creating", userFileSystemItemPath, null, operationContext);
                    userFileSystemChildren.Add(itemInfo);
                }
            }
        }
        else
        {
            // Possibly folder was deleted in the remote storage.
            Logger.LogError($"Listing failed. Folder not found", UserFileSystemPath, default, null, operationContext); 
        }

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

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

        if (!Mapping.TryGetRemoteStoragePathById(RemoteStorageItemId, out string remoteStoragePath)) return;
        DirectoryInfo remoteStorageItem = new DirectoryInfo(remoteStoragePath);

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