Skip to content

Commit 389a8b3

Browse files
fix: go-ipfs 0.4.14 adding multiple files
1 parent 864c8e7 commit 389a8b3

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

src/CoreApi/FileSystemApi.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ internal FileSystemApi(IpfsClient ipfs)
9191
{
9292
if (options == null)
9393
options = new AddFileOptions();
94-
options.Wrap = false;
95-
96-
// Add the files and sub-directories.
94+
options.Wrap = false;
95+
96+
// Add the files and sub-directories.
9797
path = Path.GetFullPath(path);
9898
var files = Directory
9999
.EnumerateFiles(path)
@@ -104,11 +104,22 @@ internal FileSystemApi(IpfsClient ipfs)
104104
.EnumerateDirectories(path)
105105
.Select(dir => AddDirectoryAsync(dir, recursive, options, cancel));
106106
files = files.Union(folders);
107+
}
108+
109+
// go-ipfs v0.4.14 sometimes fails when sending lots of 'add file'
110+
// requests. It's happy with adding one file at a time.
111+
#if true
112+
var links = new List<IFileSystemLink>();
113+
foreach (var file in files)
114+
{
115+
var node = await file;
116+
links.Add(node.ToLink());
107117
}
108-
var nodes = await Task.WhenAll(files);
109-
110-
// Create the directory with links to the created files and sub-directories
118+
#else
119+
var nodes = await Task.WhenAll(files);
111120
var links = nodes.Select(node => node.ToLink());
121+
#endif
122+
// Create the directory with links to the created files and sub-directories
112123
var folder = emptyFolder.Value.AddLinks(links);
113124
var directory = await ipfs.Object.PutAsync(folder, cancel);
114125

test/CoreApi/FileSystemApiTest.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using System.Linq;
77
using System.Text;
8+
using System.Threading;
89
using System.Threading.Tasks;
910

1011
namespace Ipfs.Api
@@ -184,7 +185,7 @@ public void AddDirectory()
184185
}
185186
finally
186187
{
187-
Directory.Delete(temp, true);
188+
DeleteTemp(temp);
188189
}
189190
}
190191

@@ -227,7 +228,24 @@ public void AddDirectoryRecursive()
227228
}
228229
finally
229230
{
230-
Directory.Delete(temp, true);
231+
DeleteTemp(temp);
232+
}
233+
}
234+
235+
void DeleteTemp(string temp)
236+
{
237+
while (true)
238+
{
239+
try
240+
{
241+
Directory.Delete(temp, true);
242+
break;
243+
}
244+
catch (Exception)
245+
{
246+
Thread.Sleep(1);
247+
continue; // most likely anti-virus is reading a file
248+
}
231249
}
232250
}
233251

0 commit comments

Comments
 (0)