From c4708163ac9e8157f9c875fb201cfd05f6419c70 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sat, 25 Mar 2023 15:42:11 +0100 Subject: stop sqlite3 DB creation if it doesnt exist This would otherwise happen if e.g. the user has the page open, the SQLite file is deleted, the user then activates the overlay layer, and the sqlite3.connect() creates the database. --- fietsboek/views/profile.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/fietsboek/views/profile.py b/fietsboek/views/profile.py index a08fad7..9f68d2f 100644 --- a/fietsboek/views/profile.py +++ b/fietsboek/views/profile.py @@ -1,6 +1,7 @@ """Endpoints for the user profile pages.""" import datetime import sqlite3 +import urllib.parse from dataclasses import dataclass from typing import Optional @@ -192,7 +193,16 @@ def user_tile(request: Request) -> Response: if path is None: return HTTPNotFound() - connection = sqlite3.connect(path) + # See + # https://docs.python.org/3/library/sqlite3.html#how-to-work-with-sqlite-uris + # https://stackoverflow.com/questions/10205744/opening-sqlite3-database-from-python-in-read-only-mode + # https://stackoverflow.com/questions/17170202/dont-want-to-create-a-new-database-if-it-doesnt-already-exists + sqlite_uri = urllib.parse.urlunparse(("file", "", str(path), "", "mode=ro", "")) + try: + connection = sqlite3.connect(sqlite_uri, uri=True) + except sqlite3.OperationalError: + return HTTPNotFound() + cursor = connection.cursor() result = cursor.execute( "SELECT data FROM tiles WHERE zoom = ? AND x = ? AND y = ?;", -- cgit v1.2.3