A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using System;
5using System.IO;
6
7namespace osu.Framework.IO.Network
8{
9 /// <summary>
10 /// Downloads a file from the internet to a specified location
11 /// </summary>
12 public class FileWebRequest : WebRequest
13 {
14 public string Filename;
15
16 protected override string Accept => "application/octet-stream";
17
18 protected override Stream CreateOutputStream()
19 {
20 string path = Path.GetDirectoryName(Filename);
21 if (!string.IsNullOrEmpty(path)) Directory.CreateDirectory(path);
22
23 return new FileStream(Filename, FileMode.Create, FileAccess.Write, FileShare.Write, 32768);
24 }
25
26 public FileWebRequest(string filename, string url)
27 : base(url)
28 {
29 Timeout *= 2;
30 Filename = filename;
31 }
32
33 protected override void Complete(Exception e = null)
34 {
35 ResponseStream?.Close();
36
37 if (e != null)
38 {
39 try
40 {
41 File.Delete(Filename);
42 }
43 catch (Exception eOther)
44 {
45 e = new AggregateException(e, eOther);
46 }
47 }
48
49 base.Complete(e);
50 }
51 }
52}