root commited on
Commit
b3574fc
·
1 Parent(s): 4d801c0
Files changed (2) hide show
  1. image_routes.py +17 -1
  2. templates/_revision_notes.html +6 -2
image_routes.py CHANGED
@@ -2,6 +2,7 @@ from flask import Blueprint, send_from_directory, current_app, request, jsonify
2
  from flask_login import login_required, current_user
3
  from utils import get_db_connection
4
  import os
 
5
  from datetime import datetime
6
 
7
  image_bp = Blueprint('image_bp', __name__)
@@ -139,12 +140,13 @@ def delete_note():
139
  @image_bp.route('/save_note_json', methods=['POST'])
140
  @login_required
141
  def save_note_json():
142
- """Save revision notes as JSON (no rasterization)."""
143
  try:
144
  data = request.json
145
  image_id = data.get('image_id')
146
  session_id = data.get('session_id')
147
  json_data = data.get('json_data')
 
148
 
149
  if not image_id or not session_id or not json_data:
150
  return jsonify({'error': 'Missing required fields'}), 400
@@ -162,6 +164,20 @@ def save_note_json():
162
 
163
  # Save JSON to database
164
  conn.execute("UPDATE images SET note_json = ? WHERE id = ?", (json_data, image_id))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  conn.commit()
166
  conn.close()
167
 
 
2
  from flask_login import login_required, current_user
3
  from utils import get_db_connection
4
  import os
5
+ import base64
6
  from datetime import datetime
7
 
8
  image_bp = Blueprint('image_bp', __name__)
 
140
  @image_bp.route('/save_note_json', methods=['POST'])
141
  @login_required
142
  def save_note_json():
143
+ """Save revision notes as JSON and rasterized PNG for PDF/quiz display."""
144
  try:
145
  data = request.json
146
  image_id = data.get('image_id')
147
  session_id = data.get('session_id')
148
  json_data = data.get('json_data')
149
+ image_data = data.get('image_data')
150
 
151
  if not image_id or not session_id or not json_data:
152
  return jsonify({'error': 'Missing required fields'}), 400
 
164
 
165
  # Save JSON to database
166
  conn.execute("UPDATE images SET note_json = ? WHERE id = ?", (json_data, image_id))
167
+
168
+ # Save rasterized PNG if provided (needed for PDF generation and quiz display)
169
+ if image_data and image_data.startswith('data:image/'):
170
+ # Strip the data URL prefix (e.g. "data:image/png;base64,")
171
+ header, encoded = image_data.split(',', 1)
172
+ img_bytes = base64.b64decode(encoded)
173
+
174
+ filename = f"note_{session_id}_{image_id}_{int(datetime.now().timestamp())}.png"
175
+ save_path = os.path.join(current_app.config['PROCESSED_FOLDER'], filename)
176
+ with open(save_path, 'wb') as f:
177
+ f.write(img_bytes)
178
+
179
+ conn.execute("UPDATE images SET note_filename = ? WHERE id = ?", (filename, image_id))
180
+
181
  conn.commit()
182
  conn.close()
183
 
templates/_revision_notes.html CHANGED
@@ -503,9 +503,12 @@
503
  }
504
 
505
  async function saveNotes() {
506
- // Save as JSON instead of rasterized image
507
  const jsonData = JSON.stringify(canvas.toJSON());
508
 
 
 
 
509
  try {
510
  const response = await fetch('/save_note_json', {
511
  method: 'POST',
@@ -513,7 +516,8 @@
513
  body: JSON.stringify({
514
  image_id: activeImageId,
515
  session_id: '{{ session_id }}',
516
- json_data: jsonData
 
517
  })
518
  });
519
 
 
503
  }
504
 
505
  async function saveNotes() {
506
+ // Save as JSON AND rasterized PNG (needed for PDF/quiz display)
507
  const jsonData = JSON.stringify(canvas.toJSON());
508
 
509
+ // Rasterize canvas to PNG data URL for PDF/quiz rendering
510
+ const imageData = canvas.toDataURL({ format: 'png', multiplier: 2 });
511
+
512
  try {
513
  const response = await fetch('/save_note_json', {
514
  method: 'POST',
 
516
  body: JSON.stringify({
517
  image_id: activeImageId,
518
  session_id: '{{ session_id }}',
519
+ json_data: jsonData,
520
+ image_data: imageData
521
  })
522
  });
523