added file sync button

This commit is contained in:
2025-05-27 11:52:02 +02:00
parent a67470d616
commit dca23787e4
8 changed files with 143 additions and 0 deletions

Binary file not shown.

View File

@@ -14,6 +14,7 @@ def init_app(app: Flask):
from .contacts import contacts_bp as contacts_routes
from .rooms import rooms_bp as rooms_routes
from .conversations import conversations_bp as conversations_routes
from .admin import admin as admin_routes
# Initialize routes
init_main_routes(main_bp)
@@ -31,6 +32,7 @@ def init_app(app: Flask):
app.register_blueprint(rooms_routes)
app.register_blueprint(contacts_routes)
app.register_blueprint(conversations_routes)
app.register_blueprint(admin_routes)
@app.route('/rooms/<int:room_id>/trash')
@login_required

Binary file not shown.

67
routes/admin.py Normal file
View File

@@ -0,0 +1,67 @@
from flask import Blueprint, jsonify
from flask_login import login_required, current_user
from models import db, Room, RoomFile, User
import os
from datetime import datetime
admin = Blueprint('admin', __name__)
@admin.route('/api/admin/sync-files', methods=['POST'])
@login_required
def sync_files():
if not current_user.is_admin:
return jsonify({'error': 'Unauthorized'}), 403
try:
DATA_ROOT = '/data/rooms'
admin_user = User.query.filter_by(is_admin=True).first()
if not admin_user:
return jsonify({'error': 'No admin user found'}), 500
rooms = Room.query.all()
for room in rooms:
room_dir = os.path.join(DATA_ROOT, str(room.id))
if not os.path.exists(room_dir):
continue
for root, dirs, files in os.walk(room_dir):
rel_root = os.path.relpath(root, room_dir)
rel_path = '' if rel_root == '.' else rel_root.replace('\\', '/')
# Folders
for d in dirs:
exists = RoomFile.query.filter_by(room_id=room.id, name=d, path=rel_path, type='folder').first()
folder_path = os.path.join(root, d)
stat = os.stat(folder_path)
if not exists:
rf = RoomFile(
room_id=room.id,
name=d,
path=rel_path,
type='folder',
size=None,
modified=stat.st_mtime,
uploaded_by=admin_user.id,
uploaded_at=datetime.utcfromtimestamp(stat.st_mtime)
)
db.session.add(rf)
# Files
for f in files:
exists = RoomFile.query.filter_by(room_id=room.id, name=f, path=rel_path, type='file').first()
file_path = os.path.join(root, f)
stat = os.stat(file_path)
if not exists:
rf = RoomFile(
room_id=room.id,
name=f,
path=rel_path,
type='file',
size=stat.st_size,
modified=stat.st_mtime,
uploaded_by=admin_user.id,
uploaded_at=datetime.utcfromtimestamp(stat.st_mtime)
)
db.session.add(rf)
db.session.commit()
return jsonify({'success': True, 'message': 'File system synchronized successfully'})
except Exception as e:
db.session.rollback()
return jsonify({'error': str(e)}), 500

View File

@@ -3,6 +3,7 @@
{% from "settings/tabs/colors.html" import colors_tab %}
{% from "settings/tabs/company_info.html" import company_info_tab %}
{% from "settings/tabs/security.html" import security_tab %}
{% from "settings/tabs/debugging.html" import debugging_tab %}
{% from "settings/components/reset_colors_modal.html" import reset_colors_modal %}
{% block title %}Settings - DocuPulse{% endblock %}
@@ -40,6 +41,11 @@
<i class="fas fa-shield-alt me-2"></i>Security
</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link {% if active_tab == 'debugging' %}active{% endif %}" id="debugging-tab" data-bs-toggle="tab" data-bs-target="#debugging" type="button" role="tab" aria-controls="debugging" aria-selected="{{ 'true' if active_tab == 'debugging' else 'false' }}">
<i class="fas fa-bug me-2"></i>Debugging
</button>
</li>
</ul>
</div>
<div class="card-body">
@@ -58,6 +64,11 @@
<div class="tab-pane fade {% if active_tab == 'security' %}show active{% endif %}" id="security" role="tabpanel" aria-labelledby="security-tab">
{{ security_tab() }}
</div>
<!-- Debugging Tab -->
<div class="tab-pane fade {% if active_tab == 'debugging' %}show active{% endif %}" id="debugging" role="tabpanel" aria-labelledby="debugging-tab">
{{ debugging_tab() }}
</div>
</div>
</div>
</div>

View File

@@ -0,0 +1,63 @@
{% macro debugging_tab() %}
<div class="debugging-tab">
<div class="card shadow-sm mb-4">
<div class="card-body">
<div class="d-flex align-items-center gap-3 mb-3">
<button type="button" id="syncFilesBtn" class="btn btn-primary d-flex align-items-center gap-2" style="background-color:var(--primary-color); border:1px solid var(--primary-color);" onmouseover="this.style.backgroundColor='var(--primary-light)'" onmouseout="this.style.backgroundColor='var(--primary-color)'">
<i class="fas fa-sync-alt"></i> Sync File System
</button>
<div id="syncStatus" class="text-muted small"></div>
</div>
<div class="alert alert-warning">
<i class="fas fa-exclamation-triangle me-2"></i>
<strong>Warning:</strong> The sync operation will scan the entire file system and update the database records. This may take some time depending on the number of files.
</div>
</div>
</div>
</div>
<script>
document.getElementById('syncFilesBtn').addEventListener('click', async function() {
const btn = this;
const status = document.getElementById('syncStatus');
// Disable button and show loading state
btn.disabled = true;
btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Syncing...';
status.textContent = 'Syncing file system...';
try {
const response = await fetch('/api/admin/sync-files', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': document.querySelector('meta[name="csrf-token"]').getAttribute('content')
}
});
const data = await response.json();
if (response.ok) {
status.textContent = 'Sync completed successfully!';
status.className = 'text-success small';
} else {
throw new Error(data.error || 'Sync failed');
}
} catch (error) {
status.textContent = error.message;
status.className = 'text-danger small';
} finally {
// Reset button state
btn.disabled = false;
btn.innerHTML = '<i class="fas fa-sync-alt"></i> Sync File System';
// Clear status after 5 seconds
setTimeout(() => {
status.textContent = '';
status.className = 'text-muted small';
}, 5000);
}
});
</script>
{% endmacro %}