Terraria ModLoader  0.11.7.5
A mod to make and play Terraria mods
UploadFile.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Collections.Specialized;
4 using System.Globalization;
5 using System.IO;
6 using System.Net;
7 using System.Text;
8 
9 namespace Terraria.ModLoader.IO
10 {
11  public class UploadFile
12  {
13  public UploadFile() {
14  ContentType = "application/octet-stream";
15  }
16 
17  public string Name { get; set; }
18 
19  public string Filename { get; set; }
20 
21  public string ContentType { get; set; }
22 
23  public byte[] Content { get; set; }
24 
25  public static byte[] UploadFiles(string address, IEnumerable<UploadFile> files, NameValueCollection values) {
26  var request = WebRequest.Create(address);
27  request.Method = "POST";
28  var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
29  request.ContentType = "multipart/form-data; boundary=" + boundary;
30  boundary = "--" + boundary;
31  using (var requestStream = request.GetRequestStream()) {
32  WriteValues(requestStream, values, boundary);
33  WriteFiles(requestStream, files, boundary);
34  var boundaryBuffer = Encoding.ASCII.GetBytes(boundary + "--");
35  requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);
36  }
37  using (var response = request.GetResponse())
38  using (var responseStream = response.GetResponseStream())
39  using (var stream = new MemoryStream()) {
40  responseStream.CopyTo(stream);
41  return stream.ToArray();
42  }
43  }
44 
45  public static byte[] GetUploadFilesRequestData(IEnumerable<UploadFile> files, NameValueCollection values, string boundary) {
46  boundary = "--" + boundary;
47  using (var requestStream = new MemoryStream()) {
48  WriteValues(requestStream, values, boundary);
49  WriteFiles(requestStream, files, boundary);
50  var boundaryBuffer = Encoding.ASCII.GetBytes(boundary + "--");
51  requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);
52  return requestStream.ToArray();
53  }
54  }
55 
56  private static void WriteValues(Stream requestStream, NameValueCollection values, string boundary) {
57  if (values == null)
58  return;
59 
60  // Write the values
61  foreach (string name in values.Keys) {
62  var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
63  requestStream.Write(buffer, 0, buffer.Length);
64  buffer = Encoding.ASCII.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"{1}{1}", name, Environment.NewLine));
65  requestStream.Write(buffer, 0, buffer.Length);
66  buffer = Encoding.UTF8.GetBytes(values[name] + Environment.NewLine);
67  requestStream.Write(buffer, 0, buffer.Length);
68  }
69  }
70 
71  private static void WriteFiles(Stream requestStream, IEnumerable<UploadFile> files, string boundary) {
72  if (files == null)
73  return;
74 
75  // Write the files
76  foreach (var file in files) {
77  var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
78  requestStream.Write(buffer, 0, buffer.Length);
79  buffer = Encoding.UTF8.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", file.Name, file.Filename, Environment.NewLine));
80  requestStream.Write(buffer, 0, buffer.Length);
81  buffer = Encoding.ASCII.GetBytes(string.Format("Content-Type: {0}{1}{1}", file.ContentType, Environment.NewLine));
82  requestStream.Write(buffer, 0, buffer.Length);
83  requestStream.Write(file.Content, 0, file.Content.Length);
84  buffer = Encoding.ASCII.GetBytes(Environment.NewLine);
85  requestStream.Write(buffer, 0, buffer.Length);
86  }
87  }
88  }
89 }
static byte[] UploadFiles(string address, IEnumerable< UploadFile > files, NameValueCollection values)
Definition: UploadFile.cs:25
static void WriteFiles(Stream requestStream, IEnumerable< UploadFile > files, string boundary)
Definition: UploadFile.cs:71
static void WriteValues(Stream requestStream, NameValueCollection values, string boundary)
Definition: UploadFile.cs:56
static byte[] GetUploadFilesRequestData(IEnumerable< UploadFile > files, NameValueCollection values, string boundary)
Definition: UploadFile.cs:45
Sandstorm, Hell, Above surface during Eclipse, Space