SalexAI commited on
Commit
0b985a7
·
verified ·
1 Parent(s): 2ec6629

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -15
app.py CHANGED
@@ -3,30 +3,37 @@ import os
3
  from datetime import datetime
4
 
5
  app = Flask(__name__)
6
-
7
- # Ensure output folder exists
8
- os.makedirs("markdown_files", exist_ok=True)
9
 
10
  @app.route("/create-md", methods=["POST"])
11
  def create_markdown():
12
- data = request.get_json()
13
- if not data or "title" not in data or "content" not in data:
14
- return jsonify({"error": "Missing 'title' or 'content'"}), 400
15
-
16
- title = data["title"].strip()
17
- content = data["content"]
18
-
19
- # Sanitize filename
 
 
 
 
 
20
  filename = f"{title.replace(' ', '_')}_{int(datetime.utcnow().timestamp())}.md"
21
- filepath = os.path.join("markdown_files", filename)
22
 
23
- # Write markdown content
24
- with open(filepath, "w", encoding="utf-8") as f:
25
- f.write(f"# {title}\n\n{content}")
 
 
26
 
27
  return jsonify({
28
  "message": "Markdown file created",
29
  "filename": filename,
 
30
  "path": filepath
31
  }), 201
32
 
 
3
  from datetime import datetime
4
 
5
  app = Flask(__name__)
6
+ BASE_DIR = "markdown_files"
7
+ os.makedirs(BASE_DIR, exist_ok=True)
 
8
 
9
  @app.route("/create-md", methods=["POST"])
10
  def create_markdown():
11
+ try:
12
+ data = request.get_json(force=True)
13
+ title = data["title"].strip()
14
+ content = data["content"]
15
+ tag = data.get("tag", "untagged").strip().replace(" ", "_")
16
+ except Exception as e:
17
+ return jsonify({"error": f"Invalid request body or missing fields: {str(e)}"}), 400
18
+
19
+ # Create subfolder for tag
20
+ tag_dir = os.path.join(BASE_DIR, tag)
21
+ os.makedirs(tag_dir, exist_ok=True)
22
+
23
+ # Generate a timestamped filename
24
  filename = f"{title.replace(' ', '_')}_{int(datetime.utcnow().timestamp())}.md"
25
+ filepath = os.path.join(tag_dir, filename)
26
 
27
+ try:
28
+ with open(filepath, "w", encoding="utf-8") as f:
29
+ f.write(f"# {title}\n\n{content}")
30
+ except Exception as e:
31
+ return jsonify({"error": f"Could not write file: {str(e)}"}), 500
32
 
33
  return jsonify({
34
  "message": "Markdown file created",
35
  "filename": filename,
36
+ "tag": tag,
37
  "path": filepath
38
  }), 201
39