forked from Ailur/burgernotes-server
export notes
This commit is contained in:
parent
c99ac8acd2
commit
145cad27fa
27
main
27
main
|
@ -215,6 +215,33 @@ def apilistnotes():
|
||||||
|
|
||||||
return datatemplate, 200
|
return datatemplate, 200
|
||||||
|
|
||||||
|
@app.route("/api/exportnotes", methods=("GET", "POST"))
|
||||||
|
def apiexportnotes():
|
||||||
|
if request.method == "POST":
|
||||||
|
data = request.get_json()
|
||||||
|
secretKey = data["secretKey"]
|
||||||
|
|
||||||
|
userCookie = get_session(secretKey)
|
||||||
|
user = get_user(userCookie["id"])
|
||||||
|
|
||||||
|
conn = get_db_connection()
|
||||||
|
notes = conn.execute("SELECT * FROM notes WHERE creator = ? ORDER BY id DESC;", (user["id"],)).fetchall()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
datatemplate = []
|
||||||
|
|
||||||
|
for note in notes:
|
||||||
|
notetemplate = {
|
||||||
|
"id": note["id"],
|
||||||
|
"created": note["created"],
|
||||||
|
"edited": note["edited"],
|
||||||
|
"title": note["title"],
|
||||||
|
"content": note["content"]
|
||||||
|
}
|
||||||
|
datatemplate.append(notetemplate)
|
||||||
|
|
||||||
|
return datatemplate, 200
|
||||||
|
|
||||||
@app.route("/api/newnote", methods=("GET", "POST"))
|
@app.route("/api/newnote", methods=("GET", "POST"))
|
||||||
def apinewnote():
|
def apinewnote():
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
|
|
|
@ -188,6 +188,7 @@ body {
|
||||||
padding: 10px;
|
padding: 10px;
|
||||||
color: black;
|
color: black;
|
||||||
border-radius: 8px;
|
border-radius: 8px;
|
||||||
|
min-width: 338.5px
|
||||||
}
|
}
|
||||||
|
|
||||||
.optionsDiv button {
|
.optionsDiv button {
|
||||||
|
|
|
@ -29,6 +29,7 @@ let noteBox = document.getElementById("noteBox")
|
||||||
let loadingStuff = document.getElementById("loadingStuff")
|
let loadingStuff = document.getElementById("loadingStuff")
|
||||||
let burgerDropdown = document.getElementById("burgerDropdown")
|
let burgerDropdown = document.getElementById("burgerDropdown")
|
||||||
let burgerButton = document.getElementById("burgerButton")
|
let burgerButton = document.getElementById("burgerButton")
|
||||||
|
let exportNotesButton = document.getElementById("exportNotesButton")
|
||||||
|
|
||||||
for (let i = 0; i < 40; i++) {
|
for (let i = 0; i < 40; i++) {
|
||||||
notesDiv.appendChild(loadingStuff.cloneNode())
|
notesDiv.appendChild(loadingStuff.cloneNode())
|
||||||
|
@ -306,3 +307,53 @@ burgerButton.addEventListener("click", (event) => {
|
||||||
burgerDropdown.style.left = String(event.clientX) + "px"
|
burgerDropdown.style.left = String(event.clientX) + "px"
|
||||||
burgerDropdown.style.top = String(event.clientY) + "px"
|
burgerDropdown.style.top = String(event.clientY) + "px"
|
||||||
});
|
});
|
||||||
|
function downloadObjectAsJson(exportObj, exportName) {
|
||||||
|
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(exportObj));
|
||||||
|
var downloadAnchorNode = document.createElement("a");
|
||||||
|
downloadAnchorNode.setAttribute("href", dataStr);
|
||||||
|
downloadAnchorNode.setAttribute("download", exportName + ".json");
|
||||||
|
document.body.appendChild(downloadAnchorNode);
|
||||||
|
downloadAnchorNode.click();
|
||||||
|
downloadAnchorNode.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
function exportNotes() {
|
||||||
|
let noteExport = []
|
||||||
|
fetch("/api/exportnotes", {
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify({
|
||||||
|
secretKey: secretkey
|
||||||
|
}),
|
||||||
|
headers: {
|
||||||
|
"Content-type": "application/json; charset=UTF-8"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then((response) => response)
|
||||||
|
.then((response) => {
|
||||||
|
async function doStuff() {
|
||||||
|
let responseData = await response.json()
|
||||||
|
for (let i in responseData) {
|
||||||
|
let bytes = CryptoJS.AES.decrypt(responseData[i]["title"], password);
|
||||||
|
let originalTitle = bytes.toString(CryptoJS.enc.Utf8);
|
||||||
|
|
||||||
|
responseData[i]["title"] = originalTitle
|
||||||
|
|
||||||
|
let bytesd = CryptoJS.AES.decrypt(responseData[i]["content"], password);
|
||||||
|
let originalContent = bytesd.toString(CryptoJS.enc.Utf8);
|
||||||
|
|
||||||
|
responseData[i]["content"] = originalContent
|
||||||
|
}
|
||||||
|
let jsonString = JSON.parse(JSON.stringify(responseData))
|
||||||
|
console.log(jsonString)
|
||||||
|
|
||||||
|
downloadObjectAsJson(jsonString, "data")
|
||||||
|
}
|
||||||
|
doStuff()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
exportNotesButton.addEventListener("click", (event) => {
|
||||||
|
exportNotesButton.innerText = "exporting.."
|
||||||
|
exportNotes()
|
||||||
|
exportNotesButton.innerText = "export notes"
|
||||||
|
});
|
|
@ -39,6 +39,7 @@
|
||||||
<p id="storageThing"></p>
|
<p id="storageThing"></p>
|
||||||
<button id="logOutButton">log out</button>
|
<button id="logOutButton">log out</button>
|
||||||
<button id="deleteMyAccountButton">delete my account</button>
|
<button id="deleteMyAccountButton">delete my account</button>
|
||||||
|
<button id="exportNotesButton">export notes</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue