Skip to content

Commit

Permalink
fixed some issues with text editor
Browse files Browse the repository at this point in the history
  • Loading branch information
Mast3rwaf1z committed Nov 3, 2024
1 parent e149ebd commit 86b967f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
27 changes: 20 additions & 7 deletions app/Api/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import Data.List (find)
import Network.HTTP.Types (HeaderName)
import Text.Regex (matchRegex, mkRegex)
import System.IO (openFile, IOMode (ReadMode, WriteMode), hGetContents, hPutStr, hClose, writeFile)
import System.Directory (getDirectoryContents)
import System.Directory (getDirectoryContents, removeFile)
import Settings (getEditorRoot)

type Header = (HeaderName, ByteString)
type APIResponse = IO (Status, String, [Header])
Expand Down Expand Up @@ -98,11 +99,12 @@ apiMap = [
),
("/editor/new", \r -> do
filename <- getRequestBodyChunk r
files <- getDirectoryContents "./editor_root"
editor_root <- getEditorRoot
files <- getDirectoryContents editor_root
if elem (unpackBS filename) files then do
return (status400, j2s [aesonQQ|{"message":"Error! file already exists"}|], jsonHeaders)
else do
handle <- openFile ("./editor_root/" ++ unpackBS filename) WriteMode
handle <- openFile (editor_root ++ "/" ++ unpackBS filename) WriteMode
hPutStr handle ""
hClose handle
return (status200, j2s [aesonQQ|{"status":"ok"}|], jsonHeaders)
Expand All @@ -118,12 +120,14 @@ apiMap = [
return (status200, j2s [aesonQQ|{"entries":#{unpackBS $ toStrict $ encode $ show entries}}|], jsonHeaders)
),
("/editor/sidebar", \_ -> do
files <- getDirectoryContents "./editor_root"
editor_root <- getEditorRoot
files <- getDirectoryContents editor_root
return (status200, j2s [aesonQQ|#{files}|], jsonHeaders)
),
("/editor/content/.*", \r -> do
let filename = unpack $ last $ pathInfo r
handle <- openFile ("./editor_root/" ++ filename) ReadMode
editor_root <- getEditorRoot
handle <- openFile (editor_root ++ "/" ++ filename) ReadMode
contents <- hGetContents handle
return (status200, contents, defaultHeaders)
)
Expand Down Expand Up @@ -156,13 +160,22 @@ apiMap = [
body <- getRequestBodyChunk r
let content = unpackBS body
let filename = unpack $ last $ pathInfo r
handle <- openFile ("./editor_root/" ++ filename) WriteMode
editor_root <- getEditorRoot
handle <- openFile (editor_root ++ "/" ++ filename) WriteMode
hPutStr handle content
hClose handle
return (status200, messageResponse "ok", jsonHeaders)
)
]),
("DELETE", [])
("DELETE", [
("/editor/delete", \r -> do
body <- getRequestBodyChunk r
editor_root <- getEditorRoot
let filename = unpackBS body
removeFile $ editor_root ++ "/" ++ filename
return (status200, messageResponse "ok", jsonHeaders)
)
])
]

api :: Request -> APIResponse
Expand Down
12 changes: 12 additions & 0 deletions app/Pages/Projects/Editor.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ commands = [hsx|
body: content
}).then(_ => status_message("Successfully saved file"))
}
function delete_file() {
var filename = document.getElementById("filename").innerHTML
fetch("/api/editor/delete", {
method: "DELETE",
body: filename
}).then(async _ => {
sidebar.innerHTML = ""
await populate_sidebar()
status_message("deleted file!")
})
}
</script>
|]

Expand Down Expand Up @@ -87,6 +98,7 @@ mainFrame = [hsx|
<textarea style="min-width: 700px; min-height: 300px;" id="editor">
</textarea><br>
<button onclick="save_file()">Save File</button>
<button onclick="delete_file()">Delete File</button>
|]

editor :: Html
Expand Down
7 changes: 6 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,18 @@
type = lib.types.str;
default = "homepage";
};
editor.root = lib.mkOption {
type = lib.types.str;
default = "/var/run/website/editor";
};
};

config.systemd.services.website = {
config.systemd.services.homepage = {
enable = cfg.enable;
environment.HOMEPAGE_PORT = builtins.toString cfg.port;
environment.HOMEPAGE_DB = cfg.db.name;
environment.HOMEPAGE_DB_USER = cfg.db.user;
environment.HOMEPAGE_EDITOR_ROOT = cfg.editor.root;
serviceConfig = {
User = cfg.db.user;
WorkingDirectory = "${packages.${system}.default}";
Expand Down
4 changes: 4 additions & 0 deletions lib/Settings.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ getColor = argOrEnvOrBool "--color" "HOMEPAGE_COLOR"

getMigrate :: IO Bool
getMigrate = argOrEnvOrBool "--migrate" "HOMEPAGE_MIGRATE"

getEditorRoot :: IO String
getEditorRoot = argOrEnvOrDefault "--editor_root" "HOMEPAGE_EDITOR_ROOT" "./editor_root"

0 comments on commit 86b967f

Please sign in to comment.