public override async Task <string> ProcessData(DbUserContent uc, Stream input, Stream output) { Image <Rgba32> image; //Open this as an image try { image = Image <Rgba32> .Load <Rgba32>(input); } catch { return("Error opening the image!"); } //Now, we'll apply transformations if (settings.do_resize) { image.Mutate(x => x.Resize(settings.resize_x, settings.resize_y)); } //Now, we'll write to the output and set values image.SaveAsPng(output); uc.size = (int)output.Length; uc.type = "image/png"; return(null); }
public static async Task OnHttpRequest(Microsoft.AspNetCore.Http.HttpContext e) { //Get the file data DbUserContent uc = await Program.conn.GetUserContentByName(e.Request.Path.ToString().Split('/')[2]); if (uc == null) { e.Response.StatusCode = 404; await Program.WriteStringToStreamAsync(e.Response.Body, "Not Found"); return; } //Set headers e.Response.ContentLength = uc.size; e.Response.ContentType = uc.type; e.Response.Headers.Add("X-Uploader-ID", uc.uploader.ToString()); e.Response.Headers.Add("X-Uploader-Node", uc.node_name); e.Response.Headers.Add("Date", uc.upload_time.ToUniversalTime().ToString("r")); //If we requested a download, set the content type if (e.Request.Query["download"] == "true") { e.Response.ContentType = "application/octet-strean"; } //Open file stream and copy using (FileStream fs = new FileStream(Program.config.content_path + uc.name, FileMode.Open, FileAccess.Read)) await fs.CopyToAsync(e.Response.Body); }
/// <summary> /// Processes the data. /// </summary> /// <param name="uc">File info</param> /// <param name="input">Input stream</param> /// <param name="output">Output stream</param> /// <returns>Not null if there was an error</returns> public abstract Task <string> ProcessData(DbUserContent uc, Stream input, Stream output);
public static async Task OnHttpRequest(Microsoft.AspNetCore.Http.HttpContext e) { //Authenticate the user DbUser user = await Program.conn.AuthenticateUserToken(e.Request.Headers["Authorization"].ToString().Substring("Bearer ".Length)); if (user == null) { e.Response.StatusCode = 401; await Program.WriteStringToStreamAsync(e.Response.Body, "Not Authenticated"); return; } //Make sure that the content length is within range if (!e.Request.ContentLength.HasValue) { e.Response.StatusCode = 411; await Program.WriteStringToStreamAsync(e.Response.Body, "Content-Type Header is Required"); return; } if (e.Request.ContentLength.Value > Program.config.max_filesize) { e.Response.StatusCode = 413; await Program.WriteStringToStreamAsync(e.Response.Body, "File Too Large"); return; } //Get the application BaseAdapter adapter; string application_id = e.Request.Query["application_id"]; if (Program.applications.ContainsKey(application_id)) { adapter = Program.applications[application_id]; } else { e.Response.StatusCode = 400; await Program.WriteStringToStreamAsync(e.Response.Body, "Invalid Application ID"); return; } //Generate a filename to use string name = LibDeltaSystem.Tools.SecureStringTool.GenerateSecureString(24); while (File.Exists(Program.config.content_path + name)) { name = LibDeltaSystem.Tools.SecureStringTool.GenerateSecureString(24); } //Create a random token to use string token = LibDeltaSystem.Tools.SecureStringTool.GenerateSecureString(48); while (await Program.conn.GetUserContentByToken(token) != null) { token = LibDeltaSystem.Tools.SecureStringTool.GenerateSecureString(48); } //Create a DB entry DbUserContent uc = new DbUserContent { node_name = Program.config.node_name, size = (int)e.Request.ContentLength.Value, token = token, type = e.Request.ContentType, uploader = user._id, upload_time = DateTime.UtcNow, url = Program.config.content_url + name, name = name, _id = ObjectId.GenerateNewId(), application_id = application_id }; //Create a file to write to using (FileStream fs = new FileStream(Program.config.content_path + name, FileMode.Create)) using (MemoryStream ms = new MemoryStream()) { //Copy to stream byte[] buffer = new byte[BUFFER_SIZE]; int remaining = (int)e.Request.ContentLength.Value; while (remaining > 0) { int read = await e.Request.Body.ReadAsync(buffer, 0, BUFFER_SIZE); await ms.WriteAsync(buffer, 0, read); remaining -= read; } //Process ms.Position = 0; await adapter.ProcessData(uc, ms, fs); } //Insert await Program.conn.system_user_uploads.InsertOneAsync(uc); //Write data e.Response.ContentType = "application/json"; await Program.WriteStringToStreamAsync(e.Response.Body, JsonConvert.SerializeObject(uc, Formatting.Indented)); }
public static async Task OnCanvasRequest(Microsoft.AspNetCore.Http.HttpContext e, DbServer s, DbUser u, int tribe_id, string next) { //Find canvas if (!ObjectId.TryParse(next, out ObjectId id)) { throw new StandardError("Canvas Not Found", StandardErrorCode.NotFound); } DbCanvas c = await Program.connection.LoadCanvasData(id); if (c == null) { throw new StandardError("Canvas Not Found", StandardErrorCode.NotFound); } //Get request method var method = Program.FindRequestMethod(e); //Act from the method if (method == RequestHttpMethod.get) { //Write response await Program.QuickWriteJsonToDoc(e, ConvertCanvas(c)); } else if (method == RequestHttpMethod.post) { //Check scope await Program.CheckTokenScope(u, null); //Rename the canvas; decode request CanvasCreateRequest request = Program.DecodePostBody <CanvasCreateRequest>(e); //Set await c.RenameCanvas(Program.connection, request.name, request.color); //Send RPC message SendRPCMessage(c, s, u, tribe_id, RPCPayloadModifyCanvas.RPCPayloadModifyCanvas_CanvasChange.Modify); //Write response await Program.QuickWriteJsonToDoc(e, ConvertCanvas(c)); } else if (method == RequestHttpMethod.delete) { //Check scope await Program.CheckTokenScope(u, null); //Delete canvas await c.DeleteCanvas(Program.connection); //Send RPC message SendRPCMessage(c, s, u, tribe_id, RPCPayloadModifyCanvas.RPCPayloadModifyCanvas_CanvasChange.Delete); //Write response await Program.QuickWriteJsonToDoc(e, ConvertCanvas(c)); } else if (method == RequestHttpMethod.put) { //Check scope await Program.CheckTokenScope(u, null); //We'll update the thumbnail; Decode the request UpdateThumbnailRequest request = Program.DecodePostBody <UpdateThumbnailRequest>(e); //Try to find DbUserContent uc = await Program.connection.GetUserContentByToken(request.token); if (uc == null) { throw new StandardError("Token Not Valid", StandardErrorCode.InvalidInput); } if (uc.application_id != CANVAS_THUMBNAIL_APPLICATION_ID) { throw new StandardError("Specified User Content Application ID Mismatch", StandardErrorCode.InvalidInput); } //Update await c.SetNewThumbnail(Program.connection, uc); //Send RPC message SendRPCMessage(c, s, u, tribe_id, RPCPayloadModifyCanvas.RPCPayloadModifyCanvas_CanvasChange.Modify); //Write response await Program.QuickWriteJsonToDoc(e, ConvertCanvas(c)); } else { throw new StandardError("Unexpected method.", StandardErrorCode.BadMethod); } }