IFolder Interface |
Namespace: ITHit.FileSystem
The IFolder type exposes the following members.
Name | Description | |
---|---|---|
CreateFileAsync |
Creates a new file in this folder in the remote storage.
| |
CreateFolderAsync |
Creates a new folder in the remote storage.
| |
DeleteAsync |
Called before a file or a folder is being deleted from the user file system.
(Inherited from IFileSystemItem.) | |
GetChildrenAsync |
Gets list of files and folders contained in this folder that match a search pattern.
| |
GetPropertiesAsync |
Gets list of custom properties.
(Inherited from IFileSystemItem.) | |
GetThumbnailAsync |
Called when thumbnail is requested by the platform.
(Inherited from IFileSystemItem.) | |
MoveToAsync |
Called before a file or a folder is moved to a new location or renamed.
(Inherited from IFileSystemItem.) | |
WriteAsync |
Updates this folder info in the remote storage.
|
In addition to metods provided by IFileSystemItem provides GetChildrenAsync(String, IOperationContext, IFolderListingResultContext, CancellationToken) method for enumerating this folder content as well as CreateFileAsync(IFileMetadata, Stream, IOperationContext, IInSyncResultContext, CancellationToken) and CreateFolderAsync(IFolderMetadata, IOperationContext, IInSyncResultContext, CancellationToken) methods for creating new items in this folder.
The code below is part of 'VirtualFileSystem' C# sample provided with the SDK.
public class VirtualFolder : VirtualFileSystemItem, IFolder { public VirtualFolder(IMapping mapping, string path, ILogger logger) : base(mapping, path, logger) { } public async Task<IFileMetadata> CreateFileAsync(IFileMetadata metadata, Stream content = null, IOperationContext operationContext = null, IInSyncResultContext inSyncResultContext = null, CancellationToken cancellationToken = default) { string userFileSystemNewItemPath = Path.Combine(UserFileSystemPath, metadata.Name); Logger.LogMessage($"{nameof(IFolder)}.{nameof(CreateFileAsync)}()", userFileSystemNewItemPath, default, operationContext, metadata); FileInfo remoteStorageNewItem = new FileInfo(Path.Combine(RemoteStoragePath, metadata.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, operationContext, metadata); } remoteStorageStream.SetLength(content.Length); } } // Update remote storage file metadata. remoteStorageNewItem.Attributes = metadata.Attributes.Value & ~FileAttributes.ReadOnly; remoteStorageNewItem.CreationTimeUtc = metadata.CreationTime.Value.UtcDateTime; remoteStorageNewItem.LastWriteTimeUtc = metadata.LastWriteTime.Value.UtcDateTime; remoteStorageNewItem.LastAccessTimeUtc = metadata.LastAccessTime.Value.UtcDateTime; remoteStorageNewItem.Attributes = metadata.Attributes.Value; // 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 metadata, IOperationContext operationContext, IInSyncResultContext inSyncResultContext, CancellationToken cancellationToken = default) { string userFileSystemNewItemPath = Path.Combine(UserFileSystemPath, metadata.Name); Logger.LogMessage($"{nameof(IFolder)}.{nameof(CreateFolderAsync)}()", userFileSystemNewItemPath, default, operationContext, metadata); DirectoryInfo remoteStorageNewItem = new DirectoryInfo(Path.Combine(RemoteStoragePath, metadata.Name)); remoteStorageNewItem.Create(); // Update remote storage folder metadata. remoteStorageNewItem.Attributes = metadata.Attributes.Value & ~FileAttributes.ReadOnly; remoteStorageNewItem.CreationTimeUtc = metadata.CreationTime.Value.UtcDateTime; remoteStorageNewItem.LastWriteTimeUtc = metadata.LastWriteTime.Value.UtcDateTime; remoteStorageNewItem.LastAccessTimeUtc = metadata.LastAccessTime.Value.UtcDateTime; remoteStorageNewItem.Attributes = metadata.Attributes.Value; // 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 timeout 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(IFolderMetadata metadata, IOperationContext operationContext, IInSyncResultContext inSyncResultContext, CancellationToken cancellationToken = default) { Logger.LogMessage($"{nameof(IFolder)}.{nameof(WriteAsync)}()", UserFileSystemPath, default, operationContext, metadata); DirectoryInfo remoteStorageItem = new DirectoryInfo(RemoteStoragePath); // Update remote storage folder metadata. if (metadata.Attributes.HasValue) { remoteStorageItem.Attributes = metadata.Attributes.Value & ~FileAttributes.ReadOnly; } if (metadata.CreationTime.HasValue) { remoteStorageItem.CreationTimeUtc = metadata.CreationTime.Value.UtcDateTime; } if (metadata.LastWriteTime.HasValue) { remoteStorageItem.LastWriteTimeUtc = metadata.LastWriteTime.Value.UtcDateTime; } if (metadata.LastAccessTime.HasValue) { remoteStorageItem.LastAccessTimeUtc = metadata.LastAccessTime.Value.UtcDateTime; } if (metadata.Attributes.HasValue) { remoteStorageItem.Attributes = metadata.Attributes.Value; } return null; } }