forked from ITHit/UserFileSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualFile.cs
More file actions
89 lines (78 loc) · 3.82 KB
/
VirtualFile.cs
File metadata and controls
89 lines (78 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using ITHit.FileSystem;
using ITHit.FileSystem.Samples.Common;
namespace VirtualFileSystem
{
/// <summary>
/// Represents a file in the remote storage.
/// </summary>
/// <remarks>You will change methods of this class to read/write data from/to your remote storage.</remarks>
internal class VirtualFile : VirtualFileSystemItem, IVirtualFile
{
/// <summary>
/// Creates instance of this class.
/// </summary>
/// <param name="userFileSystemFilePath">Path of this file in the user file system.</param>
/// <param name="virtualDrive">Virtual Drive instance that created this item.</param>
/// <param name="logger">Logger.</param>
public VirtualFile(string userFileSystemFilePath, VirtualDrive virtualDrive, ILogger logger)
: base(userFileSystemFilePath, virtualDrive, logger)
{
}
/// <summary>
/// Reads this file content from the remote storage.
/// </summary>
/// <param name="offset">File content offset, in bytes, to start reading from.</param>
/// <param name="length">Data length to read, in bytes.</param>
/// <param name="fileSize">Total file size, in bytes.</param>
/// <param name="resultContext">
/// You will use this parameter to return file content by
/// calling <see cref="ITransferDataResultContext.ReturnData(byte[], long, long)"/>
/// </param>
public async Task ReadAsync(long offset, long length, long fileSize, ITransferDataResultContext resultContext)
{
// On Windows this method has a 60 sec timeout.
// To process longer requests and reset the timout timer call the resultContext.ReportProgress() or resultContext.ReturnData() method.
await using (FileStream stream = File.OpenRead(RemoteStoragePath))
{
const long MAX_CHUNK_SIZE = 0x500000; //5Mb
long chunkSize = Math.Min(MAX_CHUNK_SIZE, length);
stream.Seek(offset, SeekOrigin.Begin);
long total = offset + length;
byte[] buffer = new byte[chunkSize];
long bytesRead;
while ( (bytesRead = await stream.ReadAsync(buffer, 0, (int)chunkSize) ) > 0)
{
resultContext.ReturnData(buffer, offset, bytesRead);
offset += bytesRead;
length -= bytesRead;
chunkSize = Math.Min(MAX_CHUNK_SIZE, length);
if (offset >= total)
{
return;
}
}
}
}
public async Task<bool> ValidateDataAsync(long offset, long length)
{
return true;
}
/// <summary>
/// Updates file in the remote storage.
/// </summary>
/// <param name="fileInfo">New information about the file, such as creation date, modification date, attributes, etc.</param>
/// <param name="content">New file content or null if the file content is not modified.</param>
/// <param name="eTagOld">The ETag to be sent to the remote storage as part of the update request to make sure the content is not overwritten.</param>
/// <param name="lockInfo">Information about the lock. Caller passes null if the item is not locked.</param>
/// <returns>The new ETag returned from the remote storage.</returns>
public async Task<string> UpdateAsync(IFileMetadata fileInfo, Stream content = null, string eTagOld = null, ServerLockInfo lockInfo = null)
{
return await CreateOrUpdateFileAsync(RemoteStoragePath, fileInfo, FileMode.Open, content, eTagOld, lockInfo);
}
}
}