11using System ;
22using System . Collections . Generic ;
33using System . IO ;
4- using System . Linq ;
54using System . Threading ;
65using System . Threading . Tasks ;
76using Azure ;
@@ -185,7 +184,7 @@ public override async Task DeleteFileAsync(string virtualPath, CancellationToken
185184 }
186185 }
187186
188- public override async Task < byte [ ] > ReadFileAsync ( string virtualPath , CancellationToken cancellationToken = default )
187+ public override async Task < Stream > ReadFileStreamAsync ( string virtualPath , CancellationToken cancellationToken = default )
189188 {
190189 await GetFileAsync ( virtualPath , cancellationToken ) ;
191190
@@ -198,45 +197,15 @@ public override async Task<byte[]> ReadFileAsync(string virtualPath, Cancellatio
198197 var directory = client . GetDirectoryClient ( directoryPath ) ;
199198 var download = await directory . GetFileClient ( filePath ) . DownloadAsync ( cancellationToken : cancellationToken ) ;
200199
201- using var memoryStream = new MemoryStream ( ) ;
202- await download . Value . Content . CopyToAsync ( memoryStream , 81920 , cancellationToken ) ;
203-
204- return memoryStream . ToArray ( ) ;
205- }
206- catch ( Exception exception )
207- {
208- throw Exception ( exception ) ;
209- }
210- }
211-
212- public override async Task < string > ReadTextFileAsync ( string virtualPath , CancellationToken cancellationToken = default )
213- {
214- await GetFileAsync ( virtualPath , cancellationToken ) ;
215-
216- var path = GetPath ( virtualPath ) ;
217- var filePath = GetLastPathPart ( path ) ;
218- var directoryPath = GetParentPathPart ( path ) ;
219-
220- try
221- {
222- var directory = client . GetDirectoryClient ( directoryPath ) ;
223- var file = directory . GetFileClient ( filePath ) ;
224- var download = await file . DownloadAsync ( cancellationToken : cancellationToken ) ;
225-
226- using var memoryStream = new MemoryStream ( ) ;
227- await download . Value . Content . CopyToAsync ( memoryStream , 81920 , cancellationToken ) ;
228- using var streamReader = new StreamReader ( memoryStream ) ;
229- memoryStream . Position = 0 ;
230-
231- return await streamReader . ReadToEndAsync ( ) ;
200+ return download . Value . Content ;
232201 }
233202 catch ( Exception exception )
234203 {
235204 throw Exception ( exception ) ;
236205 }
237206 }
238207
239- public override async Task WriteFileAsync ( string virtualPath , byte [ ] contents , bool overwrite = false , CancellationToken cancellationToken = default )
208+ public override async Task WriteFileAsync ( string virtualPath , Stream contents , bool overwrite = false , CancellationToken cancellationToken = default )
240209 {
241210 if ( ! overwrite && await FileExistsAsync ( virtualPath , cancellationToken ) )
242211 {
@@ -253,47 +222,18 @@ public override async Task WriteFileAsync(string virtualPath, byte[] contents, b
253222 await directory . CreateIfNotExistsAsync ( cancellationToken : cancellationToken ) ;
254223 var file = directory . GetFileClient ( filePath ) ;
255224
256- using var memoryStream = new MemoryStream ( contents ) ;
257- await file . CreateAsync ( memoryStream . Length , cancellationToken : cancellationToken ) ;
258-
259- await file . UploadRangeAsync ( new HttpRange ( 0 , memoryStream . Length ) , memoryStream , cancellationToken : cancellationToken ) ;
260- }
261- catch ( Exception exception )
262- {
263- throw Exception ( exception ) ;
264- }
265- }
266-
267- public override async Task AppendFileAsync ( string virtualPath , byte [ ] contents , CancellationToken cancellationToken = default )
268- {
269- await GetFileAsync ( virtualPath , cancellationToken ) ;
270- var existingContents = await ReadFileAsync ( virtualPath , cancellationToken ) ;
271-
272- var path = GetPath ( virtualPath ) ;
273- var filePath = GetLastPathPart ( path ) ;
274- var directoryPath = GetParentPathPart ( path ) ;
275-
276- try
277- {
278- var directory = client . GetDirectoryClient ( directoryPath ) ;
279- var file = directory . GetFileClient ( filePath ) ;
280-
281- contents = existingContents . Concat ( contents ) . ToArray ( ) ;
282-
283- using var memoryStream = new MemoryStream ( contents ) ;
284-
285- await file . DeleteAsync ( cancellationToken ) ;
286- await file . CreateAsync ( memoryStream . Length , cancellationToken : cancellationToken ) ;
225+ contents . Seek ( 0 , SeekOrigin . Begin ) ;
287226
288- await file . UploadRangeAsync ( new HttpRange ( 0 , memoryStream . Length ) , memoryStream , cancellationToken : cancellationToken ) ;
227+ await file . CreateAsync ( contents . Length , cancellationToken : cancellationToken ) ;
228+ await file . UploadRangeAsync ( new HttpRange ( 0 , contents . Length ) , contents , cancellationToken : cancellationToken ) ;
289229 }
290230 catch ( Exception exception )
291231 {
292232 throw Exception ( exception ) ;
293233 }
294234 }
295235
296- private static Exception Exception ( Exception exception )
236+ protected override Exception Exception ( Exception exception )
297237 {
298238 if ( exception is FileSystemException )
299239 {
0 commit comments