2017-08-23 03:26:01 +00:00
|
|
|
|
using System;
|
2017-08-23 04:24:52 +00:00
|
|
|
|
using System.Collections.Generic;
|
2023-03-17 08:08:32 +00:00
|
|
|
|
using System.IO;
|
2017-08-23 03:26:01 +00:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using Ooui;
|
2023-03-17 08:08:32 +00:00
|
|
|
|
using Tesses.WebServer;
|
2017-08-23 03:26:01 +00:00
|
|
|
|
|
|
|
|
|
namespace Samples
|
|
|
|
|
{
|
2018-09-02 04:56:35 +00:00
|
|
|
|
public class FilesSample : ISample
|
2017-08-23 03:26:01 +00:00
|
|
|
|
{
|
2017-11-10 06:35:47 +00:00
|
|
|
|
public string Title => "Upload files";
|
2018-09-02 04:56:35 +00:00
|
|
|
|
public string Path => "/files";
|
2017-11-10 06:35:47 +00:00
|
|
|
|
|
2018-09-02 04:56:35 +00:00
|
|
|
|
public void Publish ()
|
2017-08-23 03:26:01 +00:00
|
|
|
|
{
|
2017-11-10 06:35:47 +00:00
|
|
|
|
var app = CreateElement ();
|
2017-08-23 03:26:01 +00:00
|
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
|
Xamarin.Forms.PageExtensions.Ui.Publish (Path, app);
|
2017-08-23 03:26:01 +00:00
|
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
|
Xamarin.Forms.PageExtensions.Ui.PublishCustomResponse ("/files/upload", HandleUpload);
|
2017-08-23 03:26:01 +00:00
|
|
|
|
}
|
2023-03-17 08:08:32 +00:00
|
|
|
|
void HandleUpload(ServerContext listenerContext,CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
if(listenerContext.Method == "POST")
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory("Uploads");
|
|
|
|
|
foreach(var file in listenerContext.ParseBody((field,filename,ct)=>{
|
|
|
|
|
Console.WriteLine($"Uploading file: {filename} with ct: {ct}");
|
|
|
|
|
return File.Create(System.IO.Path.Combine("Uploads",filename));
|
|
|
|
|
}))
|
|
|
|
|
{
|
|
|
|
|
file.Value.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*void HandleUpload (ServerContext listenerContext, CancellationToken token)
|
2017-08-23 03:26:01 +00:00
|
|
|
|
{
|
|
|
|
|
var req = listenerContext.Request;
|
|
|
|
|
var ct = req.ContentType;
|
|
|
|
|
var bi = ct.IndexOf ("boundary=", StringComparison.InvariantCulture);
|
|
|
|
|
var boundaryString = ct.Substring (bi + 9);
|
2017-08-23 04:24:52 +00:00
|
|
|
|
var boundaryBytes = System.Text.Encoding.UTF8.GetBytes ("--" + boundaryString + "\r\n");
|
2017-08-23 03:26:01 +00:00
|
|
|
|
var boundaryEndBytes = System.Text.Encoding.UTF8.GetBytes (boundaryString + "--");
|
2017-08-23 04:24:52 +00:00
|
|
|
|
var headerEndBytes = System.Text.Encoding.UTF8.GetBytes ("\r\n\r\n");
|
2017-08-23 03:26:01 +00:00
|
|
|
|
Console.WriteLine ("OMGGGGGG " + boundaryString);
|
2017-08-23 04:24:52 +00:00
|
|
|
|
|
|
|
|
|
var state = 0;
|
|
|
|
|
var buffer = new byte[1024];
|
|
|
|
|
var bufferLen = 0;
|
|
|
|
|
|
|
|
|
|
var needsRead = true;
|
|
|
|
|
|
|
|
|
|
using (var s = req.InputStream) {
|
|
|
|
|
while (state < 1000) {
|
|
|
|
|
if (needsRead) {
|
|
|
|
|
var r = buffer.Length - bufferLen;
|
|
|
|
|
if (r <= 0) {
|
|
|
|
|
Array.Resize (ref buffer, buffer.Length * 2);
|
|
|
|
|
r = buffer.Length - bufferLen;
|
|
|
|
|
}
|
|
|
|
|
var n = s.Read (buffer, bufferLen, r);
|
|
|
|
|
if (n > 0) {
|
|
|
|
|
bufferLen += n;
|
|
|
|
|
}
|
|
|
|
|
else if (n == 0) {
|
|
|
|
|
// End!
|
|
|
|
|
state = 1000;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
state = 1001;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
switch (state) {
|
|
|
|
|
case 0:
|
|
|
|
|
var i = FindIndex (buffer, 0, bufferLen, boundaryBytes);
|
|
|
|
|
if (i >= 0) {
|
|
|
|
|
var e = i + boundaryBytes.Length;
|
|
|
|
|
var r = bufferLen - e;
|
|
|
|
|
Buffer.BlockCopy (buffer, e, buffer, 0, r);
|
|
|
|
|
bufferLen = r;
|
|
|
|
|
state = 1;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
needsRead = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
i = FindIndex (buffer, 0, bufferLen, headerEndBytes);
|
|
|
|
|
if (i >= 0) {
|
|
|
|
|
var h = System.Text.Encoding.ASCII.GetString (buffer, 0, i);
|
|
|
|
|
Console.WriteLine ("HEADERS {0}", h);
|
|
|
|
|
var e = i + headerEndBytes.Length;
|
|
|
|
|
var r = bufferLen - e;
|
|
|
|
|
Buffer.BlockCopy (buffer, e, buffer, 0, r);
|
|
|
|
|
bufferLen = r;
|
|
|
|
|
state = 2;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
needsRead = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
i = FindIndex (buffer, 0, bufferLen, boundaryEndBytes);
|
|
|
|
|
if (i >= 0) {
|
|
|
|
|
Console.WriteLine ("DATA {0}", bufferLen);
|
|
|
|
|
var data = new byte[bufferLen];
|
|
|
|
|
var e = i + boundaryBytes.Length;
|
|
|
|
|
var r = bufferLen - e;
|
|
|
|
|
Buffer.BlockCopy (buffer, e, buffer, 0, r);
|
|
|
|
|
bufferLen = r;
|
|
|
|
|
state = 0;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
needsRead = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Console.WriteLine ("STATE " + state);
|
2017-08-23 03:26:01 +00:00
|
|
|
|
listenerContext.Response.StatusCode = 200;
|
|
|
|
|
listenerContext.Response.Close ();
|
2023-03-17 08:08:32 +00:00
|
|
|
|
}*/
|
2017-08-23 04:24:52 +00:00
|
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
|
|
2017-11-10 06:35:47 +00:00
|
|
|
|
|
|
|
|
|
public Element CreateElement ()
|
|
|
|
|
{
|
|
|
|
|
var heading = new Heading ("Upload Files");
|
|
|
|
|
var subtitle = new Paragraph ("Upload files to the app");
|
|
|
|
|
|
|
|
|
|
var uploadForm = new Form ();
|
|
|
|
|
uploadForm.Action = "/files/upload";
|
|
|
|
|
uploadForm.Method = "POST";
|
|
|
|
|
uploadForm.EncodingType = "multipart/form-data";
|
|
|
|
|
uploadForm.AppendChild (new Input (InputType.File) { Name = "file" });
|
|
|
|
|
uploadForm.AppendChild (new Input (InputType.Submit) { Value = "Upload" });
|
|
|
|
|
|
|
|
|
|
var app = new Div ();
|
|
|
|
|
app.AppendChild (heading);
|
|
|
|
|
app.AppendChild (subtitle);
|
|
|
|
|
app.AppendChild (uploadForm);
|
|
|
|
|
|
|
|
|
|
return app;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-23 03:26:01 +00:00
|
|
|
|
}
|