forked from blankresearch/Memory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
277 lines (219 loc) · 8.1 KB
/
app.py
File metadata and controls
277 lines (219 loc) · 8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
from flask import (
Flask,
render_template,
request,
redirect,
url_for,
send_from_directory,
jsonify,
)
import sqlite3
import os
from flask_cors import CORS
app = Flask(__name__)
DB_FILE = "notes.db"
NOTES_DIR = "notes"
UPLOAD_IMAGE_FOLDER = os.path.join(NOTES_DIR, "images")
UPLOAD_VIDEO_FOLDER = os.path.join(NOTES_DIR, "videos")
app.config["UPLOAD_IMAGE_FOLDER"] = UPLOAD_IMAGE_FOLDER
app.config["UPLOAD_VIDEO_FOLDER"] = UPLOAD_VIDEO_FOLDER
# app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024 # 100MB limit for videos
CORS(
app,
resources={r"/upload_image": {"origins": "*"}, r"/upload_video": {"origins": "*"}},
)
if not os.path.exists(UPLOAD_IMAGE_FOLDER):
os.makedirs(UPLOAD_IMAGE_FOLDER)
if not os.path.exists(UPLOAD_VIDEO_FOLDER):
os.makedirs(UPLOAD_VIDEO_FOLDER)
def init_db():
with sqlite3.connect(DB_FILE) as conn:
c = conn.cursor()
c.execute(
"""
CREATE TABLE IF NOT EXISTS folders (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE
)"""
)
c.execute(
"""
CREATE TABLE IF NOT EXISTS notes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT UNIQUE,
filename TEXT,
folder_id INTEGER,
FOREIGN KEY (folder_id) REFERENCES folders(id) ON DELETE CASCADE
)"""
)
c.execute("INSERT OR IGNORE INTO folders (name) VALUES ('Principal')")
c.execute("INSERT OR IGNORE INTO folders (name) VALUES ('Templates')")
conn.commit()
init_db()
@app.route("/")
def index():
with sqlite3.connect(DB_FILE) as conn:
c = conn.cursor()
c.execute("SELECT id, name FROM folders")
folders = c.fetchall()
c.execute("SELECT id, title FROM notes")
notes = c.fetchall()
return render_template("index.html", notes=notes, folders=folders)
@app.route("/add_folder", methods=["POST"])
def add_folder():
name = request.form["name"]
with sqlite3.connect(DB_FILE) as conn:
c = conn.cursor()
c.execute("INSERT INTO folders (name) VALUES (?)", (name,))
conn.commit()
return redirect(url_for("index"))
@app.route("/edit_folder/<int:folder_id>", methods=["GET", "POST"])
def edit_folder(folder_id):
with sqlite3.connect(DB_FILE) as conn:
c = conn.cursor()
if request.method == "POST":
new_name = request.form["name"]
c.execute("UPDATE folders SET name = ? WHERE id = ?", (new_name, folder_id))
conn.commit()
return redirect(url_for("index"))
else:
c.execute("SELECT name FROM folders WHERE id = ?", (folder_id,))
folder = c.fetchone()
return render_template("edit_folder.html", folder=folder, folder_id=folder_id)
@app.route("/delete_folder/<int:folder_id>", methods=["POST"])
def delete_folder(folder_id):
with sqlite3.connect(DB_FILE) as conn:
c = conn.cursor()
c.execute("DELETE FROM folders WHERE id = ?", (folder_id,))
conn.commit()
return redirect(url_for("index"))
@app.route("/add", methods=["POST"])
def add_note():
if request.is_json: # If JSON request
data = request.get_json()
else: # If form submission
data = request.form
title = data.get("title")
folder_id = data.get("folder_id")
subtitle = data.get("subtitle", "")
if not title or not folder_id:
return jsonify({"success": False, "error": "Missing title or folder ID"}), 400
filename = f"{title}.md"
filepath = os.path.join(NOTES_DIR, filename)
with open(filepath, "w") as f:
f.write(f"")
with sqlite3.connect(DB_FILE) as conn:
c = conn.cursor()
c.execute(
"INSERT INTO notes (title, filename, folder_id, subtitle) VALUES (?, ?, ?, ?)",
(title, filename, folder_id, subtitle),
)
conn.commit()
return jsonify({"success": True})
@app.route("/edit/<int:note_id>", methods=["GET", "POST"])
def edit_note(note_id):
with sqlite3.connect(DB_FILE) as conn:
c = conn.cursor()
c.execute(
"SELECT title, filename, subtitle FROM notes WHERE id = ?", (note_id,)
)
note = c.fetchone()
if request.method == "POST":
content = request.form["content"]
with open(os.path.join(NOTES_DIR, note[1]), "w") as f:
f.write(content)
return redirect(url_for("index"))
with open(os.path.join(NOTES_DIR, note[1]), "r") as f:
content = f.read()
return render_template(
"edit.html", title=note[0], subtitle=note[2], content=content, note_id=note_id
)
@app.route("/delete/<int:note_id>", methods=["POST"])
def delete_note(note_id):
with sqlite3.connect(DB_FILE) as conn:
c = conn.cursor()
c.execute("SELECT filename FROM notes WHERE id=?", (note_id,))
row = c.fetchone()
if row:
path = os.path.join(NOTES_DIR, row[0])
if os.path.exists(path):
os.remove(path)
c.execute("DELETE FROM notes WHERE id=?", (note_id,))
conn.commit()
return redirect(url_for("index"))
@app.route("/upload_image", methods=["POST"])
def upload_image():
if "file" not in request.files:
return jsonify({"error": "No file part"}), 400
file = request.files["file"]
if file.filename == "":
return jsonify({"error": "No selected file"}), 400
filepath = os.path.join(app.config["UPLOAD_IMAGE_FOLDER"], file.filename)
file.save(filepath)
return jsonify({"url": f"/notes/images/{file.filename}"})
@app.route("/upload_video", methods=["POST"])
def upload_video():
if "file" not in request.files:
return jsonify({"error": "No file part"}), 400
file = request.files["file"]
if file.filename == "":
return jsonify({"error": "No selected file"}), 400
filepath = os.path.join(app.config["UPLOAD_VIDEO_FOLDER"], file.filename)
file.save(filepath)
return jsonify({"url": f"/notes/videos/{file.filename}"})
@app.route("/notes/images/<filename>")
def get_uploaded_image(filename):
return send_from_directory(app.config["UPLOAD_IMAGE_FOLDER"], filename)
@app.route("/notes/videos/<filename>")
def get_uploaded_video(filename):
return send_from_directory(app.config["UPLOAD_VIDEO_FOLDER"], filename)
@app.route("/save/<int:note_id>", methods=["POST"])
def save_note(note_id):
data = request.json
new_content = data.get("content", "")
with sqlite3.connect(DB_FILE) as conn:
c = conn.cursor()
c.execute("SELECT filename FROM notes WHERE id = ?", (note_id,))
note = c.fetchone()
if note:
filepath = os.path.join(NOTES_DIR, note[0])
with open(filepath, "w") as f:
f.write(new_content)
return jsonify({"success": True})
else:
return jsonify({"success": False, "error": "Note not found"}), 404
@app.route("/note/<int:note_id>", methods=["GET"])
def view_note(note_id):
with sqlite3.connect(DB_FILE) as conn:
c = conn.cursor()
c.execute(
"SELECT title, filename, subtitle FROM notes WHERE id = ?", (note_id,)
)
note = c.fetchone()
if not note:
return "Note not found", 404
with open(os.path.join(NOTES_DIR, note[1]), "r") as f:
content = f.read()
return render_template(
"note.html", title=note[0], subtitle=note[2], content=content, note_id=note_id
)
@app.route("/get_notes", methods=["GET"])
def get_notes():
folder_id = request.args.get("folder_id")
if not folder_id:
return jsonify({"error": "Missing folder_id parameter"}), 400
try:
folder_id = int(folder_id)
except ValueError:
return jsonify({"error": "Invalid folder_id parameter"}), 400
with sqlite3.connect(DB_FILE) as conn:
c = conn.cursor()
c.execute(
"SELECT id, title, subtitle FROM notes WHERE folder_id = ?", (folder_id,)
)
notes = [
{"id": row[0], "title": row[1], "subtitle": row[2]} for row in c.fetchall()
]
return jsonify(notes)
if __name__ == "__main__":
app.run(debug=True)