HttpServerBoxed 0.0.11 alpha
A simple http server for C# and .NET
Loading...
Searching...
No Matches
HSB.Components.FilePart Class Reference

Represents a file of a multipart form. More...

Inheritance diagram for HSB.Components.FilePart:
HSB.Components.FormPart

Public Member Functions

 FilePart (byte[] data)
 
string GetMimeType ()
 
byte[] GetBytes ()
 
void SaveToDisk (string path)
 
override string ToString ()
 
- Public Member Functions inherited from HSB.Components.FormPart
 FormPart (byte[] data)
 
override string ToString ()
 

Public Attributes

readonly string ContentType
 
readonly string FileName
 
- Public Attributes inherited from HSB.Components.FormPart
string ContentDisposition
 
string Name
 
byte[] Data
 

Additional Inherited Members

- Static Public Member Functions inherited from HSB.Components.FormPart
static FormPart Build (byte[] data)
 

Detailed Description

Represents a file of a multipart form.

Definition at line 8 of file FilePart.cs.

Constructor & Destructor Documentation

◆ FilePart()

HSB.Components.FilePart.FilePart ( byte[]  data)

Definition at line 14 of file FilePart.cs.

14 : base(data)
15 {
16 //first line is Content-Disposition
17 //second line is Content-Type
18 //third and forth line are CRLF
19 //the rest is the data
20
21 FileName = ContentDisposition.Split(";")[2].Split("=")[1].Replace("\"", "").Replace("\r\n", "");
22
23 int contentTypeLineStart = data.IndexOf("\r\n"u8.ToArray());
24 int contentTypeLineEnd = data.IndexOf("\r\n\r\n"u8.ToArray());
25 try
26 {
27 ContentType = Encoding.UTF8
28 .GetString(data[(contentTypeLineStart + 2)..contentTypeLineEnd])
29 .Split("Content-Type: ")[1];
30 }
31 catch (Exception)
32 {
33 ContentType = MimeTypeUtils.APPLICATION_OCTET;
34 }
35 base.Data = data[(contentTypeLineEnd + 4)..^2]; //skip the two CRLF at the begin and the one at the end
36 }

Member Function Documentation

◆ GetBytes()

byte[] HSB.Components.FilePart.GetBytes ( )

Definition at line 43 of file FilePart.cs.

44 {
45 return Data;
46 }

◆ GetMimeType()

string HSB.Components.FilePart.GetMimeType ( )

Definition at line 38 of file FilePart.cs.

39 {
40 return ContentType;
41 }

◆ SaveToDisk()

void HSB.Components.FilePart.SaveToDisk ( string  path)

Definition at line 47 of file FilePart.cs.

48 {
49 string _path;
50 if (Path.HasExtension(path))
51 _path = path;
52 else
53 {
54 _path = Path.Combine(path, FileName);
55 if (Utils.IsUnsafePath(FileName))
56 {
57 var detectedExt = MimeTypeUtils.GetExtension(ContentType);
58 if (detectedExt == "")
59 detectedExt = ".bin";
60 _path = Path.Combine(path,
61 "file_" + Utils.GenerateRandomString(4) + "." + detectedExt);
62 }
63 }
64
65 File.WriteAllBytes(_path, Data);
66 }

◆ ToString()

override string HSB.Components.FilePart.ToString ( )

Definition at line 68 of file FilePart.cs.

69 {
70 return $"Filename : {FileName} | Content-Type : {ContentType} | Size {Data.Length.AsSizeHumanReadable()}";
71 }

Member Data Documentation

◆ ContentType

readonly string HSB.Components.FilePart.ContentType

Definition at line 11 of file FilePart.cs.

◆ FileName

readonly string HSB.Components.FilePart.FileName

Definition at line 12 of file FilePart.cs.


The documentation for this class was generated from the following file: