2
2
// Licensed under the MIT License. See the LICENSE.
3
3
4
4
using Files . Core . Storage ;
5
+ using Files . Core . Storage . DirectStorage ;
5
6
using Files . Core . Storage . Enums ;
7
+ using Files . Core . Storage . ExtendableStorage ;
6
8
using Files . Core . Storage . Extensions ;
7
9
using Files . Core . Storage . LocatableStorage ;
8
10
using Files . Core . Storage . ModifiableStorage ;
11
+ using Files . Core . Storage . NestedStorage ;
9
12
using Files . Shared . Helpers ;
10
13
using FluentFTP ;
11
14
using System ;
17
20
18
21
namespace Files . App . Storage . FtpStorage
19
22
{
20
- public sealed class FtpStorageFolder : FtpStorable , ILocatableFolder , IModifiableFolder
23
+ public sealed class FtpStorageFolder : FtpStorable , ILocatableFolder , IModifiableFolder , IFolderExtended , INestedFolder , IDirectCopy , IDirectMove
21
24
{
22
- public FtpStorageFolder ( string path , string name )
23
- : base ( path , name )
25
+ public FtpStorageFolder ( string path , string name , IFolder ? parent )
26
+ : base ( path , name , parent )
24
27
{
25
28
}
26
29
27
30
/// <inheritdoc/>
28
- public async Task < IFile > GetFileAsync ( string fileName , CancellationToken cancellationToken = default )
31
+ public async Task < INestedFile > GetFileAsync ( string fileName , CancellationToken cancellationToken = default )
29
32
{
30
33
using var ftpClient = GetFtpClient ( ) ;
31
34
await ftpClient . EnsureConnectedAsync ( cancellationToken ) ;
@@ -36,11 +39,11 @@ public async Task<IFile> GetFileAsync(string fileName, CancellationToken cancell
36
39
if ( item is null || item . Type != FtpObjectType . File )
37
40
throw new FileNotFoundException ( ) ;
38
41
39
- return new FtpStorageFile ( path , item . Name ) ;
42
+ return new FtpStorageFile ( path , item . Name , this ) ;
40
43
}
41
44
42
45
/// <inheritdoc/>
43
- public async Task < IFolder > GetFolderAsync ( string folderName , CancellationToken cancellationToken = default )
46
+ public async Task < INestedFolder > GetFolderAsync ( string folderName , CancellationToken cancellationToken = default )
44
47
{
45
48
using var ftpClient = GetFtpClient ( ) ;
46
49
await ftpClient . EnsureConnectedAsync ( cancellationToken ) ;
@@ -51,11 +54,11 @@ public async Task<IFolder> GetFolderAsync(string folderName, CancellationToken c
51
54
if ( item is null || item . Type != FtpObjectType . Directory )
52
55
throw new DirectoryNotFoundException ( ) ;
53
56
54
- return new FtpStorageFolder ( path , item . Name ) ;
57
+ return new FtpStorageFolder ( path , item . Name , this ) ;
55
58
}
56
59
57
60
/// <inheritdoc/>
58
- public async IAsyncEnumerable < IStorable > GetItemsAsync ( StorableKind kind = StorableKind . All , [ EnumeratorCancellation ] CancellationToken cancellationToken = default )
61
+ public async IAsyncEnumerable < INestedStorable > GetItemsAsync ( StorableKind kind = StorableKind . All , [ EnumeratorCancellation ] CancellationToken cancellationToken = default )
59
62
{
60
63
using var ftpClient = GetFtpClient ( ) ;
61
64
await ftpClient . EnsureConnectedAsync ( cancellationToken ) ;
@@ -65,32 +68,32 @@ public async IAsyncEnumerable<IStorable> GetItemsAsync(StorableKind kind = Stora
65
68
foreach ( var item in await ftpClient . GetListing ( Path , cancellationToken ) )
66
69
{
67
70
if ( item . Type == FtpObjectType . File )
68
- yield return new FtpStorageFile ( item . FullName , item . Name ) ;
71
+ yield return new FtpStorageFile ( item . FullName , item . Name , this ) ;
69
72
}
70
73
}
71
74
else if ( kind == StorableKind . Folders )
72
75
{
73
76
foreach ( var item in await ftpClient . GetListing ( Path , cancellationToken ) )
74
77
{
75
78
if ( item . Type == FtpObjectType . Directory )
76
- yield return new FtpStorageFolder ( item . FullName , item . Name ) ;
79
+ yield return new FtpStorageFolder ( item . FullName , item . Name , this ) ;
77
80
}
78
81
}
79
82
else
80
83
{
81
84
foreach ( var item in await ftpClient . GetListing ( Path , cancellationToken ) )
82
85
{
83
86
if ( item . Type == FtpObjectType . File )
84
- yield return new FtpStorageFile ( item . FullName , item . Name ) ;
87
+ yield return new FtpStorageFile ( item . FullName , item . Name , this ) ;
85
88
86
89
if ( item . Type == FtpObjectType . Directory )
87
- yield return new FtpStorageFolder ( item . FullName , item . Name ) ;
90
+ yield return new FtpStorageFolder ( item . FullName , item . Name , this ) ;
88
91
}
89
92
}
90
93
}
91
94
92
95
/// <inheritdoc/>
93
- public async Task DeleteAsync ( IStorable item , bool permanently = false , CancellationToken cancellationToken = default )
96
+ public async Task DeleteAsync ( INestedStorable item , bool permanently = false , CancellationToken cancellationToken = default )
94
97
{
95
98
using var ftpClient = GetFtpClient ( ) ;
96
99
await ftpClient . EnsureConnectedAsync ( cancellationToken ) ;
@@ -110,11 +113,11 @@ public async Task DeleteAsync(IStorable item, bool permanently = false, Cancella
110
113
}
111
114
112
115
/// <inheritdoc/>
113
- public async Task < IStorable > CreateCopyOfAsync ( IStorable itemToCopy , CreationCollisionOption collisionOption = default , CancellationToken cancellationToken = default )
116
+ public async Task < INestedStorable > CreateCopyOfAsync ( INestedStorable itemToCopy , bool overwrite = default , CancellationToken cancellationToken = default )
114
117
{
115
118
if ( itemToCopy is IFile sourceFile )
116
119
{
117
- var copiedFile = await CreateFileAsync ( itemToCopy . Name , collisionOption , cancellationToken ) ;
120
+ var copiedFile = await CreateFileAsync ( itemToCopy . Name , overwrite , cancellationToken ) ;
118
121
await sourceFile . CopyContentsToAsync ( copiedFile , cancellationToken ) ;
119
122
120
123
return copiedFile ;
@@ -126,41 +129,34 @@ public async Task<IStorable> CreateCopyOfAsync(IStorable itemToCopy, CreationCol
126
129
}
127
130
128
131
/// <inheritdoc/>
129
- public async Task < IStorable > MoveFromAsync ( IStorable itemToMove , IModifiableFolder source , CreationCollisionOption collisionOption = default , CancellationToken cancellationToken = default )
132
+ public async Task < INestedStorable > MoveFromAsync ( INestedStorable itemToMove , IModifiableFolder source , bool overwrite = default , CancellationToken cancellationToken = default )
130
133
{
131
134
using var ftpClient = GetFtpClient ( ) ;
132
135
await ftpClient . EnsureConnectedAsync ( cancellationToken ) ;
133
136
134
- var newItem = await CreateCopyOfAsync ( itemToMove , collisionOption , cancellationToken ) ;
137
+ var newItem = await CreateCopyOfAsync ( itemToMove , overwrite , cancellationToken ) ;
135
138
await source . DeleteAsync ( itemToMove , true , cancellationToken ) ;
136
139
137
140
return newItem ;
138
141
}
139
142
140
143
/// <inheritdoc/>
141
- public async Task < IFile > CreateFileAsync ( string desiredName , CreationCollisionOption collisionOption = default , CancellationToken cancellationToken = default )
144
+ public async Task < INestedFile > CreateFileAsync ( string desiredName , bool overwrite = default , CancellationToken cancellationToken = default )
142
145
{
143
146
using var ftpClient = GetFtpClient ( ) ;
144
147
await ftpClient . EnsureConnectedAsync ( cancellationToken ) ;
145
148
146
149
var newPath = $ "{ Path } /{ desiredName } ";
147
- if ( await ftpClient . FileExists ( newPath , cancellationToken ) )
148
- {
149
- if ( collisionOption == CreationCollisionOption . FailIfExists )
150
- throw new IOException ( "File already exists." ) ;
151
-
152
- if ( collisionOption == CreationCollisionOption . OpenIfExists )
153
- return new FtpStorageFile ( newPath , desiredName ) ;
154
- }
150
+ if ( overwrite && await ftpClient . FileExists ( newPath , cancellationToken ) )
151
+ throw new IOException ( "File already exists." ) ;
155
152
156
153
using var stream = new MemoryStream ( ) ;
157
- var replaceExisting = collisionOption == CreationCollisionOption . ReplaceExisting ;
158
- var result = await ftpClient . UploadStream ( stream , newPath , replaceExisting ? FtpRemoteExists . Overwrite : FtpRemoteExists . Skip , token : cancellationToken ) ;
154
+ var result = await ftpClient . UploadStream ( stream , newPath , overwrite ? FtpRemoteExists . Overwrite : FtpRemoteExists . Skip , token : cancellationToken ) ;
159
155
160
156
if ( result == FtpStatus . Success )
161
157
{
162
158
// Success
163
- return new FtpStorageFile ( newPath , desiredName ) ;
159
+ return new FtpStorageFile ( newPath , desiredName , this ) ;
164
160
}
165
161
else if ( result == FtpStatus . Skipped )
166
162
{
@@ -175,27 +171,20 @@ public async Task<IFile> CreateFileAsync(string desiredName, CreationCollisionOp
175
171
}
176
172
177
173
/// <inheritdoc/>
178
- public async Task < IFolder > CreateFolderAsync ( string desiredName , CreationCollisionOption collisionOption = default , CancellationToken cancellationToken = default )
174
+ public async Task < INestedFolder > CreateFolderAsync ( string desiredName , bool overwrite = default , CancellationToken cancellationToken = default )
179
175
{
180
176
using var ftpClient = GetFtpClient ( ) ;
181
177
await ftpClient . EnsureConnectedAsync ( cancellationToken ) ;
182
178
183
179
var newPath = $ "{ Path } /{ desiredName } ";
184
- if ( await ftpClient . DirectoryExists ( newPath , cancellationToken ) )
185
- {
186
- if ( collisionOption == CreationCollisionOption . FailIfExists )
187
- throw new IOException ( "Directory already exists." ) ;
188
-
189
- if ( collisionOption == CreationCollisionOption . OpenIfExists )
190
- return new FtpStorageFolder ( newPath , desiredName ) ;
191
- }
180
+ if ( overwrite && await ftpClient . DirectoryExists ( newPath , cancellationToken ) )
181
+ throw new IOException ( "Directory already exists." ) ;
192
182
193
- var replaceExisting = collisionOption == CreationCollisionOption . ReplaceExisting ;
194
- var isSuccessful = await ftpClient . CreateDirectory ( newPath , replaceExisting , cancellationToken ) ;
183
+ var isSuccessful = await ftpClient . CreateDirectory ( newPath , overwrite , cancellationToken ) ;
195
184
if ( ! isSuccessful )
196
185
throw new IOException ( "Directory was not successfully created." ) ;
197
186
198
- return new FtpStorageFolder ( newPath , desiredName ) ;
187
+ return new FtpStorageFolder ( newPath , desiredName , this ) ;
199
188
}
200
189
}
201
190
}
0 commit comments