resolvendo dependencias dos projetos
Deploy / Deploy (push) Successful in 2m21s Details

This commit is contained in:
JotaChina 2025-12-08 17:23:09 -03:00
parent bc50a42299
commit 7c2b1897ba
7 changed files with 365383 additions and 348953 deletions

711705
_data/all.yml

File diff suppressed because it is too large Load Diff

View File

@ -28,6 +28,7 @@
- classical-sample-of-a-melodic-music-lmms.wav - classical-sample-of-a-melodic-music-lmms.wav
- deep-house-vespertine-feat-georg-no-days-off.wav - deep-house-vespertine-feat-georg-no-days-off.wav
- demo-aesthetescence.wav - demo-aesthetescence.wav
- dr-dre.wav
- dreamhop-animal-l-bonus-r0und-ep.wav - dreamhop-animal-l-bonus-r0und-ep.wav
- drifting-rev-d-csammis-track-1.wav - drifting-rev-d-csammis-track-1.wav
- dubstep-fyrebreak-saichania-original-mix.wav - dubstep-fyrebreak-saichania-original-mix.wav

1871
_data/dr-dre.yml Normal file

File diff suppressed because it is too large Load Diff

View File

@ -11,12 +11,10 @@ permalink: /envie_seu_projeto/
<div class="container"> <div class="container">
<br /> <br />
<!-- 1. MENU DE ABAS (Padrão) -->
<div class="tabs is-centered is-boxed is-medium mb-6"> <div class="tabs is-centered is-boxed is-medium mb-6">
{% include sidebar.html %} {% include sidebar.html %}
</div> </div>
<!-- 2. CABEÇALHO -->
<div class="columns is-centered"> <div class="columns is-centered">
<div class="column is-8 has-text-centered"> <div class="column is-8 has-text-centered">
<h1 class="title is-3 has-text-grey-dark mb-3"> <h1 class="title is-3 has-text-grey-dark mb-3">
@ -30,14 +28,12 @@ permalink: /envie_seu_projeto/
</div> </div>
</div> </div>
<!-- 3. FORMULÁRIO DE UPLOAD -->
<div class="columns is-centered"> <div class="columns is-centered">
<div class="column is-8"> <div class="column is-8">
<div class="box p-5" style="background-color: #f0f8ff; border: 1px solid #cfe8fc; border-radius: 12px;"> <div class="box p-5" style="background-color: #f0f8ff; border: 1px solid #cfe8fc; border-radius: 12px;">
<form id="upload-project-form"> <form id="upload-project-form">
<!-- ÁREA DE ARQUIVO (DROPZONE) -->
<div class="field mb-5"> <div class="field mb-5">
<label class="label has-text-grey-dark">Arquivo do Projeto (LMMS)</label> <label class="label has-text-grey-dark">Arquivo do Projeto (LMMS)</label>
<div class="file-upload-wrapper" id="drop-zone"> <div class="file-upload-wrapper" id="drop-zone">
@ -51,7 +47,6 @@ permalink: /envie_seu_projeto/
</div> </div>
</div> </div>
<!-- INFORMAÇÕES BÁSICAS -->
<div class="columns"> <div class="columns">
<div class="column is-6"> <div class="column is-6">
<div class="field"> <div class="field">
@ -110,7 +105,6 @@ permalink: /envie_seu_projeto/
</div> </div>
</div> </div>
<!-- PREVIEW DE ÁUDIO (OPCIONAL) -->
<div class="field mb-5"> <div class="field mb-5">
<label class="label has-text-grey-dark">Preview de Áudio (Opcional)</label> <label class="label has-text-grey-dark">Preview de Áudio (Opcional)</label>
<div class="file has-name is-fullwidth"> <div class="file has-name is-fullwidth">
@ -132,7 +126,6 @@ permalink: /envie_seu_projeto/
<p class="help">Envie um .mp3 ou .wav para que as pessoas possam ouvir antes de baixar.</p> <p class="help">Envie um .mp3 ou .wav para que as pessoas possam ouvir antes de baixar.</p>
</div> </div>
<!-- BOTÃO DE ENVIO -->
<div class="field mt-5"> <div class="field mt-5">
<div class="control"> <div class="control">
<button type="submit" class="button is-info is-fullwidth is-medium shadow-sm" id="submit-btn"> <button type="submit" class="button is-info is-fullwidth is-medium shadow-sm" id="submit-btn">
@ -153,7 +146,6 @@ permalink: /envie_seu_projeto/
</div> </div>
</main> </main>
<!-- MODAL DE SUCESSO -->
<div id="success-modal" class="modal"> <div id="success-modal" class="modal">
<div class="modal-background"></div> <div class="modal-background"></div>
<div class="modal-card"> <div class="modal-card">
@ -175,6 +167,50 @@ permalink: /envie_seu_projeto/
</div> </div>
</div> </div>
<div id="resolve-modal" class="modal">
<div class="modal-background"></div>
<div class="modal-card">
<header class="modal-card-head has-background-warning-light">
<p class="modal-card-title has-text-warning-dark">Atenção: Arquivos Faltando</p>
<button class="delete" aria-label="close"></button>
</header>
<section class="modal-card-body p-5">
<div class="notification is-warning is-light">
<span class="icon"><i class="fa-solid fa-triangle-exclamation"></i></span>
<strong>Seu projeto usa samples externos!</strong>
<p class="mt-2">Para que o projeto funcione para todos, precisamos dos seguintes arquivos:</p>
</div>
<div class="box">
<ul id="missing-files-list" style="list-style: disc; margin-left: 20px; color: #d63031;">
</ul>
</div>
<form id="resolve-form">
<input type="hidden" name="project_file" id="hidden-project-name">
<div class="field">
<label class="label">Envie os arquivos listados acima:</label>
<div class="file has-name is-fullwidth is-warning">
<label class="file-label">
<input class="file-input" type="file" name="sample_files" multiple required>
<span class="file-cta">
<span class="file-icon"><i class="fa-solid fa-upload"></i></span>
<span class="file-label">Selecionar Samples...</span>
</span>
<span class="file-name" id="resolve-file-name">Nenhum selecionado</span>
</label>
</div>
</div>
<button type="submit" class="button is-warning is-fullwidth mt-4" id="resolve-btn">
Enviar Arquivos e Finalizar
</button>
</form>
</section>
</div>
</div>
<style> <style>
/* Estilo do Dropzone */ /* Estilo do Dropzone */
.file-upload-wrapper { .file-upload-wrapper {
@ -216,105 +252,176 @@ permalink: /envie_seu_projeto/
<script> <script>
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
// 1. Lógica do Dropzone (Visual) // --- ELEMENTOS GERAIS ---
const dropZone = document.getElementById('drop-zone'); const dropZone = document.getElementById('drop-zone');
const fileInput = document.getElementById('project_file'); const fileInput = document.getElementById('project_file');
const fileNameDisplay = document.getElementById('file-name-display'); const fileNameDisplay = document.getElementById('file-name-display');
const dragTextTitle = dropZone.querySelector('.title'); const dragTextTitle = dropZone.querySelector('.title');
// Efeito visual ao arrastar // --- MODAIS ---
['dragenter', 'dragover'].forEach(eventName => { const successModal = document.getElementById('success-modal');
dropZone.addEventListener(eventName, (e) => { const resolveModal = document.getElementById('resolve-modal');
e.preventDefault(); const closeBtns = document.querySelectorAll('.delete, #close-success-modal, .modal-background');
dropZone.classList.add('dragover');
}); // --- FORMULÁRIOS ---
}); const mainForm = document.getElementById('upload-project-form');
['dragleave', 'drop'].forEach(eventName => { const resolveForm = document.getElementById('resolve-form');
dropZone.addEventListener(eventName, (e) => { const submitBtn = document.getElementById('submit-btn');
dropZone.classList.remove('dragover'); const resolveBtn = document.getElementById('resolve-btn');
});
}); // 1. VISUAL DO DROPZONE
['dragenter', 'dragover'].forEach(evt => dropZone.addEventListener(evt, (e) => {
e.preventDefault(); dropZone.classList.add('dragover');
}));
['dragleave', 'drop'].forEach(evt => dropZone.addEventListener(evt, (e) => {
dropZone.classList.remove('dragover');
}));
// Mostrar nome do arquivo selecionado
fileInput.addEventListener('change', () => { fileInput.addEventListener('change', () => {
if (fileInput.files.length > 0) { if (fileInput.files.length > 0) {
const name = fileInput.files[0].name; fileNameDisplay.textContent = fileInput.files[0].name;
fileNameDisplay.textContent = name;
fileNameDisplay.classList.remove('is-hidden'); fileNameDisplay.classList.remove('is-hidden');
dragTextTitle.textContent = "Arquivo Selecionado!"; dragTextTitle.textContent = "Arquivo Pronto!";
dropZone.style.borderColor = "#4caf50"; dropZone.style.borderColor = "#4caf50";
dropZone.style.backgroundColor = "#f0fff4"; dropZone.style.backgroundColor = "#f0fff4";
} }
}); });
// 2. Lógica do Input de Áudio (padrão Bulma) // 2. INPUT DE ÁUDIO & RESOLVE (Nomes de arquivo)
const audioInput = document.querySelector('.file-input'); document.querySelectorAll('.file-input').forEach(input => {
const audioName = document.querySelector('.file-name'); input.addEventListener('change', () => {
if(audioInput) { if (input.files.length > 0) {
audioInput.addEventListener('change', () => { const nameContainer = input.parentElement.querySelector('.file-name');
if (audioInput.files.length > 0) { if (nameContainer) {
audioName.textContent = audioInput.files[0].name; nameContainer.textContent = input.files.length > 1
? `${input.files.length} arquivos selecionados`
: input.files[0].name;
}
} }
}); });
} });
// 3. Simulação de Envio (Modal) // 3. FUNÇÃO DE FECHAR MODAIS
const form = document.getElementById('upload-project-form'); closeBtns.forEach(btn => {
const modal = document.getElementById('success-modal'); btn.addEventListener('click', () => {
const submitBtn = document.getElementById('submit-btn'); successModal.classList.remove('is-active');
const closeBtns = document.querySelectorAll('.delete, #close-success-modal, .modal-background'); resolveModal.classList.remove('is-active');
document.documentElement.classList.remove('is-clipped');
});
});
form.addEventListener('submit', async (e) => { // ============================================================
// 4. SUBMISSÃO PRINCIPAL (PROJETO)
// ============================================================
mainForm.addEventListener('submit', async (e) => {
e.preventDefault(); e.preventDefault();
submitBtn.classList.add('is-loading'); submitBtn.classList.add('is-loading');
const formData = new FormData(form); const formData = new FormData(mainForm);
try { try {
// APONTA PARA O SEU SERVIDOR PYTHON // Rota principal de upload
// Se estiver rodando localmente, geralmente é localhost:5000
// Se for no servidor de produção, coloque o IP ou domínio correto
const response = await fetch('https://alice.ufsj.edu.br:33002/api/upload', { const response = await fetch('https://alice.ufsj.edu.br:33002/api/upload', {
method: 'POST', method: 'POST',
body: formData body: formData
}); });
// Parseamos o JSON independente do status para ler mensagens
const result = await response.json(); const result = await response.json();
if (response.ok) { submitBtn.classList.remove('is-loading');
// Sucesso!
submitBtn.classList.remove('is-loading');
// Atualiza a mensagem do modal se quiser if (response.status === 200) {
const modalTitle = modal.querySelector('.title'); // SUCESSO TOTAL (Status 200)
modalTitle.textContent = "Projeto Processado!"; mostrarSucesso();
mainForm.reset();
resetDropzone();
modal.classList.add('is-active'); } else if (response.status === 202) {
document.documentElement.classList.add('is-clipped'); // SUCESSO PARCIAL (Status 202 - Faltam Samples)
mostrarResolveModal(result);
form.reset();
document.getElementById('file-name-display').classList.add('is-hidden');
document.querySelector('.drag-text .title').textContent = "Arraste e solte o arquivo aqui";
document.getElementById('drop-zone').style.borderColor = "#3273dc";
document.getElementById('drop-zone').style.backgroundColor = "#fff";
} else { } else {
alert('Erro: ' + (result.error || 'Falha desconhecida')); // ERRO (400, 500)
submitBtn.classList.remove('is-loading'); alert('Erro: ' + (result.error || 'Ocorreu um erro desconhecido.'));
} }
} catch (error) { } catch (error) {
console.error('Erro:', error); console.error('Erro:', error);
alert('Erro de conexão com o servidor de processamento.'); alert('Erro de conexão com o servidor.');
submitBtn.classList.remove('is-loading'); submitBtn.classList.remove('is-loading');
} }
}); });
// Fechar Modal // ============================================================
closeBtns.forEach(btn => { // 5. SUBMISSÃO DE RESOLUÇÃO (SAMPLES FALTANDO)
btn.addEventListener('click', () => { // ============================================================
modal.classList.remove('is-active'); resolveForm.addEventListener('submit', async (e) => {
document.documentElement.classList.remove('is-clipped'); e.preventDefault();
}); resolveBtn.classList.add('is-loading');
const formData = new FormData(resolveForm);
try {
// Rota de resolução (envio dos samples faltantes)
const response = await fetch('https://alice.ufsj.edu.br:33002/api/upload/resolve', {
method: 'POST',
body: formData
});
const result = await response.json();
resolveBtn.classList.remove('is-loading');
if (response.ok) {
// Agora sim, tudo certo!
resolveModal.classList.remove('is-active');
mostrarSucesso();
mainForm.reset();
resetDropzone();
} else {
alert('Erro ao enviar samples: ' + (result.error || 'Erro desconhecido'));
}
} catch (error) {
console.error(error);
alert('Erro ao enviar samples.');
resolveBtn.classList.remove('is-loading');
}
}); });
// --- FUNÇÕES AUXILIARES ---
function mostrarSucesso() {
successModal.classList.add('is-active');
document.documentElement.classList.add('is-clipped');
}
function mostrarResolveModal(data) {
// 1. Preenche o input hidden com o nome do projeto (retornado pelo backend)
document.getElementById('hidden-project-name').value = data.project_file;
// 2. Preenche a lista visual de arquivos faltando
const list = document.getElementById('missing-files-list');
list.innerHTML = ''; // Limpa lista anterior
if (data.missing_files && data.missing_files.length > 0) {
data.missing_files.forEach(file => {
const li = document.createElement('li');
li.textContent = file;
list.appendChild(li);
});
}
// 3. Abre o modal
resolveModal.classList.add('is-active');
document.documentElement.classList.add('is-clipped');
}
function resetDropzone() {
fileNameDisplay.classList.add('is-hidden');
dragTextTitle.textContent = "Arraste e solte o arquivo aqui";
dropZone.style.borderColor = "#3273dc";
dropZone.style.backgroundColor = "#fff";
}
}); });
</script> </script>

