@@ -14,6 +14,7 @@ namespace ManagedCode.Storage.FileSystem;
1414public class FileSystemStorage : IFileSystemStorage
1515{
1616 private readonly string _path ;
17+ private readonly Dictionary < string , FileStream > _lockedFiles = new ( ) ;
1718
1819 public FileSystemStorage ( FileSystemStorageOptions fileSystemStorageOptions )
1920 {
@@ -157,7 +158,8 @@ public async IAsyncEnumerable<bool> ExistsAsync(IEnumerable<BlobMetadata> blobs,
157158 {
158159 Name = fileInfo . Name ,
159160 Uri = new Uri ( Path . Combine ( _path , blobName ) ) ,
160- ContentType = MimeHelper . GetMimeType ( fileInfo . Extension )
161+ ContentType = MimeHelper . GetMimeType ( fileInfo . Extension ) ,
162+ Length = fileInfo . Length
161163 } ;
162164
163165 return Task . FromResult < BlobMetadata ? > ( result ) ;
@@ -280,4 +282,36 @@ public async Task CreateContainerAsync(CancellationToken cancellationToken = def
280282 }
281283
282284 #endregion
285+
286+ public async Task SetLegalHold ( string blobName , bool hasLegalHold , CancellationToken cancellationToken = default )
287+ {
288+ if ( hasLegalHold && ! _lockedFiles . ContainsKey ( blobName ) )
289+ {
290+ var file = await DownloadAsync ( blobName , cancellationToken ) ;
291+
292+ if ( file is null ) return ;
293+
294+ var fileStream = File . OpenRead ( file . FilePath ) ; // Opening with FileAccess.Read only
295+ fileStream . Lock ( 0 , fileStream . Length ) ; // Attempting to lock a region of the read-only file
296+
297+ _lockedFiles . Add ( blobName , fileStream ) ;
298+
299+ return ;
300+ }
301+
302+ if ( ! hasLegalHold )
303+ {
304+ if ( _lockedFiles . ContainsKey ( blobName ) )
305+ {
306+ _lockedFiles [ blobName ] . Unlock ( 0 , _lockedFiles [ blobName ] . Length ) ;
307+ _lockedFiles [ blobName ] . Dispose ( ) ;
308+ _lockedFiles . Remove ( blobName ) ;
309+ }
310+ }
311+ }
312+
313+ public Task < bool > HasLegalHold ( string blobName , CancellationToken cancellationToken = default )
314+ {
315+ return Task . FromResult ( _lockedFiles . ContainsKey ( blobName ) ) ;
316+ }
283317}
0 commit comments