Terraria ModLoader  0.11.1
A framework for Terraria mods
Terraria.ModLoader.IO.UploadFile Class Reference

Public Member Functions

 UploadFile ()
 

Static Public Member Functions

static byte [] GetUploadFilesRequestData (IEnumerable< UploadFile > files, NameValueCollection values)
 
static byte [] UploadFiles (string address, IEnumerable< UploadFile > files, NameValueCollection values)
 

Properties

byte [] Content [get, set]
 
string ContentType [get, set]
 
string Filename [get, set]
 
string Name [get, set]
 

Static Private Member Functions

static void WriteFiles (Stream requestStream, IEnumerable< UploadFile > files, string boundary)
 
static void WriteValues (Stream requestStream, NameValueCollection values, string boundary)
 

Detailed Description

Definition at line 11 of file UploadFile.cs.

Constructor & Destructor Documentation

◆ UploadFile()

Terraria.ModLoader.IO.UploadFile.UploadFile ( )

Definition at line 13 of file UploadFile.cs.

13  {
14  ContentType = "application/octet-stream";
15  }

Member Function Documentation

◆ GetUploadFilesRequestData()

static byte [] Terraria.ModLoader.IO.UploadFile.GetUploadFilesRequestData ( IEnumerable< UploadFile files,
NameValueCollection  values 
)
static

Definition at line 45 of file UploadFile.cs.

45  {
46  var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
47  boundary = "--" + boundary;
48  using (var requestStream = new MemoryStream()) {
49  WriteValues(requestStream, values, boundary);
50  WriteFiles(requestStream, files, boundary);
51  var boundaryBuffer = Encoding.ASCII.GetBytes(boundary + "--");
52  requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);
53  return requestStream.ToArray();
54  }
55  }
static void WriteFiles(Stream requestStream, IEnumerable< UploadFile > files, string boundary)
Definition: UploadFile.cs:72
static void WriteValues(Stream requestStream, NameValueCollection values, string boundary)
Definition: UploadFile.cs:57

◆ UploadFiles()

static byte [] Terraria.ModLoader.IO.UploadFile.UploadFiles ( string  address,
IEnumerable< UploadFile files,
NameValueCollection  values 
)
static

Definition at line 25 of file UploadFile.cs.

25  {
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  }
static void WriteFiles(Stream requestStream, IEnumerable< UploadFile > files, string boundary)
Definition: UploadFile.cs:72
static void WriteValues(Stream requestStream, NameValueCollection values, string boundary)
Definition: UploadFile.cs:57

◆ WriteFiles()

static void Terraria.ModLoader.IO.UploadFile.WriteFiles ( Stream  requestStream,
IEnumerable< UploadFile files,
string  boundary 
)
staticprivate

Definition at line 72 of file UploadFile.cs.

References Terraria.ModLoader.Environment.

72  {
73  if (files == null)
74  return;
75 
76  // Write the files
77  foreach (var file in files) {
78  var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
79  requestStream.Write(buffer, 0, buffer.Length);
80  buffer = Encoding.UTF8.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", file.Name, file.Filename, Environment.NewLine));
81  requestStream.Write(buffer, 0, buffer.Length);
82  buffer = Encoding.ASCII.GetBytes(string.Format("Content-Type: {0}{1}{1}", file.ContentType, Environment.NewLine));
83  requestStream.Write(buffer, 0, buffer.Length);
84  requestStream.Write(file.Content, 0, file.Content.Length);
85  buffer = Encoding.ASCII.GetBytes(Environment.NewLine);
86  requestStream.Write(buffer, 0, buffer.Length);
87  }
88  }
Sandstorm, Hell, Above surface during Eclipse, Space

◆ WriteValues()

static void Terraria.ModLoader.IO.UploadFile.WriteValues ( Stream  requestStream,
NameValueCollection  values,
string  boundary 
)
staticprivate

Definition at line 57 of file UploadFile.cs.

References Terraria.ModLoader.Environment.

57  {
58  if (values == null)
59  return;
60 
61  // Write the values
62  foreach (string name in values.Keys) {
63  var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
64  requestStream.Write(buffer, 0, buffer.Length);
65  buffer = Encoding.ASCII.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"{1}{1}", name, Environment.NewLine));
66  requestStream.Write(buffer, 0, buffer.Length);
67  buffer = Encoding.UTF8.GetBytes(values[name] + Environment.NewLine);
68  requestStream.Write(buffer, 0, buffer.Length);
69  }
70  }
Sandstorm, Hell, Above surface during Eclipse, Space

Property Documentation

◆ Content

byte [] Terraria.ModLoader.IO.UploadFile.Content
getset

Definition at line 23 of file UploadFile.cs.

◆ ContentType

string Terraria.ModLoader.IO.UploadFile.ContentType
getset

Definition at line 21 of file UploadFile.cs.

◆ Filename

string Terraria.ModLoader.IO.UploadFile.Filename
getset

Definition at line 19 of file UploadFile.cs.

◆ Name

string Terraria.ModLoader.IO.UploadFile.Name
getset

Definition at line 17 of file UploadFile.cs.