Skip to content

Latest commit

 

History

History
94 lines (75 loc) · 2.64 KB

api_file.md

File metadata and controls

94 lines (75 loc) · 2.64 KB

FileSystem API

General Usage

Get Files

IFile file = await fileSystem.GetAsync("path/to/file.txt");
IFile file = await fileSystem.GetAsync("path/to/file.txt", create:true); 
IFile file = await fileSystem.GetAsync("path/to/file.txt", create:true, cancellationToken: cancel);

// create 
//    if `true` : Create file if not exists
//    else : Throw if not exists

Write Files

await file.WriteAsync(byteArray);
await file.WriteAsync(stream);

// overwrite
await file.WriteAsync(byteArray, overwrite:true);
await file.WriteAsync(stream,    overwrite:true);

// overwrite and/or cancellationToken
await file.WriteAsync(byteArray, overwrite:true, cancellationToken: cancel);
await file.WriteAsync(stream,    overwrite:true, cancellationToken: cancel);

Read Files

// Read to byte array
byte[] byteArray = await file.ReadAllBytesAsync();
byte[] byteArray = await file.ReadAllBytesAsync(cancellationToken: cancel);

//Read to stream
await file.ReadToStreamAsync(stream);
await file.ReadToStreamAsync(stream, cancellationToken: cancel);

Check if a file exists

bool exists = await file.ExistsAsync();
bool exists = await file.ExistsAsync(cancellationToken: cancel);

NOTE: This only has consistent behaviour for files, not directories. Directories are less important in Flysystem, they're created implicitly and often ignored because not every adapter (filesystem type) supports directories.

Delete Files

await file.DeleteAsync();
await file.DeleteAsync(cancellationToken: cancel);

Rename Files

await file.RenameAsync("newname.txt");
await file.RenameAsync("newname.txt", cancellationToken: cancel);

Copy Files

//TODO IFile file = await file.CopyAsync("copy.txt");
//TODO IFile file = await file.CopyAsync("copy.txt", cancellationToken: cancel);

IFile otherFile = await fileSystem.GetAsync("path/to/other/file.txt");

await file.CopyToAsync(otherFile);
await file.CopyToAsync(otherFile, cancellationToken: cancel);

Get Mimetypes

string mimeType = await file.GetMimeTypeAsync();
string mimeType = await file.GetMimeTypeAsync(cancellationToken: cancel);

Get Timestamps

DateTime date = await file.GetLastModificationDateAsync();
DateTime date = await file.GetLastModificationDateAsync(cancellationToken: cancel);

Get File sizes (in bytes)

long sizeInBytes = await file.GetSizeAsync();
long sizeInBytes = await file.GetSizeAsync(cancellationToken: cancel);

Get Checksum

string checksum = await file.GetChecksumAsync();
string checksum = await file.GetChecksumAsync(cancellationToken: cancel);