View File

@ -3,7 +3,6 @@ import os
import logging import logging
from urllib.parse import unquote from urllib.parse import unquote
def load_json(path): def load_json(path):
try: try:
with open(path, "r", encoding="utf-8") as f: with open(path, "r", encoding="utf-8") as f:

View File

@ -127,9 +127,18 @@ def setup_logger():
def process_single_file(args): def process_single_file(args):
""" """
Worker robusto. Worker robusto.
Recebe uma tupla (file_name, clean_slug) pré-calculada para evitar colisão de nomes inválidos. Garante geração de WAV tanto para .mmpz quanto para .mmp.
""" """
file_name, clean_slug = args # === TRATAMENTO DE ENTRADA (Tupla ou String) ===
if isinstance(args, str):
file_name = args
original_base = os.path.splitext(file_name)[0]
clean_slug = slugify(original_base)
if not clean_slug:
clean_slug = f"upload-incorreto-{int(time.time())}"
else:
file_name, clean_slug = args
# ===============================================
file_path = os.path.join(MMP_FOLDER, file_name) file_path = os.path.join(MMP_FOLDER, file_name)
pid = os.getpid() pid = os.getpid()
@ -138,15 +147,18 @@ def process_single_file(args):
# Recupera o nome original base para exibição # Recupera o nome original base para exibição
original_base_name = os.path.splitext(file_name)[0] original_base_name = os.path.splitext(file_name)[0]
# Define os nomes de saída baseados no Slug pré-calculado # Define os nomes de saída baseados no Slug
wav_name = clean_slug + ".wav" wav_name = clean_slug + ".wav"
json_name = clean_slug + ".json" json_name = clean_slug + ".json"
yml_name = clean_slug + ".yml" yml_name = clean_slug + ".yml"
# Variável que vai armazenar o caminho final do arquivo .mmp a ser processado/renderizado
target_mmp_path = ""
try: try:
logging.info(f"[PID {pid}] Processando: {file_name} -> Slug: {clean_slug}") logging.info(f"[PID {pid}] Processando: {file_name} -> Slug: {clean_slug}")
# 1. Tratamento MMPZ # 1. Tratamento e Extração
if file_name.endswith(".mmpz"): if file_name.endswith(".mmpz"):
destination_path = os.path.join(MMPZ_FOLDER, file_name) destination_path = os.path.join(MMPZ_FOLDER, file_name)
@ -154,10 +166,9 @@ def process_single_file(args):
if not os.path.exists(destination_path): if not os.path.exists(destination_path):
shutil.move(file_path, destination_path) shutil.move(file_path, destination_path)
elif os.path.exists(file_path): elif os.path.exists(file_path):
# Se já existe lá, apaga o da origem para não duplicar/confundir
os.remove(file_path) os.remove(file_path)
# Define o nome do arquivo MMP extraído usando o SLUG (padronizado) # Define o nome do arquivo MMP extraído usando o SLUG
mmp_temp_name = clean_slug + ".mmp" mmp_temp_name = clean_slug + ".mmp"
output_mmp_path = os.path.join(MMP_FOLDER, mmp_temp_name) output_mmp_path = os.path.join(MMP_FOLDER, mmp_temp_name)
@ -176,18 +187,28 @@ def process_single_file(args):
check=True, check=True,
env={"QT_QPA_PLATFORM": "offscreen", **os.environ}, env={"QT_QPA_PLATFORM": "offscreen", **os.environ},
) )
# Define o arquivo extraído como o alvo para renderização e parsing
target_mmp_path = output_mmp_path
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
result["error"] = f"Erro no dump MMP: {e.stderr.decode('utf-8')}" result["error"] = f"Erro no dump MMP: {e.stderr.decode('utf-8')}"
logging.error(f"[PID {pid}] {result['error']}") logging.error(f"[PID {pid}] {result['error']}")
return result return result
# Comando para gerar WAV elif file_name.endswith(".mmp"):
# Se já for MMP, ele é o próprio alvo
target_mmp_path = file_path
# 2. GERAÇÃO DE ÁUDIO (AGORA FORA DO IF/ELSE)
# Isso garante que rode tanto para MMPZ (usando o extraído) quanto para MMP puro
if os.path.exists(target_mmp_path):
abs_target_mmp = os.path.abspath(target_mmp_path)
abs_wav_out = os.path.abspath(os.path.join(WAV_FOLDER, wav_name)) abs_wav_out = os.path.abspath(os.path.join(WAV_FOLDER, wav_name))
wav_cmd = [ wav_cmd = [
"lmms", "lmms",
"-r", "-r",
abs_dest, abs_target_mmp, # Usa sempre o .mmp descompactado
"-o", "-o",
abs_wav_out, abs_wav_out,
"-f", "-f",
@ -196,30 +217,24 @@ def process_single_file(args):
try: try:
subprocess.run(wav_cmd, check=True, capture_output=True, text=True) subprocess.run(wav_cmd, check=True, capture_output=True, text=True)
logging.info(f"[PID {pid}] Áudio WAV gerado: {wav_name}") logging.info(f"[PID {pid}] Áudio WAV gerado com sucesso: {wav_name}")
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
logging.warning(f"[PID {pid}] Falha no WAV: {e.stderr}") logging.warning(f"[PID {pid}] Falha na geração do WAV: {e.stderr}")
else:
result["error"] = "Arquivo alvo MMP não encontrado para renderização."
logging.error(f"[PID {pid}] {result['error']}")
return result
# Define qual arquivo será lido pelo parser # 3. Parsing e Salvamento de Dados
file_to_parse = output_mmp_path if os.path.exists(target_mmp_path):
mmp_data = parse_mmp_file(target_mmp_path)
elif file_name.endswith(".mmp"):
# Se já for MMP, usa ele direto
file_to_parse = file_path
# (Opcional) Gerar WAV para .mmp puro se necessário
# Se quiser gerar wav para .mmp puro, a lógica seria similar à acima
# 2. Parsing e Salvamento
if os.path.exists(file_to_parse):
mmp_data = parse_mmp_file(file_to_parse)
if mmp_data: if mmp_data:
# Injeta metadados de padronização # Injeta metadados de padronização
mmp_data["file"] = clean_slug mmp_data["file"] = clean_slug
mmp_data["original_title"] = original_base_name mmp_data["original_title"] = original_base_name
# Salva metadados usando os nomes sanitizados # Salva metadados
save_to_json( save_to_json(
mmp_data, os.path.join(METADATA_FOLDER, json_name) mmp_data, os.path.join(METADATA_FOLDER, json_name)
) )
@ -228,9 +243,7 @@ def process_single_file(args):
result["success"] = True result["success"] = True
result["data"] = mmp_data result["data"] = mmp_data
# === IMPORTANTE: NÃO DELETAMOS MAIS O ARQUIVO MMP === # Mantemos o arquivo .mmp na pasta para referência futura
# O arquivo file_to_parse (que agora é slug.mmp) permanece na pasta.
else: else:
result["error"] = "Dados vazios após parsing." result["error"] = "Dados vazios após parsing."
else: else:
@ -265,7 +278,6 @@ def main_parallel():
return return
# === PRÉ-PROCESSAMENTO DOS SLUGS === # === PRÉ-PROCESSAMENTO DOS SLUGS ===
# Calcula os slugs sequencialmente para garantir contagem correta de nomes inválidos
tasks = [] tasks = []
invalid_count = 0 invalid_count = 0
@ -286,7 +298,7 @@ def main_parallel():
num_cores = multiprocessing.cpu_count() num_cores = multiprocessing.cpu_count()
logging.info(f"Processando {len(tasks)} arquivos com {num_cores} workers.") logging.info(f"Processando {len(tasks)} arquivos com {num_cores} workers.")
# O pool.map agora envia a tupla (arquivo, slug) # O pool.map envia a tupla (arquivo, slug)
with multiprocessing.Pool(processes=num_cores) as pool: with multiprocessing.Pool(processes=num_cores) as pool:
results = pool.map(process_single_file, tasks) results = pool.map(process_single_file, tasks)

View File

@ -1,185 +1,364 @@
import os import os
import subprocess # <--- 1. ADICIONE ISSO import subprocess
import xml.etree.ElementTree as ET
import gzip
import zipfile
import zlib # <--- IMPORTANTE: Adicionado para suporte a mmpz raw
import json
import shutil
import time
from flask import Flask, request, jsonify from flask import Flask, request, jsonify
from flask_cors import CORS from flask_cors import CORS
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
# Importa suas funções e configurações existentes # Importa suas funções e configurações existentes
from main import process_single_file, rebuild_indexes, generate_manifests from main import process_single_file, rebuild_indexes, generate_manifests, slugify
from utils import MMP_FOLDER, MMPZ_FOLDER, CERT_PATH, KEY_PATH, BASE_DATA, SRC_MMPSEARCH, SAMPLE_SRC, METADATA_FOLDER
# 2. ADICIONE BASE_PATH ABAIXO (Assume que utils tem o caminho raiz do projeto)
from utils import MMP_FOLDER, MMPZ_FOLDER, CERT_PATH, KEY_PATH, BASE_DATA, SRC_MMPSEARCH, SAMPLE_SRC
app = Flask(__name__) app = Flask(__name__)
CORS(app) CORS(app)
ALLOWED_EXTENSIONS = {"mmp", "mmpz"} ALLOWED_EXTENSIONS = {"mmp", "mmpz"}
ALLOWED_SAMPLE_EXTENSIONS = {'wav', 'mp3', 'ogg', 'flac', 'ds'}
# --- CONFIGURAÇÕES ---
MANIFEST_PATH = os.path.join(METADATA_FOLDER, "samples-manifest.json")
XML_IMPORTED_PATH_PREFIX = "src_mmpSearch/samples/imported"
PHYSICAL_IMPORTED_FOLDER = os.path.join(SAMPLE_SRC, "imported")
def allowed_file(filename): def allowed_file(filename):
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
def allowed_sample(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_SAMPLE_EXTENSIONS
# --- 3. NOVA FUNÇÃO DE BUILD ---
def run_jekyll_build(): def run_jekyll_build():
"""Roda o comando de build do Jekyll."""
print("Iniciando build do Jekyll...") print("Iniciando build do Jekyll...")
command = ["bundle", "exec", "jekyll", "build", "--destination", "/var/www/html/trens/mmpSearch/"]
# O comando que você pediu, separado em lista (mais seguro)
command = [
"bundle",
"exec",
"jekyll",
"build",
"--destination",
"/var/www/html/trens/mmpSearch/",
]
try: try:
# cwd=BASE_PATH garante que o comando rode na pasta onde está o Gemfile subprocess.run(command, check=True, cwd=BASE_DATA, capture_output=True, text=True)
result = subprocess.run(
command, check=True, cwd=BASE_DATA, capture_output=True, text=True
)
print("Jekyll Build Sucesso!") print("Jekyll Build Sucesso!")
print(result.stdout) # Opcional: ver log do jekyll
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
print(f"ERRO no Jekyll Build: {e.stderr}") print(f"ERRO no Jekyll Build: {e.stderr}")
# Não vamos travar o retorno para o usuário se o site falhar,
# mas logamos o erro no console do servidor.
def load_manifest_keys():
"""Carrega as pastas de fábrica (keys do manifesto)."""
if not os.path.exists(MANIFEST_PATH):
return ["drums", "instruments", "effects", "presets", "samples"]
try:
with open(MANIFEST_PATH, 'r', encoding='utf-8') as f:
data = json.load(f)
return list(data.keys())
except Exception:
return []
def convert_mmpz_to_mmp(mmpz_path, mmp_target_path):
"""
Tenta descompactar .mmpz usando métodos Python (rápido) e falha para o CLI do LMMS (robusto).
Salva o resultado em mmp_target_path.
"""
print(f"Tentando converter: {mmpz_path} -> {mmp_target_path}")
# --- TENTATIVA 1: Métodos Python (Memória/Rápido) ---
content = None
try:
with open(mmpz_path, "rb") as f:
content = f.read()
except Exception:
pass
if content:
# 1.1 Tenta ZIP
if zipfile.is_zipfile(mmpz_path):
try:
with zipfile.ZipFile(mmpz_path, 'r') as z:
with open(mmp_target_path, 'wb') as f_out:
f_out.write(z.read(z.namelist()[0]))
print("Sucesso: Descompactado via ZIP.")
return True
except: pass
# 1.2 Tenta GZIP
try:
decompressed = gzip.decompress(content)
with open(mmp_target_path, "wb") as f_out:
f_out.write(decompressed)
print("Sucesso: Descompactado via GZIP.")
return True
except: pass
# 1.3 Tenta ZLIB (Qt Default)
try:
decompressed = zlib.decompress(content)
with open(mmp_target_path, "wb") as f_out:
f_out.write(decompressed)
print("Sucesso: Descompactado via ZLIB.")
return True
except: pass
# --- TENTATIVA 2: Fallback para LMMS CLI (Lento/Robusto) ---
# Se o Python falhar, pedimos ao próprio LMMS para converter
print("Métodos Python falharam. Tentando fallback via LMMS CLI...")
cmd = ["lmms", "--dump", mmpz_path]
# Define ambiente sem interface gráfica para evitar erros no servidor
env_vars = os.environ.copy()
env_vars["QT_QPA_PLATFORM"] = "offscreen"
try:
# O LMMS joga o XML no stdout, então capturamos e salvamos no arquivo
with open(mmp_target_path, "w") as f_out:
subprocess.run(
cmd,
stdout=f_out,
stderr=subprocess.PIPE,
check=True,
env=env_vars
)
# Verifica se o arquivo foi criado e tem conteúdo
if os.path.exists(mmp_target_path) and os.path.getsize(mmp_target_path) > 0:
print("Sucesso: Descompactado via LMMS CLI (--dump).")
return True
else:
print("Erro: LMMS CLI rodou, mas arquivo de saída está vazio.")
return False
except subprocess.CalledProcessError as e:
print(f"Falha crítica no LMMS CLI: {e.stderr.decode('utf-8') if e.stderr else str(e)}")
return False
except Exception as e:
print(f"Erro inesperado ao chamar LMMS: {e}")
return False
def analyze_mmp_dependencies(mmp_filename):
"""
Analisa um arquivo .mmp (que deve ser texto XML puro).
Retorna: (is_clean, missing_files_list)
"""
filepath = os.path.join(MMP_FOLDER, mmp_filename)
if not os.path.exists(filepath):
print(f"Erro: Arquivo não encontrado: {filepath}")
return False, []
try:
tree = ET.parse(filepath)
except ET.ParseError as e:
print(f"Erro Fatal: O arquivo {mmp_filename} não é um XML válido: {e}")
return False, []
root = tree.getroot()
factory_folders = load_manifest_keys()
missing_samples = set()
# Varre todos os audiofileprocessor
for audio_node in root.findall(".//audiofileprocessor"):
src = audio_node.get('src', '')
if not src:
continue
# 1. É arquivo de fábrica?
is_factory = any(src.startswith(folder + "/") for folder in factory_folders)
# 2. Já foi importado anteriormente?
is_already_imported = src.startswith("src_mmpSearch/samples/imported/")
# Se não é nativo e não está na pasta de importados, é externo
if not is_factory and not is_already_imported:
file_name = os.path.basename(src)
missing_samples.add(file_name)
return len(missing_samples) == 0, list(missing_samples)
def update_xml_paths(mmp_filename, file_map):
"""
Atualiza os caminhos no XML para apontar para a pasta imported.
"""
filepath = os.path.join(MMP_FOLDER, mmp_filename)
try:
tree = ET.parse(filepath)
root = tree.getroot()
factory_folders = load_manifest_keys()
changes_made = False
for audio_node in root.findall(".//audiofileprocessor"):
src = audio_node.get('src', '')
if not src: continue
is_factory = any(src.startswith(folder + "/") for folder in factory_folders)
is_already_imported = src.startswith(XML_IMPORTED_PATH_PREFIX)
if not is_factory and not is_already_imported:
base_name = os.path.basename(src)
# Verifica se temos esse arquivo mapeado (upload do usuário)
if base_name in file_map:
safe_name = file_map[base_name]
new_src = f"{XML_IMPORTED_PATH_PREFIX}/{safe_name}"
audio_node.set('src', new_src)
audio_node.set('vol', audio_node.get('vol', '1'))
changes_made = True
print(f"Path corrigido: {base_name} -> {new_src}")
if changes_made:
tree.write(filepath, encoding='UTF-8', xml_declaration=True)
print(f"Projeto {mmp_filename} atualizado com novos caminhos.")
return True
except Exception as e:
print(f"Erro ao atualizar XML: {e}")
return False
# --- ROTAS ---
@app.route("/api/upload", methods=["POST"]) @app.route("/api/upload", methods=["POST"])
def upload_file(): def upload_file():
# 1. Verificações Básicas
if "project_file" not in request.files: if "project_file" not in request.files:
return jsonify({"error": "Nenhum arquivo enviado"}), 400 return jsonify({"error": "Nenhum arquivo enviado"}), 400
file = request.files["project_file"] file = request.files["project_file"]
if file.filename == "": if file.filename == "":
return jsonify({"error": "Nome do arquivo vazio"}), 400 return jsonify({"error": "Nome do arquivo vazio"}), 400
if file and allowed_file(file.filename): if file and allowed_file(file.filename):
filename = secure_filename(file.filename) # 1. Sanitização do nome
original_name = file.filename
name_without_ext = os.path.splitext(original_name)[0]
ext = os.path.splitext(original_name)[1].lower()
clean_name = slugify(name_without_ext)
if not clean_name:
clean_name = f"upload-{int(time.time())}"
if filename.endswith(".mmpz"): # O nome final no sistema será sempre .mmp
save_path = os.path.join(MMPZ_FOLDER, filename) final_mmp_filename = f"{clean_name}.mmp"
final_mmp_path = os.path.join(MMP_FOLDER, final_mmp_filename)
# Caminho temporário para o upload original
upload_filename = f"{clean_name}{ext}"
if ext == ".mmpz":
upload_path = os.path.join(MMPZ_FOLDER, upload_filename)
else: else:
save_path = os.path.join(MMP_FOLDER, filename) upload_path = os.path.join(MMP_FOLDER, upload_filename)
try: try:
file.save(save_path) # Salva o arquivo original
print(f"Arquivo salvo em: {save_path}") file.save(upload_path)
print(f"Upload salvo em: {upload_path}")
# 2. SE FOR MMPZ, CONVERTE IMEDIATAMENTE PARA MMP
if ext == ".mmpz":
print("Detectado arquivo compactado. Iniciando extração...")
success = convert_mmpz_to_mmp(upload_path, final_mmp_path)
if not success:
# Se falhou zlib/gzip, tenta o fallback do sistema (lmms --dump) se disponível
# Mas por segurança, retornamos erro aqui para não travar
return jsonify({"error": "Falha crítica: O arquivo .mmpz não pode ser descompactado. Tente enviar o .mmp descomprimido."}), 400
# Opcional: Remover o .mmpz original já que já extraímos
# os.remove(upload_path)
# Se já for .mmp, apenas garante que está no lugar certo
elif ext == ".mmp" and upload_path != final_mmp_path:
shutil.move(upload_path, final_mmp_path)
# A partir daqui, TRABALHAMOS APENAS COM O ARQUIVO .MMP (final_mmp_filename)
# 3. Analisa dependências no arquivo .mmp limpo
is_clean, missing_files = analyze_mmp_dependencies(final_mmp_filename)
if not is_clean:
return jsonify({
"status": "missing_samples",
"message": "Samples externos detectados.",
"project_file": final_mmp_filename, # Retornamos o nome do MMP, pois é ele que vamos editar
"missing_files": missing_files
}), 202
# 4. Se limpo, processa
return process_and_build(final_mmp_filename)
except Exception as e: except Exception as e:
return jsonify({"error": f"Falha ao salvar disco: {str(e)}"}), 500 import traceback
traceback.print_exc()
# 3. DISPARA SEU PIPELINE return jsonify({"error": f"Erro interno: {str(e)}"}), 500
result = process_single_file(filename)
if result["success"]:
# 4. Atualiza o site.data.all (all.json)
rebuild_indexes()
# --- 5. AQUI ESTÁ A MÁGICA ---
# Só roda se o processamento e o indice derem certo
run_jekyll_build()
# -----------------------------
# Geração de Manifestos
try:
manifest_report = generate_manifests(SRC_MMPSEARCH)
except Exception as e:
manifest_report = {"generated": [], "failed": [str(e)]}
return jsonify(
{
"message": "Projeto processado e Site Atualizado!",
"data": result["data"],
}
), 200
else:
return jsonify({"error": f"Erro no processamento: {result['error']}"}), 500
return jsonify({"error": "Tipo de arquivo não permitido"}), 400 return jsonify({"error": "Tipo de arquivo não permitido"}), 400
ALLOWED_SAMPLE_EXTENSIONS = {'wav', 'mp3', 'ogg', 'flac'}
def allowed_sample(filename): @app.route("/api/upload/resolve", methods=["POST"])
return '.' in filename and \ def resolve_samples():
filename.rsplit('.', 1)[1].lower() in ALLOWED_SAMPLE_EXTENSIONS """
Recebe o nome do arquivo .mmp ( existente no servidor) e os samples.
"""
project_filename = request.form.get('project_file') # Espera o nome .mmp
uploaded_files = request.files.getlist("sample_files")
if not project_filename or not uploaded_files:
return jsonify({"error": "Dados incompletos"}), 400
# Garante pasta
os.makedirs(PHYSICAL_IMPORTED_FOLDER, exist_ok=True)
os.chmod(PHYSICAL_IMPORTED_FOLDER, 0o775)
file_map = {}
# 1. Salva os samples
for file in uploaded_files:
if file and allowed_sample(file.filename):
clean_sample_name = secure_filename(file.filename)
save_path = os.path.join(PHYSICAL_IMPORTED_FOLDER, clean_sample_name)
file.save(save_path)
os.chmod(save_path, 0o664)
# Mapeia nome original -> nome salvo
file_map[file.filename] = clean_sample_name
file_map[clean_sample_name] = clean_sample_name # Fallback
# 2. Atualiza o XML (.mmp)
if update_xml_paths(project_filename, file_map):
# 3. Processa e Builda
return process_and_build(project_filename)
else:
return jsonify({"error": "Falha ao atualizar referências no projeto."}), 500
def process_and_build(filename):
"""Encapsula a chamada do main.py"""
result = process_single_file(filename)
if result["success"]:
rebuild_indexes()
run_jekyll_build()
generate_manifests(SRC_MMPSEARCH)
return jsonify({
"message": "Sucesso! Projeto processado.",
"data": result["data"]
}), 200
else:
return jsonify({"error": f"Erro no processamento: {result['error']}"}), 500
# Rota avulsa de sample mantida igual...
@app.route('/api/upload/sample', methods=['POST']) @app.route('/api/upload/sample', methods=['POST'])
def upload_sample(): def upload_sample_standalone():
if 'sample_file' not in request.files: if 'sample_file' not in request.files: return jsonify({'error': 'Nenhum arquivo'}), 400
return jsonify({'error': 'Nenhum arquivo enviado'}), 400
file = request.files['sample_file'] file = request.files['sample_file']
# Pega a subpasta (opcional), ex: "Drums/Kicks"
subfolder = request.form.get('subfolder', '').strip() subfolder = request.form.get('subfolder', '').strip()
if file.filename == '':
return jsonify({'error': 'Nome do arquivo vazio'}), 400
if file and allowed_sample(file.filename): if file and allowed_sample(file.filename):
filename = secure_filename(file.filename) filename = secure_filename(file.filename)
# Define o caminho final (Raiz de samples + Subpasta)
# Evita "directory traversal" removendo ..
safe_subfolder = subfolder.replace('..', '').strip('/') safe_subfolder = subfolder.replace('..', '').strip('/')
target_dir = os.path.join(SAMPLE_SRC, safe_subfolder) target_dir = os.path.join(SAMPLE_SRC, safe_subfolder)
os.makedirs(target_dir, exist_ok=True)
# Cria a pasta se não existir file.save(os.path.join(target_dir, filename))
if not os.path.exists(target_dir): generate_manifests(SRC_MMPSEARCH)
try: run_jekyll_build()
os.makedirs(target_dir, exist_ok=True) return jsonify({'message': 'Sample enviado!'}), 200
# Garante permissão para o grupo www-data return jsonify({'error': 'Erro no arquivo'}), 400
os.system(f"chmod -R 775 {target_dir}")
except Exception as e:
return jsonify({'error': f"Erro ao criar pasta: {str(e)}"}), 500
save_path = os.path.join(target_dir, filename)
try:
file.save(save_path)
# Garante permissão do arquivo
os.chmod(save_path, 0o664)
print(f"Sample salvo em: {save_path}")
# 1. Regenerar o manifesto (samples-manifest.json)
# Passamos SRC_MMPSEARCH pois o generate_manifests espera a raiz do projeto
generate_manifests(SRC_MMPSEARCH)
# 2. Reconstruir o site (para o Jekyll ler o novo JSON)
run_jekyll_build()
return jsonify({'message': 'Sample enviado e biblioteca atualizada!'}), 200
except Exception as e:
return jsonify({'error': f"Falha ao processar: {str(e)}"}), 500
return jsonify({'error': 'Tipo de arquivo não permitido (apenas wav, mp3, ogg, flac)'}), 400
if __name__ == "__main__": if __name__ == "__main__":
print("Iniciando Servidor de Upload MMP...") context = (CERT_PATH, KEY_PATH) if os.path.exists(CERT_PATH) else "adhoc"
app.run(host="0.0.0.0", port=33002, ssl_context=context, debug=True)
# Opção 1: Usar certificado real (O ideal - Navegador aceita de primeira)
if os.path.exists(CERT_PATH) and os.path.exists(KEY_PATH):
print("Certificados encontrados! Rodando HTTPS seguro na porta 33002.")
context = (CERT_PATH, KEY_PATH)
# Use host='0.0.0.0' para garantir que escute externamente
app.run(host="0.0.0.0", port=33002, ssl_context=context, debug=True)
# Opção 2: Modo 'adhoc' (Certificado temporário gerado na hora)
else:
print(
"Certificados reais não encontrados. Usando modo 'adhoc' (Auto-assinado)."
)
print(
"ATENÇÃO: O navegador vai avisar que 'Não é seguro'. Você precisará autorizar."
)
# Requer: pip install pyopenssl
try:
app.run(host="0.0.0.0", port=33002, ssl_context="adhoc", debug=True)
except Exception as e:
print(f"Erro ao iniciar SSL adhoc: {e}")
print("Tentando rodar em HTTP simples (pode dar erro de Mixed Content)...")
app.run(host="0.0.0.0", port=33002, debug=True)