@@ -141,9 +141,7 @@ public virtual Task SendFileAsync(IFileInfo file)
141
141
{
142
142
using ( var readStream = file . CreateReadStream ( ) )
143
143
{
144
- readStream . Seek ( offset , SeekOrigin . Begin ) ; // TODO: What if !CanSeek?
145
-
146
- await StreamCopyOperation . CopyToAsync ( readStream , Body , count , cancellationToken ) ;
144
+ await SendFileAsync ( Body , readStream , offset , count , cancellationToken ) ;
147
145
return ;
148
146
}
149
147
}
@@ -159,23 +157,8 @@ public virtual Task SendFileAsync(IFileInfo file)
159
157
await sendFile . SendFileAsync ( file . PhysicalPath , offset , count , cancellationToken ) ;
160
158
}
161
159
162
- // Not safe for overlapped writes.
163
- private static async Task SendFileAsync ( Stream outputStream , string fileName , long offset , long ? length , CancellationToken cancellationToken )
160
+ private static async Task SendFileAsync ( Stream outputStream , string fileName , long offset , long ? count , CancellationToken cancellationToken )
164
161
{
165
- cancellationToken . ThrowIfCancellationRequested ( ) ;
166
-
167
- var fileInfo = new FileInfo ( fileName ) ;
168
- if ( offset < 0 || offset > fileInfo . Length )
169
- {
170
- throw new ArgumentOutOfRangeException ( nameof ( offset ) , offset , string . Empty ) ;
171
- }
172
-
173
- if ( length . HasValue &&
174
- ( length . Value < 0 || length . Value > fileInfo . Length - offset ) )
175
- {
176
- throw new ArgumentOutOfRangeException ( nameof ( length ) , length , string . Empty ) ;
177
- }
178
-
179
162
int bufferSize = 1024 * 16 ;
180
163
181
164
var fileStream = new FileStream (
@@ -188,10 +171,29 @@ private static async Task SendFileAsync(Stream outputStream, string fileName, lo
188
171
189
172
using ( fileStream )
190
173
{
191
- fileStream . Seek ( offset , SeekOrigin . Begin ) ;
174
+ await SendFileAsync ( outputStream , fileStream , offset , count , cancellationToken ) ;
175
+ }
176
+ }
177
+
178
+ // Not safe for overlapped writes.
179
+ private static async Task SendFileAsync ( Stream outputStream , Stream readStream , long offset , long ? length , CancellationToken cancellationToken )
180
+ {
181
+ cancellationToken . ThrowIfCancellationRequested ( ) ;
182
+
183
+ if ( offset < 0 || offset > readStream . Length )
184
+ {
185
+ throw new ArgumentOutOfRangeException ( nameof ( offset ) , offset , string . Empty ) ;
186
+ }
192
187
193
- await StreamCopyOperation . CopyToAsync ( fileStream , outputStream , length , cancellationToken ) ;
188
+ if ( length . HasValue &&
189
+ ( length . Value < 0 || length . Value > readStream . Length - offset ) )
190
+ {
191
+ throw new ArgumentOutOfRangeException ( nameof ( length ) , length , string . Empty ) ;
194
192
}
193
+
194
+ readStream . Seek ( offset , SeekOrigin . Begin ) ; // TODO: What if !CanSeek?
195
+
196
+ await StreamCopyOperation . CopyToAsync ( readStream , outputStream , length , cancellationToken ) ;
195
197
}
196
198
}
197
199
}
0 commit comments