att
This commit is contained in:
parent
143fa311a9
commit
a890b418f9
@ -39,13 +39,33 @@ def check_storage_access():
|
||||
out_dir = normalize_path(OUTPUT_DIR_BASE)
|
||||
|
||||
try:
|
||||
os.makedirs(out_dir, exist_ok=True)
|
||||
# Criar diretório com permissões explícitas
|
||||
os.makedirs(out_dir, mode=0o777, exist_ok=True)
|
||||
print(f"✅ Diretório de saída pronto: {out_dir}")
|
||||
|
||||
# Verificar permissões de escrita
|
||||
test_file = os.path.join(out_dir, ".test_write")
|
||||
try:
|
||||
with open(test_file, "w") as f:
|
||||
f.write("test")
|
||||
os.remove(test_file)
|
||||
print(f"✅ Permissões de escrita verificadas")
|
||||
except Exception as e:
|
||||
print(f"⚠️ Aviso: Problema ao testar escrita: {e}")
|
||||
|
||||
# Mostrar informações do diretório
|
||||
if os.path.exists(out_dir):
|
||||
stat_info = os.stat(out_dir)
|
||||
print(f" 📊 Permissões: {oct(stat_info.st_mode)[-3:]}")
|
||||
print(f" 👤 UID: {stat_info.st_uid}, GID: {stat_info.st_gid}")
|
||||
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"❌ Erro ao criar diretório {out_dir}: {e}")
|
||||
if not IS_WINDOWS:
|
||||
print("⚠️ ATENÇÃO: Verifique permissões no /mnt/contabilidade")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
# 1️⃣ Token
|
||||
@ -168,21 +188,51 @@ def insert_record(cursor, doc):
|
||||
|
||||
def download_pdf(token, franchise_id, doc_id, image_name, invoice_date):
|
||||
try:
|
||||
print(f"📥 Baixando PDF: {image_name} (Data: {invoice_date})")
|
||||
|
||||
# Obter URL do S3
|
||||
url = f"https://sf-fiscal-api.grupoboticario.digital/v1/handle-images/NDEB/{franchise_id}/{doc_id}/{image_name}/download"
|
||||
r = requests.get(url, headers={"authorization": token})
|
||||
r.raise_for_status()
|
||||
s3_url = r.text.strip()
|
||||
pasta = os.path.join(OUTPUT_DIR_BASE, str(invoice_date))
|
||||
os.makedirs(pasta, exist_ok=True)
|
||||
file_path = os.path.join(pasta, image_name)
|
||||
print(f" 🔗 URL S3 obtida: {s3_url[:50]}...")
|
||||
|
||||
# Normalizar caminho da pasta
|
||||
pasta = normalize_path(os.path.join(OUTPUT_DIR_BASE, str(invoice_date)))
|
||||
print(f" 📁 Criando pasta: {pasta}")
|
||||
|
||||
# Criar pasta com permissões explícitas
|
||||
os.makedirs(pasta, mode=0o777, exist_ok=True)
|
||||
|
||||
# Verificar se a pasta foi criada
|
||||
if not os.path.exists(pasta):
|
||||
raise Exception(f"Pasta não foi criada: {pasta}")
|
||||
|
||||
# Normalizar caminho do arquivo
|
||||
file_path = normalize_path(os.path.join(pasta, image_name))
|
||||
print(f" 💾 Salvando em: {file_path}")
|
||||
|
||||
# Baixar PDF
|
||||
pdf = requests.get(s3_url, stream=True)
|
||||
if pdf.status_code == 200:
|
||||
with open(file_path, "wb") as f:
|
||||
for chunk in pdf.iter_content(8192):
|
||||
f.write(chunk)
|
||||
print(f"📥 PDF salvo: {file_path}")
|
||||
if chunk:
|
||||
f.write(chunk)
|
||||
|
||||
# Verificar se o arquivo foi salvo
|
||||
if os.path.exists(file_path):
|
||||
file_size = os.path.getsize(file_path)
|
||||
print(f" ✅ PDF salvo com sucesso: {file_path} ({file_size} bytes)")
|
||||
else:
|
||||
print(f" ⚠️ Arquivo não encontrado após salvar: {file_path}")
|
||||
else:
|
||||
print(f" ❌ Erro ao baixar PDF do S3: Status {pdf.status_code}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Erro ao baixar {image_name}: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
# 🚀 Main
|
||||
def main():
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user