Beispiel #1
0
        /// <inheritdoc/>
        public ValueTask <IFileSystemEntry> GetFileAsync(string name, CancellationToken cancellationToken = default)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return(ValueTaskEx.FromCanceled <IFileSystemEntry>(cancellationToken));
            }

            IFileInfo file = this.fileProvider.GetFileInfo(ToFilePath(name));

            if (!file.Exists)
            {
                return(ValueTaskEx.FromResult <IFileSystemEntry>(null));
            }

            return(ValueTaskEx.FromResult <IFileSystemEntry>(new PhysicalFileSystemEntry(file, this.contentTypeProvider)));
        }
Beispiel #2
0
        /// <inheritdoc/>
        public ValueTask <bool> TryDeleteFileAsync(string name, CancellationToken cancellationToken = default)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return(ValueTaskEx.FromCanceled <bool>(cancellationToken));
            }

            IFileInfo file = this.fileProvider.GetFileInfo(ToFilePath(name));

            if (!file.Exists)
            {
                return(ValueTaskEx.FromResult(false));
            }

            File.Delete(file.PhysicalPath);
            return(ValueTaskEx.FromResult(true));
        }
Beispiel #3
0
        /// <inheritdoc/>
        public async ValueTask PutFileAsync(IFileSystemEntry entry, CancellationToken cancellationToken = default)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                await ValueTaskEx.FromCanceled(cancellationToken);
            }

            string name      = entry.Name;
            string path      = Path.Combine(this.root, ToFilePath(name));
            string directory = Path.GetDirectoryName(path);

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            using Stream stream = await entry.CreateReadStreamAsync();

            using FileStream fileStream = File.Create(path);
            await stream.CopyToAsync(fileStream, cancellationToken);
        }