static Task OnGetRequest(Microsoft.AspNetCore.Http.HttpContext e, UserContentFileEntry f) { //Open stream on file using (FileStream fs = new FileStream(Program.config.uploaded_content_path + f.filename, FileMode.Open)) { e.Response.ContentLength = fs.Length; e.Response.ContentType = f.mimeType; return(fs.CopyToAsync(e.Response.Body)); } }
public static Task OnHttpRequest(Microsoft.AspNetCore.Http.HttpContext e) { //Get the upload token from the URL if (!e.Request.Query.ContainsKey("token")) { throw new StandardError("Missing required argument 'token' in URL.", StandardErrorType.MissingArgs); } string token = e.Request.Query["token"]; if (!Program.fileCreationTokens.ContainsKey(token)) { throw new StandardError("This token is not valid.", StandardErrorType.BadAuth); } UserContentFileEntry entry = Program.fileCreationTokens[token]; //Respond with this token. Task t = Program.QuickWriteJsonToDoc(e, entry); //Remove token Program.fileCreationTokens.Remove(token); //Await return(t); }
public static Task OnHttpRequest(Microsoft.AspNetCore.Http.HttpContext e) { //If this isn't a post request, stop if (Program.FindRequestMethod(e) != RequestHttpMethod.post) { throw new StandardError("Invalid method for this service.", StandardErrorType.NotFound); } //Extract the application ID from the query. UserContentApp application = GetApplication(e); //Grab the stream of the uploaded file var file = e.Request.Form.Files["f"]; if (file == null) { throw new StandardError("No file found. Make sure this is submitted as a form with the file under the 'f' name.", StandardErrorType.NoFile); } Stream s; try { s = file.OpenReadStream(); } catch { throw new StandardError("Could not open stream on file.", StandardErrorType.NoFile); } //Check if the length is longer than the maximum allowed. if (file.Length > Program.config.maximum_upload_size) { throw new StandardError($"The file you uploaded was too large. Please keep the length under {Program.config.maximum_upload_size} bytes (this file is {file.Length}).", StandardErrorType.FileTooBig); } //Do processing if needed. Stream outStream = DoProcess(e, s, application); //It is now time to output this to the disk. Generate an ID var collec = Program.GetFileEntryCollection(); string id = Program.GenerateRandomString(24); while (collec.Find(x => x._id == id).Count() != 0) { id = Program.GenerateRandomString(24); } //Generate a one-time token. This can be used to securely send the server the details, even if this is sent to a web client string token = Program.GenerateRandomString(32); while (Program.fileCreationTokens.ContainsKey(token)) { token = Program.GenerateRandomString(32); } //Create entry. UserContentFileEntry entry = new UserContentFileEntry { _id = id, uploadTime = DateTime.UtcNow.Ticks, filename = id, applicationId = application.id, mimeType = GetMimeType(e, application), deletionToken = Program.GenerateRandomString(64), url = "https://user-content.romanport.com/u/" + id }; //Write file try { using (FileStream fs = new FileStream(Program.config.uploaded_content_path + entry.filename, FileMode.Create)) { outStream.CopyTo(fs); } } catch { throw new StandardError("Failed to place file on disk. Try again later.", StandardErrorType.UnknownError); } //Insert entry collec.Insert(entry); //Insert entry into the temporary tokens directory Program.fileCreationTokens.Add(token, entry); //Close the stream try { outStream.Close(); } catch { } //Respond return(Program.QuickWriteJsonToDoc(e, new CreateResponse { ok = true, token = token, url = entry.url })); }