/// <summary> /// Handles deleting existing data files. Requires authentication and a special privilege. /// </summary> /// <param name="shandler"></param> /// <param name="request"></param> /// <param name="body"></param> /// <param name="encoder"></param> /// <returns></returns> public static async Task <Task> Action(ServerHandler shandler, HTTPRequest request, Stream body, IProxyHTTPEncoder encoder) { var auth_resp = await Helpers.ReadMessageFromStreamAndAuthenticate(shandler, 1024 * 16, body); if (!auth_resp.success) { return(encoder.Response(403, "Denied").SendNothing()); } if (!auth_resp.user.can_delete) { return(encoder.Response(403, "Denied").SendNothing()); } var sreq = JsonConvert.DeserializeObject <DeleteRequest>(auth_resp.payload); var sid = sreq.sid; if (shandler.items.ContainsKey(sid)) { var item = shandler.items[sid]; try { File.Delete(item.fqpath); } catch (Exception) { await encoder.WriteQuickHeaderAndStringBody( 500, "Error", JsonConvert.SerializeObject(new DeleteResponse() { success = false, }) ); return(Task.CompletedTask); } if (item.fqpath != null && item.fqpath.Length > 0) { shandler.UsedSpaceSubtract((long)item.datasize); } item.fqpath = null; await shandler.WriteItemToJournal(item); } await encoder.WriteQuickHeaderAndStringBody( 200, "Deleted", JsonConvert.SerializeObject(new DeleteResponse() { success = true, }) ); return(Task.CompletedTask); }