@@ -95,17 +95,49 @@ public static async Task<bool> UploadDirectoryAsync(Project project, string dire
95
95
96
96
var files = Directory . GetFiles ( directory , "*" , SearchOption . AllDirectories ) ;
97
97
var api = new ArtifactsApi ( ) ;
98
-
99
98
100
- var tasks = files . Select ( _ => UploadArtifactAsync ( api , project , _ , _ . Replace ( directory , "" ) ) ) . ToList ( ) ;
101
99
var total = files . Count ( ) ;
102
-
103
100
LogHelper . LogInfo ( $ "Uploading { total } assets for project { project . Name } ") ;
104
101
105
-
102
+ var finished = 0 ;
106
103
var finishedPercent = 0 ;
107
104
reportProgressAction ? . Invoke ( finishedPercent ) ;
108
105
106
+ var chunkSize = 20 ;
107
+ var subLists = files . Select ( ( x , i ) => new { Index = i , Value = x } )
108
+ . GroupBy ( x => x . Index / chunkSize )
109
+ . Select ( x => x . Select ( v => v . Value ) . ToList ( ) )
110
+ . ToList ( ) ;
111
+
112
+ Action < int > reportPercent = ( c ) =>
113
+ {
114
+ var p = finished + c ;
115
+ finishedPercent = ( int ) ( p * 100 / total ) ;
116
+ reportProgressAction ? . Invoke ( finishedPercent ) ;
117
+ } ;
118
+
119
+ foreach ( var chunk in subLists )
120
+ {
121
+ var chunkFiles = chunk . Select ( _ => ( _ , _ . Replace ( directory , "" ) ) ) . ToList ( ) ;
122
+ await BatchExecute ( api , project , chunkFiles , reportPercent , cancellationToken ) ;
123
+ // auto refresh token between each chunk run
124
+ api . Configuration . TokenRepo ? . CheckToken ( ) ;
125
+ finished += chunk . Count ;
126
+ }
127
+
128
+ LogHelper . LogInfo ( $ "Finished uploading assets for project { project . Name } ") ;
129
+
130
+ // canceled by user
131
+ if ( cancellationToken . IsCancellationRequested ) return false ;
132
+
133
+ return true ;
134
+ }
135
+
136
+ private static async Task < bool > BatchExecute ( ArtifactsApi api , Project project , List < ( string path , string relativePath ) > files , Action < int > finishedCountProgressAction = default , CancellationToken cancellationToken = default )
137
+ {
138
+ var tasks = files . Select ( _=> UploadArtifactAsync ( api , project , _ . path , _ . relativePath ) ) . ToList ( ) ;
139
+
140
+ var finished = 0 ;
109
141
while ( tasks . Count ( ) > 0 )
110
142
{
111
143
// canceled by user
@@ -116,25 +148,21 @@ public static async Task<bool> UploadDirectoryAsync(Project project, string dire
116
148
}
117
149
118
150
var finishedTask = await Task . WhenAny ( tasks ) ;
151
+ tasks . Remove ( finishedTask ) ;
119
152
120
153
if ( finishedTask . IsFaulted || finishedTask . Exception != null )
121
154
{
122
155
LogHelper . LogError ( $ "Upload exception: { finishedTask . Exception } ") ;
123
156
throw finishedTask . Exception ;
124
157
}
125
158
126
- tasks . Remove ( finishedTask ) ;
127
-
128
- var unfinishedUploadTasksCount = tasks . Count ( ) ;
129
- finishedPercent = ( int ) ( ( total - unfinishedUploadTasksCount ) / ( double ) total * 100 ) ;
130
- reportProgressAction ? . Invoke ( finishedPercent ) ;
159
+ finished ++ ;
160
+ finishedCountProgressAction ? . Invoke ( finished ) ;
131
161
132
162
}
133
- LogHelper . LogInfo ( $ "Finished uploading assets for project { project . Name } ") ;
134
163
135
164
// canceled by user
136
165
if ( cancellationToken . IsCancellationRequested ) return false ;
137
-
138
166
return true ;
139
167
}
140
168
@@ -151,6 +179,7 @@ public static async Task<bool> UploadArtifactAsync(ArtifactsApi api, Project pro
151
179
if ( fileRelativePath . StartsWith ( "/" ) )
152
180
fileRelativePath = fileRelativePath . Substring ( 1 ) ;
153
181
182
+
154
183
var artf = await api . CreateArtifactAsync ( project . Owner . Name , project . Name , new KeyRequest ( fileRelativePath ) ) ;
155
184
156
185
//Use RestSharp
@@ -171,7 +200,7 @@ public static async Task<bool> UploadArtifactAsync(ArtifactsApi api, Project pro
171
200
172
201
restRequest . AddFile ( "file" , filePath ) ;
173
202
174
- LogHelper . LogInfo ( $ "Started upload of { relativePath } ") ;
203
+ LogHelper . LogInfo ( $ "Started upload of { fileRelativePath } ") ;
175
204
var response = await restClient . ExecuteAsync ( restRequest ) ;
176
205
177
206
if ( response . StatusCode == HttpStatusCode . NoContent )
0 commit comments