Ошибка при открытии скачанного файла Django

Я столкнулся с такой проблемой что, когда я скачиваю с Django sqlite3 формат при открытии его выдает ошибку database disk image is malformed но если я открою еще не отправленную базы из папки хранения то оно работает без ошибки

Как я понял что-то происходит во время отправки

views.py

import mimetypes
import os
import sqlite3

import psycopg2
from django.http.response import HttpResponse
from environs import Env


def download_file(request):
    # Определить базовый каталог проекта Django
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    # Определяем имя текстового файла
    filename = 'dumpdata.sqlite3'
    env = Env()
    env.read_env()
    gre = psycopg2.connect(
        database=env.str('POSTGRES_DB'),
        user=env.str('POSTGRES_USER'),
        password=env.str('POSTGRES_PASSWORD'),
        host=env.str('POSTGRES_HOST'),
        port=env.str('POSTGRES_PORT')
    )
    cur_gre = gre.cursor()

    try:
        os.remove(f"db_file/{filename}")
    except FileNotFoundError:
        pass
    conn = sqlite3.connect(f"db_file/{filename}")
    cursor = conn.cursor()
    cursor.execute(
        'CREATE TABLE "main_admin_bot" ("id" bigserial NOT NULL PRIMARY KEY, "tg_id" bigint NOT NULL UNIQUE, "name" varchar(150) NULL);')
    cursor.execute(
        'CREATE TABLE "main_attempts" ("id" bigserial NOT NULL PRIMARY KEY, "ids" integer NOT NULL UNIQUE, "count" integer NOT NULL);')
    cursor.execute(
        'CREATE TABLE "main_blacklist" ("id" bigserial NOT NULL PRIMARY KEY, "words" varchar(250) NOT NULL UNIQUE, "job" boolean NOT NULL);')
    cursor.execute(
        'CREATE TABLE "main_database_sqlite3" ("id" bigserial NOT NULL PRIMARY KEY, "file" varchar(100) NOT NULL);')
    cursor.execute(
        'CREATE TABLE "main_parse_user" ("id" bigserial NOT NULL PRIMARY KEY, "user_id" bigint NULL UNIQUE, "group_id" bigint NULL, "username" varchar(150) NULL, "bio" text NULL, "first_name" varchar(250) NULL);')
    cursor.execute(
        'CREATE TABLE "main_payments" ("id" bigserial NOT NULL PRIMARY KEY, "count" bigint NOT NULL, "slug" varchar(50) NOT NULL, "price" bigint NOT NULL);')
    cursor.execute(
        'CREATE TABLE "main_token" ("id" bigserial NOT NULL PRIMARY KEY, "ids" integer NOT NULL UNIQUE, "bot_token" text NOT NULL);')
    cursor.execute(
        'CREATE TABLE "main_trycountuser" ("id" bigserial NOT NULL PRIMARY KEY, "ids" integer NOT NULL UNIQUE, "count" integer NOT NULL);')
    cursor.execute(
        'CREATE TABLE "main_user" ("id" bigserial NOT NULL PRIMARY KEY, "tg_id" bigint NOT NULL UNIQUE, "username" varchar(150) NULL, "name" varchar(150) NULL, "try_count" bigint NULL, "activate" boolean NOT NULL);')
    cursor.execute(
        'CREATE TABLE "main_channel" ("id" bigserial NOT NULL PRIMARY KEY, "channel_id" integer NOT NULL UNIQUE);')
    conn.commit()
    # Данные администратора
    # Define the full file path
    filepath = BASE_DIR + '/db_file/' + filename
    # Open the file for reading content
    path = filepath
    # Set the mime type
    # mime_type, _ = mimetypes.guess_type(filepath)
    # Set the return value of the HttpResponse
    response = HttpResponse(path)
    # Set the HTTP header for sending to browser
    response['Content-Disposition'] = "attachment; filename=%s" % filename
    # Return the response value
    return response

Вернуться на верх