anki/qt/aqt/mediasrv.py

550 lines
16 KiB
Python
Raw Normal View History

2019-02-05 04:59:03 +01:00
# Copyright: Ankitects Pty Ltd and contributors
2016-07-07 15:39:48 +02:00
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
from __future__ import annotations
Replaced the mediasrv.py SimpleHttp server by flask and waitress, fixing HTML5 media support. https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server https://stackoverflow.com/questions/5052635/what-is-relation-between-content-length-and-byte-ranges-in-http-1-1 https://stackoverflow.com/questions/16725907/google-app-engine-serving-mp3-for-audio-element-needs-content-range-header I was trying to use HTML5 audio tag to display audios like: ```html <audio id="elem_audio" src="myfile.mp3" controls></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063321-565b5500-7c77-11ea-9f8d-6e1df6f07892.png) But the progress bar seek was not working. After researching, I found the problem was the HTML server not properly responding to the HTML5 header requests. The HTML server should respond to quite complicated things as 206 partial, properly handle keep-alive, provide media ranges and other HTTP headers: https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime To implement all these on the Simple HTTP server would be quite complicated. Then, instead, I imported the `flask` web server, which is quite simple and straight forward to use. Now, the back-end is using a secure complaint HTTP back-end: 1. https://palletsprojects.com/p/flask/ > Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. > > Flask offers suggestions, but doesn't enforce any dependencies or project layout. It is up to the developer to choose the tools and libraries they want to use. There are many extensions provided by the community that make adding new functionality easy. 1. https://docs.pylonsproject.org/projects/waitress/en/latest/ > Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 2.7+ and Python 3.5+. It is also known to run on PyPy 1.6.0 on UNIX. It supports HTTP/1.0 and HTTP/1.1. Right now, anki does not support fields passing file names directly to HTML audio tags, but this can be easily done with (https://github.com/ankitects/anki/pull 540 - Added arguments to the sound tag) plus the commit https://github.com/evandroforks/anki/commit/826a97df61b99814041c41c0f2c84268280ed8ad, the HTML5 audio tag can be used like this: ```html // Audio = [sound:myfile.mp3|onlyfilename] <audio id="elem_audio" src="{{Audio}}" controls controlsList="nodownload"></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063736-c539ad80-7c79-11ea-8420-40b72185f4e7.png) # Conflicts: # qt/aqt/mediasrv.py
2020-04-29 12:38:35 +02:00
import logging
import mimetypes
Replaced the mediasrv.py SimpleHttp server by flask and waitress, fixing HTML5 media support. https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server https://stackoverflow.com/questions/5052635/what-is-relation-between-content-length-and-byte-ranges-in-http-1-1 https://stackoverflow.com/questions/16725907/google-app-engine-serving-mp3-for-audio-element-needs-content-range-header I was trying to use HTML5 audio tag to display audios like: ```html <audio id="elem_audio" src="myfile.mp3" controls></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063321-565b5500-7c77-11ea-9f8d-6e1df6f07892.png) But the progress bar seek was not working. After researching, I found the problem was the HTML server not properly responding to the HTML5 header requests. The HTML server should respond to quite complicated things as 206 partial, properly handle keep-alive, provide media ranges and other HTTP headers: https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime To implement all these on the Simple HTTP server would be quite complicated. Then, instead, I imported the `flask` web server, which is quite simple and straight forward to use. Now, the back-end is using a secure complaint HTTP back-end: 1. https://palletsprojects.com/p/flask/ > Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. > > Flask offers suggestions, but doesn't enforce any dependencies or project layout. It is up to the developer to choose the tools and libraries they want to use. There are many extensions provided by the community that make adding new functionality easy. 1. https://docs.pylonsproject.org/projects/waitress/en/latest/ > Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 2.7+ and Python 3.5+. It is also known to run on PyPy 1.6.0 on UNIX. It supports HTTP/1.0 and HTTP/1.1. Right now, anki does not support fields passing file names directly to HTML audio tags, but this can be easily done with (https://github.com/ankitects/anki/pull 540 - Added arguments to the sound tag) plus the commit https://github.com/evandroforks/anki/commit/826a97df61b99814041c41c0f2c84268280ed8ad, the HTML5 audio tag can be used like this: ```html // Audio = [sound:myfile.mp3|onlyfilename] <audio id="elem_audio" src="{{Audio}}" controls controlsList="nodownload"></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063736-c539ad80-7c79-11ea-8420-40b72185f4e7.png) # Conflicts: # qt/aqt/mediasrv.py
2020-04-29 12:38:35 +02:00
import os
2019-12-20 10:19:03 +01:00
import re
Replaced the mediasrv.py SimpleHttp server by flask and waitress, fixing HTML5 media support. https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server https://stackoverflow.com/questions/5052635/what-is-relation-between-content-length-and-byte-ranges-in-http-1-1 https://stackoverflow.com/questions/16725907/google-app-engine-serving-mp3-for-audio-element-needs-content-range-header I was trying to use HTML5 audio tag to display audios like: ```html <audio id="elem_audio" src="myfile.mp3" controls></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063321-565b5500-7c77-11ea-9f8d-6e1df6f07892.png) But the progress bar seek was not working. After researching, I found the problem was the HTML server not properly responding to the HTML5 header requests. The HTML server should respond to quite complicated things as 206 partial, properly handle keep-alive, provide media ranges and other HTTP headers: https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime To implement all these on the Simple HTTP server would be quite complicated. Then, instead, I imported the `flask` web server, which is quite simple and straight forward to use. Now, the back-end is using a secure complaint HTTP back-end: 1. https://palletsprojects.com/p/flask/ > Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. > > Flask offers suggestions, but doesn't enforce any dependencies or project layout. It is up to the developer to choose the tools and libraries they want to use. There are many extensions provided by the community that make adding new functionality easy. 1. https://docs.pylonsproject.org/projects/waitress/en/latest/ > Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 2.7+ and Python 3.5+. It is also known to run on PyPy 1.6.0 on UNIX. It supports HTTP/1.0 and HTTP/1.1. Right now, anki does not support fields passing file names directly to HTML audio tags, but this can be easily done with (https://github.com/ankitects/anki/pull 540 - Added arguments to the sound tag) plus the commit https://github.com/evandroforks/anki/commit/826a97df61b99814041c41c0f2c84268280ed8ad, the HTML5 audio tag can be used like this: ```html // Audio = [sound:myfile.mp3|onlyfilename] <audio id="elem_audio" src="{{Audio}}" controls controlsList="nodownload"></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063736-c539ad80-7c79-11ea-8420-40b72185f4e7.png) # Conflicts: # qt/aqt/mediasrv.py
2020-04-29 12:38:35 +02:00
import sys
2019-12-20 10:19:03 +01:00
import threading
2020-07-07 05:28:30 +02:00
import time
Replaced the mediasrv.py SimpleHttp server by flask and waitress, fixing HTML5 media support. https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server https://stackoverflow.com/questions/5052635/what-is-relation-between-content-length-and-byte-ranges-in-http-1-1 https://stackoverflow.com/questions/16725907/google-app-engine-serving-mp3-for-audio-element-needs-content-range-header I was trying to use HTML5 audio tag to display audios like: ```html <audio id="elem_audio" src="myfile.mp3" controls></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063321-565b5500-7c77-11ea-9f8d-6e1df6f07892.png) But the progress bar seek was not working. After researching, I found the problem was the HTML server not properly responding to the HTML5 header requests. The HTML server should respond to quite complicated things as 206 partial, properly handle keep-alive, provide media ranges and other HTTP headers: https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime To implement all these on the Simple HTTP server would be quite complicated. Then, instead, I imported the `flask` web server, which is quite simple and straight forward to use. Now, the back-end is using a secure complaint HTTP back-end: 1. https://palletsprojects.com/p/flask/ > Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. > > Flask offers suggestions, but doesn't enforce any dependencies or project layout. It is up to the developer to choose the tools and libraries they want to use. There are many extensions provided by the community that make adding new functionality easy. 1. https://docs.pylonsproject.org/projects/waitress/en/latest/ > Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 2.7+ and Python 3.5+. It is also known to run on PyPy 1.6.0 on UNIX. It supports HTTP/1.0 and HTTP/1.1. Right now, anki does not support fields passing file names directly to HTML audio tags, but this can be easily done with (https://github.com/ankitects/anki/pull 540 - Added arguments to the sound tag) plus the commit https://github.com/evandroforks/anki/commit/826a97df61b99814041c41c0f2c84268280ed8ad, the HTML5 audio tag can be used like this: ```html // Audio = [sound:myfile.mp3|onlyfilename] <audio id="elem_audio" src="{{Audio}}" controls controlsList="nodownload"></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063736-c539ad80-7c79-11ea-8420-40b72185f4e7.png) # Conflicts: # qt/aqt/mediasrv.py
2020-04-29 12:38:35 +02:00
import traceback
from dataclasses import dataclass
from http import HTTPStatus
from typing import Callable
2016-07-07 15:39:48 +02:00
Replaced the mediasrv.py SimpleHttp server by flask and waitress, fixing HTML5 media support. https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server https://stackoverflow.com/questions/5052635/what-is-relation-between-content-length-and-byte-ranges-in-http-1-1 https://stackoverflow.com/questions/16725907/google-app-engine-serving-mp3-for-audio-element-needs-content-range-header I was trying to use HTML5 audio tag to display audios like: ```html <audio id="elem_audio" src="myfile.mp3" controls></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063321-565b5500-7c77-11ea-9f8d-6e1df6f07892.png) But the progress bar seek was not working. After researching, I found the problem was the HTML server not properly responding to the HTML5 header requests. The HTML server should respond to quite complicated things as 206 partial, properly handle keep-alive, provide media ranges and other HTTP headers: https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime To implement all these on the Simple HTTP server would be quite complicated. Then, instead, I imported the `flask` web server, which is quite simple and straight forward to use. Now, the back-end is using a secure complaint HTTP back-end: 1. https://palletsprojects.com/p/flask/ > Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. > > Flask offers suggestions, but doesn't enforce any dependencies or project layout. It is up to the developer to choose the tools and libraries they want to use. There are many extensions provided by the community that make adding new functionality easy. 1. https://docs.pylonsproject.org/projects/waitress/en/latest/ > Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 2.7+ and Python 3.5+. It is also known to run on PyPy 1.6.0 on UNIX. It supports HTTP/1.0 and HTTP/1.1. Right now, anki does not support fields passing file names directly to HTML audio tags, but this can be easily done with (https://github.com/ankitects/anki/pull 540 - Added arguments to the sound tag) plus the commit https://github.com/evandroforks/anki/commit/826a97df61b99814041c41c0f2c84268280ed8ad, the HTML5 audio tag can be used like this: ```html // Audio = [sound:myfile.mp3|onlyfilename] <audio id="elem_audio" src="{{Audio}}" controls controlsList="nodownload"></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063736-c539ad80-7c79-11ea-8420-40b72185f4e7.png) # Conflicts: # qt/aqt/mediasrv.py
2020-04-29 12:38:35 +02:00
import flask
import flask_cors # type: ignore
from flask import Response, request
from waitress.server import create_server
Replaced the mediasrv.py SimpleHttp server by flask and waitress, fixing HTML5 media support. https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server https://stackoverflow.com/questions/5052635/what-is-relation-between-content-length-and-byte-ranges-in-http-1-1 https://stackoverflow.com/questions/16725907/google-app-engine-serving-mp3-for-audio-element-needs-content-range-header I was trying to use HTML5 audio tag to display audios like: ```html <audio id="elem_audio" src="myfile.mp3" controls></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063321-565b5500-7c77-11ea-9f8d-6e1df6f07892.png) But the progress bar seek was not working. After researching, I found the problem was the HTML server not properly responding to the HTML5 header requests. The HTML server should respond to quite complicated things as 206 partial, properly handle keep-alive, provide media ranges and other HTTP headers: https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime To implement all these on the Simple HTTP server would be quite complicated. Then, instead, I imported the `flask` web server, which is quite simple and straight forward to use. Now, the back-end is using a secure complaint HTTP back-end: 1. https://palletsprojects.com/p/flask/ > Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. > > Flask offers suggestions, but doesn't enforce any dependencies or project layout. It is up to the developer to choose the tools and libraries they want to use. There are many extensions provided by the community that make adding new functionality easy. 1. https://docs.pylonsproject.org/projects/waitress/en/latest/ > Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 2.7+ and Python 3.5+. It is also known to run on PyPy 1.6.0 on UNIX. It supports HTTP/1.0 and HTTP/1.1. Right now, anki does not support fields passing file names directly to HTML audio tags, but this can be easily done with (https://github.com/ankitects/anki/pull 540 - Added arguments to the sound tag) plus the commit https://github.com/evandroforks/anki/commit/826a97df61b99814041c41c0f2c84268280ed8ad, the HTML5 audio tag can be used like this: ```html // Audio = [sound:myfile.mp3|onlyfilename] <audio id="elem_audio" src="{{Audio}}" controls controlsList="nodownload"></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063736-c539ad80-7c79-11ea-8420-40b72185f4e7.png) # Conflicts: # qt/aqt/mediasrv.py
2020-04-29 12:38:35 +02:00
2021-01-22 14:37:24 +01:00
import aqt
import aqt.main
import aqt.operations
2020-11-09 10:45:14 +01:00
from anki import hooks
from anki._vendor import stringcase
from anki.collection import OpChanges
from anki.decks import DeckConfigsForUpdate, UpdateDeckConfigs
from anki.scheduler.v3 import CustomScheduling
from anki.utils import dev_mode
from aqt.changenotetype import ChangeNotetypeDialog
from aqt.deckoptions import DeckOptionsDialog
Plaintext import/export (#1850) * Add crate csv * Add start of csv importing on backend * Add Menomosyne serializer * Add csv and json importing on backend * Add plaintext importing on frontend * Add csv metadata extraction on backend * Add csv importing with GUI * Fix missing dfa file in build Added compile_data_attr, then re-ran cargo/update.py. * Don't use doubly buffered reader in csv * Escape HTML entities if CSV is not HTML Also use name 'is_html' consistently. * Use decimal number as foreign ease (like '2.5') * ForeignCard.ivl → ForeignCard.interval * Only allow fixed set of CSV delimiters * Map timestamp of ForeignCard to native due time * Don't trim CSV records * Document use of empty strings for defaults * Avoid creating CardGenContexts for every note This requires CardGenContext to be generic, so it works both with an owned and borrowed notetype. * Show all accepted file types in import file picker * Add import_json_file() * factor → ease_factor * delimter_from_value → delimiter_from_value * Map columns to fields, not the other way around * Fallback to current config for csv metadata * Add start of new import csv screen * Temporary fix for compilation issue on Linux/Mac * Disable jest bazel action for import-csv Jest fails with an error code if no tests are available, but this would not be noticable on Windows as Jest is not run there. * Fix field mapping issue * Revert "Temporary fix for compilation issue on Linux/Mac" This reverts commit 21f8a261408cdae49ec031aa21a1b659c4f66d82. * Add HtmlSwitch and move Switch to components * Fix spacing and make selectors consistent * Fix shortcut tooltip * Place import button at the top with path * Fix meta column indices * Remove NotetypeForString * Fix queue and type of foreign cards * Support different dupe resolution strategies * Allow dupe resolution selection when importing CSV * Test import of unnormalized text Close #1863. * Fix logging of foreign notes * Implement CSV exports * Use db_scalar() in notes_table_len() * Rework CSV metadata - Notetypes and decks are either defined by a global id or by a column. - If a notetype id is provided, its field map must also be specified. - If a notetype column is provided, fields are now mapped by index instead of name at import time. So the first non-meta column is used for the first field of every note, regardless of notetype. This makes importing easier and should improve compatiblity with files without a notetype column. - Ensure first field can be mapped to a column. - Meta columns must be defined as `#[meta name]:[column index]` instead of in the `#columns` tag. - Column labels contain the raw names defined by the file and must be prettified by the frontend. * Adjust frontend to new backend column mapping * Add force flags for is_html and delimiter * Detect if CSV is HTML by field content * Update dupe resolution labels * Simplify selectors * Fix coalescence of oneofs in TS * Disable meta columns from selection Plus a lot of refactoring. * Make import button stick to the bottom * Write delimiter and html flag into csv * Refetch field map after notetype change * Fix log labels for csv import * Log notes whose deck/notetype was missing * Fix hiding of empty log queues * Implement adding tags to all notes of a csv * Fix dupe resolution not being set in log * Implement adding tags to updated notes of a csv * Check first note field is not empty * Temporary fix for build on Linux/Mac * Fix inverted html check (dae) * Remove unused ftl string * Delimiter → Separator * Remove commented-out line * Don't accept .json files * Tweak tag ftl strings * Remove redundant blur call * Strip sound and add spaces in csv export * Export HTML by default * Fix unset deck in Mnemosyne import Also accept both numbers and strings for notetypes and decks in JSON. * Make DupeResolution::Update the default * Fix missing dot in extension * Make column indices 1-based * Remove StickContainer from TagEditor Fixes line breaking, border and z index on ImportCsvPage. * Assign different key combos to tag editors * Log all updated duplicates Add a log field for the true number of found notes. * Show identical notes as skipped * Split tag-editor into separate ts module (dae) * Add progress for CSV export * Add progress for text import * Tidy-ups after tag-editor split (dae) - import-csv no longer depends on editor - remove some commented lines
2022-06-01 12:26:16 +02:00
from aqt.import_export.import_csv_dialog import ImportCsvDialog
from aqt.operations.deck import update_deck_configs as update_deck_configs_op
2019-12-20 10:19:03 +01:00
from aqt.qt import *
2019-12-23 01:34:10 +01:00
app = flask.Flask(__name__, root_path="/fake")
Replaced the mediasrv.py SimpleHttp server by flask and waitress, fixing HTML5 media support. https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server https://stackoverflow.com/questions/5052635/what-is-relation-between-content-length-and-byte-ranges-in-http-1-1 https://stackoverflow.com/questions/16725907/google-app-engine-serving-mp3-for-audio-element-needs-content-range-header I was trying to use HTML5 audio tag to display audios like: ```html <audio id="elem_audio" src="myfile.mp3" controls></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063321-565b5500-7c77-11ea-9f8d-6e1df6f07892.png) But the progress bar seek was not working. After researching, I found the problem was the HTML server not properly responding to the HTML5 header requests. The HTML server should respond to quite complicated things as 206 partial, properly handle keep-alive, provide media ranges and other HTTP headers: https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime To implement all these on the Simple HTTP server would be quite complicated. Then, instead, I imported the `flask` web server, which is quite simple and straight forward to use. Now, the back-end is using a secure complaint HTTP back-end: 1. https://palletsprojects.com/p/flask/ > Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. > > Flask offers suggestions, but doesn't enforce any dependencies or project layout. It is up to the developer to choose the tools and libraries they want to use. There are many extensions provided by the community that make adding new functionality easy. 1. https://docs.pylonsproject.org/projects/waitress/en/latest/ > Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 2.7+ and Python 3.5+. It is also known to run on PyPy 1.6.0 on UNIX. It supports HTTP/1.0 and HTTP/1.1. Right now, anki does not support fields passing file names directly to HTML audio tags, but this can be easily done with (https://github.com/ankitects/anki/pull 540 - Added arguments to the sound tag) plus the commit https://github.com/evandroforks/anki/commit/826a97df61b99814041c41c0f2c84268280ed8ad, the HTML5 audio tag can be used like this: ```html // Audio = [sound:myfile.mp3|onlyfilename] <audio id="elem_audio" src="{{Audio}}" controls controlsList="nodownload"></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063736-c539ad80-7c79-11ea-8420-40b72185f4e7.png) # Conflicts: # qt/aqt/mediasrv.py
2020-04-29 12:38:35 +02:00
flask_cors.CORS(app)
2019-12-23 01:34:10 +01:00
@dataclass
class LocalFileRequest:
# base folder, eg media folder
root: str
# path to file relative to root folder
path: str
@dataclass
class BundledFileRequest:
# path relative to aqt data folder
path: str
@dataclass
class NotFound:
message: str
DynamicRequest = Callable[[], Response]
class MediaServer(threading.Thread):
2016-07-07 15:39:48 +02:00
_ready = threading.Event()
daemon = True
def __init__(self, mw: aqt.main.AnkiQt) -> None:
super().__init__()
Replaced the mediasrv.py SimpleHttp server by flask and waitress, fixing HTML5 media support. https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server https://stackoverflow.com/questions/5052635/what-is-relation-between-content-length-and-byte-ranges-in-http-1-1 https://stackoverflow.com/questions/16725907/google-app-engine-serving-mp3-for-audio-element-needs-content-range-header I was trying to use HTML5 audio tag to display audios like: ```html <audio id="elem_audio" src="myfile.mp3" controls></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063321-565b5500-7c77-11ea-9f8d-6e1df6f07892.png) But the progress bar seek was not working. After researching, I found the problem was the HTML server not properly responding to the HTML5 header requests. The HTML server should respond to quite complicated things as 206 partial, properly handle keep-alive, provide media ranges and other HTTP headers: https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime To implement all these on the Simple HTTP server would be quite complicated. Then, instead, I imported the `flask` web server, which is quite simple and straight forward to use. Now, the back-end is using a secure complaint HTTP back-end: 1. https://palletsprojects.com/p/flask/ > Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. > > Flask offers suggestions, but doesn't enforce any dependencies or project layout. It is up to the developer to choose the tools and libraries they want to use. There are many extensions provided by the community that make adding new functionality easy. 1. https://docs.pylonsproject.org/projects/waitress/en/latest/ > Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 2.7+ and Python 3.5+. It is also known to run on PyPy 1.6.0 on UNIX. It supports HTTP/1.0 and HTTP/1.1. Right now, anki does not support fields passing file names directly to HTML audio tags, but this can be easily done with (https://github.com/ankitects/anki/pull 540 - Added arguments to the sound tag) plus the commit https://github.com/evandroforks/anki/commit/826a97df61b99814041c41c0f2c84268280ed8ad, the HTML5 audio tag can be used like this: ```html // Audio = [sound:myfile.mp3|onlyfilename] <audio id="elem_audio" src="{{Audio}}" controls controlsList="nodownload"></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063736-c539ad80-7c79-11ea-8420-40b72185f4e7.png) # Conflicts: # qt/aqt/mediasrv.py
2020-04-29 12:38:35 +02:00
self.is_shutdown = False
# map of webview ids to pages
self._page_html: dict[int, str] = {}
2021-02-01 14:28:21 +01:00
def run(self) -> None:
Replaced the mediasrv.py SimpleHttp server by flask and waitress, fixing HTML5 media support. https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server https://stackoverflow.com/questions/5052635/what-is-relation-between-content-length-and-byte-ranges-in-http-1-1 https://stackoverflow.com/questions/16725907/google-app-engine-serving-mp3-for-audio-element-needs-content-range-header I was trying to use HTML5 audio tag to display audios like: ```html <audio id="elem_audio" src="myfile.mp3" controls></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063321-565b5500-7c77-11ea-9f8d-6e1df6f07892.png) But the progress bar seek was not working. After researching, I found the problem was the HTML server not properly responding to the HTML5 header requests. The HTML server should respond to quite complicated things as 206 partial, properly handle keep-alive, provide media ranges and other HTTP headers: https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime To implement all these on the Simple HTTP server would be quite complicated. Then, instead, I imported the `flask` web server, which is quite simple and straight forward to use. Now, the back-end is using a secure complaint HTTP back-end: 1. https://palletsprojects.com/p/flask/ > Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. > > Flask offers suggestions, but doesn't enforce any dependencies or project layout. It is up to the developer to choose the tools and libraries they want to use. There are many extensions provided by the community that make adding new functionality easy. 1. https://docs.pylonsproject.org/projects/waitress/en/latest/ > Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 2.7+ and Python 3.5+. It is also known to run on PyPy 1.6.0 on UNIX. It supports HTTP/1.0 and HTTP/1.1. Right now, anki does not support fields passing file names directly to HTML audio tags, but this can be easily done with (https://github.com/ankitects/anki/pull 540 - Added arguments to the sound tag) plus the commit https://github.com/evandroforks/anki/commit/826a97df61b99814041c41c0f2c84268280ed8ad, the HTML5 audio tag can be used like this: ```html // Audio = [sound:myfile.mp3|onlyfilename] <audio id="elem_audio" src="{{Audio}}" controls controlsList="nodownload"></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063736-c539ad80-7c79-11ea-8420-40b72185f4e7.png) # Conflicts: # qt/aqt/mediasrv.py
2020-04-29 12:38:35 +02:00
try:
if dev_mode:
Replaced the mediasrv.py SimpleHttp server by flask and waitress, fixing HTML5 media support. https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server https://stackoverflow.com/questions/5052635/what-is-relation-between-content-length-and-byte-ranges-in-http-1-1 https://stackoverflow.com/questions/16725907/google-app-engine-serving-mp3-for-audio-element-needs-content-range-header I was trying to use HTML5 audio tag to display audios like: ```html <audio id="elem_audio" src="myfile.mp3" controls></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063321-565b5500-7c77-11ea-9f8d-6e1df6f07892.png) But the progress bar seek was not working. After researching, I found the problem was the HTML server not properly responding to the HTML5 header requests. The HTML server should respond to quite complicated things as 206 partial, properly handle keep-alive, provide media ranges and other HTTP headers: https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime To implement all these on the Simple HTTP server would be quite complicated. Then, instead, I imported the `flask` web server, which is quite simple and straight forward to use. Now, the back-end is using a secure complaint HTTP back-end: 1. https://palletsprojects.com/p/flask/ > Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. > > Flask offers suggestions, but doesn't enforce any dependencies or project layout. It is up to the developer to choose the tools and libraries they want to use. There are many extensions provided by the community that make adding new functionality easy. 1. https://docs.pylonsproject.org/projects/waitress/en/latest/ > Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 2.7+ and Python 3.5+. It is also known to run on PyPy 1.6.0 on UNIX. It supports HTTP/1.0 and HTTP/1.1. Right now, anki does not support fields passing file names directly to HTML audio tags, but this can be easily done with (https://github.com/ankitects/anki/pull 540 - Added arguments to the sound tag) plus the commit https://github.com/evandroforks/anki/commit/826a97df61b99814041c41c0f2c84268280ed8ad, the HTML5 audio tag can be used like this: ```html // Audio = [sound:myfile.mp3|onlyfilename] <audio id="elem_audio" src="{{Audio}}" controls controlsList="nodownload"></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063736-c539ad80-7c79-11ea-8420-40b72185f4e7.png) # Conflicts: # qt/aqt/mediasrv.py
2020-04-29 12:38:35 +02:00
# idempotent if logging has already been set up
logging.basicConfig()
2020-07-07 02:50:12 +02:00
logging.getLogger("waitress").setLevel(logging.ERROR)
Replaced the mediasrv.py SimpleHttp server by flask and waitress, fixing HTML5 media support. https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server https://stackoverflow.com/questions/5052635/what-is-relation-between-content-length-and-byte-ranges-in-http-1-1 https://stackoverflow.com/questions/16725907/google-app-engine-serving-mp3-for-audio-element-needs-content-range-header I was trying to use HTML5 audio tag to display audios like: ```html <audio id="elem_audio" src="myfile.mp3" controls></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063321-565b5500-7c77-11ea-9f8d-6e1df6f07892.png) But the progress bar seek was not working. After researching, I found the problem was the HTML server not properly responding to the HTML5 header requests. The HTML server should respond to quite complicated things as 206 partial, properly handle keep-alive, provide media ranges and other HTTP headers: https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime To implement all these on the Simple HTTP server would be quite complicated. Then, instead, I imported the `flask` web server, which is quite simple and straight forward to use. Now, the back-end is using a secure complaint HTTP back-end: 1. https://palletsprojects.com/p/flask/ > Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. > > Flask offers suggestions, but doesn't enforce any dependencies or project layout. It is up to the developer to choose the tools and libraries they want to use. There are many extensions provided by the community that make adding new functionality easy. 1. https://docs.pylonsproject.org/projects/waitress/en/latest/ > Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 2.7+ and Python 3.5+. It is also known to run on PyPy 1.6.0 on UNIX. It supports HTTP/1.0 and HTTP/1.1. Right now, anki does not support fields passing file names directly to HTML audio tags, but this can be easily done with (https://github.com/ankitects/anki/pull 540 - Added arguments to the sound tag) plus the commit https://github.com/evandroforks/anki/commit/826a97df61b99814041c41c0f2c84268280ed8ad, the HTML5 audio tag can be used like this: ```html // Audio = [sound:myfile.mp3|onlyfilename] <audio id="elem_audio" src="{{Audio}}" controls controlsList="nodownload"></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063736-c539ad80-7c79-11ea-8420-40b72185f4e7.png) # Conflicts: # qt/aqt/mediasrv.py
2020-04-29 12:38:35 +02:00
desired_host = os.getenv("ANKI_API_HOST", "127.0.0.1")
desired_port = int(os.getenv("ANKI_API_PORT", "0"))
2020-12-16 06:09:30 +01:00
self.server = create_server(
app,
host=desired_host,
2020-12-16 06:09:30 +01:00
port=desired_port,
clear_untrusted_proxy_headers=True,
)
if dev_mode:
Replaced the mediasrv.py SimpleHttp server by flask and waitress, fixing HTML5 media support. https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server https://stackoverflow.com/questions/5052635/what-is-relation-between-content-length-and-byte-ranges-in-http-1-1 https://stackoverflow.com/questions/16725907/google-app-engine-serving-mp3-for-audio-element-needs-content-range-header I was trying to use HTML5 audio tag to display audios like: ```html <audio id="elem_audio" src="myfile.mp3" controls></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063321-565b5500-7c77-11ea-9f8d-6e1df6f07892.png) But the progress bar seek was not working. After researching, I found the problem was the HTML server not properly responding to the HTML5 header requests. The HTML server should respond to quite complicated things as 206 partial, properly handle keep-alive, provide media ranges and other HTTP headers: https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime To implement all these on the Simple HTTP server would be quite complicated. Then, instead, I imported the `flask` web server, which is quite simple and straight forward to use. Now, the back-end is using a secure complaint HTTP back-end: 1. https://palletsprojects.com/p/flask/ > Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. > > Flask offers suggestions, but doesn't enforce any dependencies or project layout. It is up to the developer to choose the tools and libraries they want to use. There are many extensions provided by the community that make adding new functionality easy. 1. https://docs.pylonsproject.org/projects/waitress/en/latest/ > Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 2.7+ and Python 3.5+. It is also known to run on PyPy 1.6.0 on UNIX. It supports HTTP/1.0 and HTTP/1.1. Right now, anki does not support fields passing file names directly to HTML audio tags, but this can be easily done with (https://github.com/ankitects/anki/pull 540 - Added arguments to the sound tag) plus the commit https://github.com/evandroforks/anki/commit/826a97df61b99814041c41c0f2c84268280ed8ad, the HTML5 audio tag can be used like this: ```html // Audio = [sound:myfile.mp3|onlyfilename] <audio id="elem_audio" src="{{Audio}}" controls controlsList="nodownload"></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063736-c539ad80-7c79-11ea-8420-40b72185f4e7.png) # Conflicts: # qt/aqt/mediasrv.py
2020-04-29 12:38:35 +02:00
print(
"Serving on http://%s:%s"
% (self.server.effective_host, self.server.effective_port) # type: ignore
Replaced the mediasrv.py SimpleHttp server by flask and waitress, fixing HTML5 media support. https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server https://stackoverflow.com/questions/5052635/what-is-relation-between-content-length-and-byte-ranges-in-http-1-1 https://stackoverflow.com/questions/16725907/google-app-engine-serving-mp3-for-audio-element-needs-content-range-header I was trying to use HTML5 audio tag to display audios like: ```html <audio id="elem_audio" src="myfile.mp3" controls></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063321-565b5500-7c77-11ea-9f8d-6e1df6f07892.png) But the progress bar seek was not working. After researching, I found the problem was the HTML server not properly responding to the HTML5 header requests. The HTML server should respond to quite complicated things as 206 partial, properly handle keep-alive, provide media ranges and other HTTP headers: https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime To implement all these on the Simple HTTP server would be quite complicated. Then, instead, I imported the `flask` web server, which is quite simple and straight forward to use. Now, the back-end is using a secure complaint HTTP back-end: 1. https://palletsprojects.com/p/flask/ > Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. > > Flask offers suggestions, but doesn't enforce any dependencies or project layout. It is up to the developer to choose the tools and libraries they want to use. There are many extensions provided by the community that make adding new functionality easy. 1. https://docs.pylonsproject.org/projects/waitress/en/latest/ > Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 2.7+ and Python 3.5+. It is also known to run on PyPy 1.6.0 on UNIX. It supports HTTP/1.0 and HTTP/1.1. Right now, anki does not support fields passing file names directly to HTML audio tags, but this can be easily done with (https://github.com/ankitects/anki/pull 540 - Added arguments to the sound tag) plus the commit https://github.com/evandroforks/anki/commit/826a97df61b99814041c41c0f2c84268280ed8ad, the HTML5 audio tag can be used like this: ```html // Audio = [sound:myfile.mp3|onlyfilename] <audio id="elem_audio" src="{{Audio}}" controls controlsList="nodownload"></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063736-c539ad80-7c79-11ea-8420-40b72185f4e7.png) # Conflicts: # qt/aqt/mediasrv.py
2020-04-29 12:38:35 +02:00
)
self._ready.set()
self.server.run()
except Exception:
if not self.is_shutdown:
raise
def shutdown(self) -> None:
Replaced the mediasrv.py SimpleHttp server by flask and waitress, fixing HTML5 media support. https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server https://stackoverflow.com/questions/5052635/what-is-relation-between-content-length-and-byte-ranges-in-http-1-1 https://stackoverflow.com/questions/16725907/google-app-engine-serving-mp3-for-audio-element-needs-content-range-header I was trying to use HTML5 audio tag to display audios like: ```html <audio id="elem_audio" src="myfile.mp3" controls></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063321-565b5500-7c77-11ea-9f8d-6e1df6f07892.png) But the progress bar seek was not working. After researching, I found the problem was the HTML server not properly responding to the HTML5 header requests. The HTML server should respond to quite complicated things as 206 partial, properly handle keep-alive, provide media ranges and other HTTP headers: https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime To implement all these on the Simple HTTP server would be quite complicated. Then, instead, I imported the `flask` web server, which is quite simple and straight forward to use. Now, the back-end is using a secure complaint HTTP back-end: 1. https://palletsprojects.com/p/flask/ > Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. > > Flask offers suggestions, but doesn't enforce any dependencies or project layout. It is up to the developer to choose the tools and libraries they want to use. There are many extensions provided by the community that make adding new functionality easy. 1. https://docs.pylonsproject.org/projects/waitress/en/latest/ > Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 2.7+ and Python 3.5+. It is also known to run on PyPy 1.6.0 on UNIX. It supports HTTP/1.0 and HTTP/1.1. Right now, anki does not support fields passing file names directly to HTML audio tags, but this can be easily done with (https://github.com/ankitects/anki/pull 540 - Added arguments to the sound tag) plus the commit https://github.com/evandroforks/anki/commit/826a97df61b99814041c41c0f2c84268280ed8ad, the HTML5 audio tag can be used like this: ```html // Audio = [sound:myfile.mp3|onlyfilename] <audio id="elem_audio" src="{{Audio}}" controls controlsList="nodownload"></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063736-c539ad80-7c79-11ea-8420-40b72185f4e7.png) # Conflicts: # qt/aqt/mediasrv.py
2020-04-29 12:38:35 +02:00
self.is_shutdown = True
sockets = list(self.server._map.values()) # type: ignore
Replaced the mediasrv.py SimpleHttp server by flask and waitress, fixing HTML5 media support. https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server https://stackoverflow.com/questions/5052635/what-is-relation-between-content-length-and-byte-ranges-in-http-1-1 https://stackoverflow.com/questions/16725907/google-app-engine-serving-mp3-for-audio-element-needs-content-range-header I was trying to use HTML5 audio tag to display audios like: ```html <audio id="elem_audio" src="myfile.mp3" controls></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063321-565b5500-7c77-11ea-9f8d-6e1df6f07892.png) But the progress bar seek was not working. After researching, I found the problem was the HTML server not properly responding to the HTML5 header requests. The HTML server should respond to quite complicated things as 206 partial, properly handle keep-alive, provide media ranges and other HTTP headers: https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime To implement all these on the Simple HTTP server would be quite complicated. Then, instead, I imported the `flask` web server, which is quite simple and straight forward to use. Now, the back-end is using a secure complaint HTTP back-end: 1. https://palletsprojects.com/p/flask/ > Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. > > Flask offers suggestions, but doesn't enforce any dependencies or project layout. It is up to the developer to choose the tools and libraries they want to use. There are many extensions provided by the community that make adding new functionality easy. 1. https://docs.pylonsproject.org/projects/waitress/en/latest/ > Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 2.7+ and Python 3.5+. It is also known to run on PyPy 1.6.0 on UNIX. It supports HTTP/1.0 and HTTP/1.1. Right now, anki does not support fields passing file names directly to HTML audio tags, but this can be easily done with (https://github.com/ankitects/anki/pull 540 - Added arguments to the sound tag) plus the commit https://github.com/evandroforks/anki/commit/826a97df61b99814041c41c0f2c84268280ed8ad, the HTML5 audio tag can be used like this: ```html // Audio = [sound:myfile.mp3|onlyfilename] <audio id="elem_audio" src="{{Audio}}" controls controlsList="nodownload"></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063736-c539ad80-7c79-11ea-8420-40b72185f4e7.png) # Conflicts: # qt/aqt/mediasrv.py
2020-04-29 12:38:35 +02:00
for socket in sockets:
socket.handle_close()
# https://github.com/Pylons/webtest/blob/4b8a3ebf984185ff4fefb31b4d0cf82682e1fcf7/webtest/http.py#L93-L104
self.server.task_dispatcher.shutdown()
2016-07-07 15:39:48 +02:00
def getPort(self) -> int:
self._ready.wait()
return int(self.server.effective_port) # type: ignore
def set_page_html(self, id: int, html: str) -> None:
self._page_html[id] = html
def get_page_html(self, id: int) -> str | None:
return self._page_html.get(id)
def clear_page_html(self, id: int) -> None:
try:
del self._page_html[id]
except KeyError:
pass
@app.route("/favicon.ico")
def favicon() -> Response:
request = BundledFileRequest(os.path.join("imgs", "favicon.ico"))
return _handle_builtin_file_request(request)
def _mime_for_path(path: str) -> str:
"Mime type for provided path/filename."
if path.endswith(".css"):
# some users may have invalid mime type in the Windows registry
return "text/css"
elif path.endswith(".js"):
return "application/javascript"
else:
# autodetect
mime, _encoding = mimetypes.guess_type(path)
return mime or "application/octet-stream"
def _handle_local_file_request(request: LocalFileRequest) -> Response:
directory = request.root
path = request.path
Replaced the mediasrv.py SimpleHttp server by flask and waitress, fixing HTML5 media support. https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server https://stackoverflow.com/questions/5052635/what-is-relation-between-content-length-and-byte-ranges-in-http-1-1 https://stackoverflow.com/questions/16725907/google-app-engine-serving-mp3-for-audio-element-needs-content-range-header I was trying to use HTML5 audio tag to display audios like: ```html <audio id="elem_audio" src="myfile.mp3" controls></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063321-565b5500-7c77-11ea-9f8d-6e1df6f07892.png) But the progress bar seek was not working. After researching, I found the problem was the HTML server not properly responding to the HTML5 header requests. The HTML server should respond to quite complicated things as 206 partial, properly handle keep-alive, provide media ranges and other HTTP headers: https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime To implement all these on the Simple HTTP server would be quite complicated. Then, instead, I imported the `flask` web server, which is quite simple and straight forward to use. Now, the back-end is using a secure complaint HTTP back-end: 1. https://palletsprojects.com/p/flask/ > Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. > > Flask offers suggestions, but doesn't enforce any dependencies or project layout. It is up to the developer to choose the tools and libraries they want to use. There are many extensions provided by the community that make adding new functionality easy. 1. https://docs.pylonsproject.org/projects/waitress/en/latest/ > Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 2.7+ and Python 3.5+. It is also known to run on PyPy 1.6.0 on UNIX. It supports HTTP/1.0 and HTTP/1.1. Right now, anki does not support fields passing file names directly to HTML audio tags, but this can be easily done with (https://github.com/ankitects/anki/pull 540 - Added arguments to the sound tag) plus the commit https://github.com/evandroforks/anki/commit/826a97df61b99814041c41c0f2c84268280ed8ad, the HTML5 audio tag can be used like this: ```html // Audio = [sound:myfile.mp3|onlyfilename] <audio id="elem_audio" src="{{Audio}}" controls controlsList="nodownload"></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063736-c539ad80-7c79-11ea-8420-40b72185f4e7.png) # Conflicts: # qt/aqt/mediasrv.py
2020-04-29 12:38:35 +02:00
try:
isdir = os.path.isdir(os.path.join(directory, path))
except ValueError:
return flask.make_response(
f"Path for '{directory} - {path}' is too long!",
HTTPStatus.BAD_REQUEST,
Replaced the mediasrv.py SimpleHttp server by flask and waitress, fixing HTML5 media support. https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server https://stackoverflow.com/questions/5052635/what-is-relation-between-content-length-and-byte-ranges-in-http-1-1 https://stackoverflow.com/questions/16725907/google-app-engine-serving-mp3-for-audio-element-needs-content-range-header I was trying to use HTML5 audio tag to display audios like: ```html <audio id="elem_audio" src="myfile.mp3" controls></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063321-565b5500-7c77-11ea-9f8d-6e1df6f07892.png) But the progress bar seek was not working. After researching, I found the problem was the HTML server not properly responding to the HTML5 header requests. The HTML server should respond to quite complicated things as 206 partial, properly handle keep-alive, provide media ranges and other HTTP headers: https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime To implement all these on the Simple HTTP server would be quite complicated. Then, instead, I imported the `flask` web server, which is quite simple and straight forward to use. Now, the back-end is using a secure complaint HTTP back-end: 1. https://palletsprojects.com/p/flask/ > Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. > > Flask offers suggestions, but doesn't enforce any dependencies or project layout. It is up to the developer to choose the tools and libraries they want to use. There are many extensions provided by the community that make adding new functionality easy. 1. https://docs.pylonsproject.org/projects/waitress/en/latest/ > Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 2.7+ and Python 3.5+. It is also known to run on PyPy 1.6.0 on UNIX. It supports HTTP/1.0 and HTTP/1.1. Right now, anki does not support fields passing file names directly to HTML audio tags, but this can be easily done with (https://github.com/ankitects/anki/pull 540 - Added arguments to the sound tag) plus the commit https://github.com/evandroforks/anki/commit/826a97df61b99814041c41c0f2c84268280ed8ad, the HTML5 audio tag can be used like this: ```html // Audio = [sound:myfile.mp3|onlyfilename] <audio id="elem_audio" src="{{Audio}}" controls controlsList="nodownload"></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063736-c539ad80-7c79-11ea-8420-40b72185f4e7.png) # Conflicts: # qt/aqt/mediasrv.py
2020-04-29 12:38:35 +02:00
)
directory = os.path.realpath(directory)
path = os.path.normpath(path)
fullpath = os.path.abspath(os.path.join(directory, path))
# protect against directory transversal: https://security.openstack.org/guidelines/dg_using-file-paths.html
if not fullpath.startswith(directory):
return flask.make_response(
f"Path for '{directory} - {path}' is a security leak!",
HTTPStatus.FORBIDDEN,
)
Replaced the mediasrv.py SimpleHttp server by flask and waitress, fixing HTML5 media support. https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server https://stackoverflow.com/questions/5052635/what-is-relation-between-content-length-and-byte-ranges-in-http-1-1 https://stackoverflow.com/questions/16725907/google-app-engine-serving-mp3-for-audio-element-needs-content-range-header I was trying to use HTML5 audio tag to display audios like: ```html <audio id="elem_audio" src="myfile.mp3" controls></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063321-565b5500-7c77-11ea-9f8d-6e1df6f07892.png) But the progress bar seek was not working. After researching, I found the problem was the HTML server not properly responding to the HTML5 header requests. The HTML server should respond to quite complicated things as 206 partial, properly handle keep-alive, provide media ranges and other HTTP headers: https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime To implement all these on the Simple HTTP server would be quite complicated. Then, instead, I imported the `flask` web server, which is quite simple and straight forward to use. Now, the back-end is using a secure complaint HTTP back-end: 1. https://palletsprojects.com/p/flask/ > Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. > > Flask offers suggestions, but doesn't enforce any dependencies or project layout. It is up to the developer to choose the tools and libraries they want to use. There are many extensions provided by the community that make adding new functionality easy. 1. https://docs.pylonsproject.org/projects/waitress/en/latest/ > Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 2.7+ and Python 3.5+. It is also known to run on PyPy 1.6.0 on UNIX. It supports HTTP/1.0 and HTTP/1.1. Right now, anki does not support fields passing file names directly to HTML audio tags, but this can be easily done with (https://github.com/ankitects/anki/pull 540 - Added arguments to the sound tag) plus the commit https://github.com/evandroforks/anki/commit/826a97df61b99814041c41c0f2c84268280ed8ad, the HTML5 audio tag can be used like this: ```html // Audio = [sound:myfile.mp3|onlyfilename] <audio id="elem_audio" src="{{Audio}}" controls controlsList="nodownload"></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063736-c539ad80-7c79-11ea-8420-40b72185f4e7.png) # Conflicts: # qt/aqt/mediasrv.py
2020-04-29 12:38:35 +02:00
if isdir:
return flask.make_response(
f"Path for '{directory} - {path}' is a directory (not supported)!",
HTTPStatus.FORBIDDEN,
2019-12-23 01:34:10 +01:00
)
try:
mimetype = _mime_for_path(fullpath)
2020-07-07 02:50:12 +02:00
if os.path.exists(fullpath):
if fullpath.endswith(".css"):
# caching css files prevents flicker in the webview, but we want
# a short cache
max_age = 10
elif fullpath.endswith(".js"):
# don't cache js files
max_age = 0
else:
max_age = 60 * 60
return flask.send_file(
fullpath, mimetype=mimetype, conditional=True, max_age=max_age # type: ignore[call-arg]
)
2020-07-07 02:50:12 +02:00
else:
print(f"Not found: {path}")
2020-08-31 05:29:28 +02:00
return flask.make_response(
f"Invalid path: {path}",
2020-08-31 05:29:28 +02:00
HTTPStatus.NOT_FOUND,
)
Replaced the mediasrv.py SimpleHttp server by flask and waitress, fixing HTML5 media support. https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server https://stackoverflow.com/questions/5052635/what-is-relation-between-content-length-and-byte-ranges-in-http-1-1 https://stackoverflow.com/questions/16725907/google-app-engine-serving-mp3-for-audio-element-needs-content-range-header I was trying to use HTML5 audio tag to display audios like: ```html <audio id="elem_audio" src="myfile.mp3" controls></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063321-565b5500-7c77-11ea-9f8d-6e1df6f07892.png) But the progress bar seek was not working. After researching, I found the problem was the HTML server not properly responding to the HTML5 header requests. The HTML server should respond to quite complicated things as 206 partial, properly handle keep-alive, provide media ranges and other HTTP headers: https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime To implement all these on the Simple HTTP server would be quite complicated. Then, instead, I imported the `flask` web server, which is quite simple and straight forward to use. Now, the back-end is using a secure complaint HTTP back-end: 1. https://palletsprojects.com/p/flask/ > Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. > > Flask offers suggestions, but doesn't enforce any dependencies or project layout. It is up to the developer to choose the tools and libraries they want to use. There are many extensions provided by the community that make adding new functionality easy. 1. https://docs.pylonsproject.org/projects/waitress/en/latest/ > Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 2.7+ and Python 3.5+. It is also known to run on PyPy 1.6.0 on UNIX. It supports HTTP/1.0 and HTTP/1.1. Right now, anki does not support fields passing file names directly to HTML audio tags, but this can be easily done with (https://github.com/ankitects/anki/pull 540 - Added arguments to the sound tag) plus the commit https://github.com/evandroforks/anki/commit/826a97df61b99814041c41c0f2c84268280ed8ad, the HTML5 audio tag can be used like this: ```html // Audio = [sound:myfile.mp3|onlyfilename] <audio id="elem_audio" src="{{Audio}}" controls controlsList="nodownload"></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063736-c539ad80-7c79-11ea-8420-40b72185f4e7.png) # Conflicts: # qt/aqt/mediasrv.py
2020-04-29 12:38:35 +02:00
except Exception as error:
if dev_mode:
Replaced the mediasrv.py SimpleHttp server by flask and waitress, fixing HTML5 media support. https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server https://stackoverflow.com/questions/5052635/what-is-relation-between-content-length-and-byte-ranges-in-http-1-1 https://stackoverflow.com/questions/16725907/google-app-engine-serving-mp3-for-audio-element-needs-content-range-header I was trying to use HTML5 audio tag to display audios like: ```html <audio id="elem_audio" src="myfile.mp3" controls></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063321-565b5500-7c77-11ea-9f8d-6e1df6f07892.png) But the progress bar seek was not working. After researching, I found the problem was the HTML server not properly responding to the HTML5 header requests. The HTML server should respond to quite complicated things as 206 partial, properly handle keep-alive, provide media ranges and other HTTP headers: https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime To implement all these on the Simple HTTP server would be quite complicated. Then, instead, I imported the `flask` web server, which is quite simple and straight forward to use. Now, the back-end is using a secure complaint HTTP back-end: 1. https://palletsprojects.com/p/flask/ > Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. > > Flask offers suggestions, but doesn't enforce any dependencies or project layout. It is up to the developer to choose the tools and libraries they want to use. There are many extensions provided by the community that make adding new functionality easy. 1. https://docs.pylonsproject.org/projects/waitress/en/latest/ > Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 2.7+ and Python 3.5+. It is also known to run on PyPy 1.6.0 on UNIX. It supports HTTP/1.0 and HTTP/1.1. Right now, anki does not support fields passing file names directly to HTML audio tags, but this can be easily done with (https://github.com/ankitects/anki/pull 540 - Added arguments to the sound tag) plus the commit https://github.com/evandroforks/anki/commit/826a97df61b99814041c41c0f2c84268280ed8ad, the HTML5 audio tag can be used like this: ```html // Audio = [sound:myfile.mp3|onlyfilename] <audio id="elem_audio" src="{{Audio}}" controls controlsList="nodownload"></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063736-c539ad80-7c79-11ea-8420-40b72185f4e7.png) # Conflicts: # qt/aqt/mediasrv.py
2020-04-29 12:38:35 +02:00
print(
"Caught HTTP server exception,\n%s"
% "".join(traceback.format_exception(*sys.exc_info())),
)
# swallow it - user likely surfed away from
# review screen before an image had finished
# downloading
2020-08-31 05:29:28 +02:00
return flask.make_response(
str(error),
HTTPStatus.INTERNAL_SERVER_ERROR,
)
Replaced the mediasrv.py SimpleHttp server by flask and waitress, fixing HTML5 media support. https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server https://stackoverflow.com/questions/5052635/what-is-relation-between-content-length-and-byte-ranges-in-http-1-1 https://stackoverflow.com/questions/16725907/google-app-engine-serving-mp3-for-audio-element-needs-content-range-header I was trying to use HTML5 audio tag to display audios like: ```html <audio id="elem_audio" src="myfile.mp3" controls></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063321-565b5500-7c77-11ea-9f8d-6e1df6f07892.png) But the progress bar seek was not working. After researching, I found the problem was the HTML server not properly responding to the HTML5 header requests. The HTML server should respond to quite complicated things as 206 partial, properly handle keep-alive, provide media ranges and other HTTP headers: https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime To implement all these on the Simple HTTP server would be quite complicated. Then, instead, I imported the `flask` web server, which is quite simple and straight forward to use. Now, the back-end is using a secure complaint HTTP back-end: 1. https://palletsprojects.com/p/flask/ > Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. > > Flask offers suggestions, but doesn't enforce any dependencies or project layout. It is up to the developer to choose the tools and libraries they want to use. There are many extensions provided by the community that make adding new functionality easy. 1. https://docs.pylonsproject.org/projects/waitress/en/latest/ > Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 2.7+ and Python 3.5+. It is also known to run on PyPy 1.6.0 on UNIX. It supports HTTP/1.0 and HTTP/1.1. Right now, anki does not support fields passing file names directly to HTML audio tags, but this can be easily done with (https://github.com/ankitects/anki/pull 540 - Added arguments to the sound tag) plus the commit https://github.com/evandroforks/anki/commit/826a97df61b99814041c41c0f2c84268280ed8ad, the HTML5 audio tag can be used like this: ```html // Audio = [sound:myfile.mp3|onlyfilename] <audio id="elem_audio" src="{{Audio}}" controls controlsList="nodownload"></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063736-c539ad80-7c79-11ea-8420-40b72185f4e7.png) # Conflicts: # qt/aqt/mediasrv.py
2020-04-29 12:38:35 +02:00
def _builtin_data(path: str) -> bytes:
"""Return data from file in aqt/data folder.
Path must use forward slash separators."""
# overriden location?
if data_folder := os.getenv("ANKI_DATA_FOLDER"):
full_path = os.path.join(data_folder, path)
with open(full_path, "rb") as f:
return f.read()
else:
if is_win and not getattr(sys, "frozen", False):
# default Python resource loader expects backslashes on Windows
path = path.replace("/", "\\")
reader = aqt.__loader__.get_resource_reader("aqt") # type: ignore
with reader.open_resource(path) as f:
return f.read()
def _handle_builtin_file_request(request: BundledFileRequest) -> Response:
path = request.path
mimetype = _mime_for_path(path)
data_path = f"data/web/{path}"
try:
data = _builtin_data(data_path)
return Response(data, mimetype=mimetype)
except FileNotFoundError:
return flask.make_response(
f"Invalid path: {path}",
HTTPStatus.NOT_FOUND,
)
except Exception as error:
if dev_mode:
print(
"Caught HTTP server exception,\n%s"
% "".join(traceback.format_exception(*sys.exc_info())),
)
# swallow it - user likely surfed away from
# review screen before an image had finished
# downloading
return flask.make_response(
str(error),
HTTPStatus.INTERNAL_SERVER_ERROR,
)
@app.route("/<path:pathin>", methods=["GET", "POST"])
def handle_request(pathin: str) -> Response:
request = _extract_request(pathin)
if dev_mode:
print(f"{time.time():.3f} {flask.request.method} /{pathin}")
if isinstance(request, NotFound):
print(request.message)
return flask.make_response(
f"Invalid path: {pathin}",
HTTPStatus.NOT_FOUND,
)
elif callable(request):
return _handle_dynamic_request(request)
elif isinstance(request, BundledFileRequest):
return _handle_builtin_file_request(request)
elif isinstance(request, LocalFileRequest):
return _handle_local_file_request(request)
else:
return flask.make_response(
f"unexpected request: {pathin}",
HTTPStatus.FORBIDDEN,
)
def _extract_internal_request(
path: str,
) -> BundledFileRequest | DynamicRequest | NotFound | None:
"Catch /_anki references and rewrite them to web export folder."
prefix = "_anki/"
if not path.startswith(prefix):
return None
dirname = os.path.dirname(path)
filename = os.path.basename(path)
additional_prefix = None
if dirname == "_anki":
if flask.request.method == "POST":
return _extract_collection_post_request(filename)
elif get_handler := _extract_dynamic_get_request(filename):
return get_handler
# remap legacy top-level references
base, ext = os.path.splitext(filename)
if ext == ".css":
additional_prefix = "css/"
elif ext == ".js":
if base in ("browsersel", "jquery-ui", "jquery", "plot"):
additional_prefix = "js/vendor/"
else:
additional_prefix = "js/"
# handle requests for vendored libraries
elif dirname == "_anki/js/vendor":
base, ext = os.path.splitext(filename)
Replaced the mediasrv.py SimpleHttp server by flask and waitress, fixing HTML5 media support. https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server https://stackoverflow.com/questions/5052635/what-is-relation-between-content-length-and-byte-ranges-in-http-1-1 https://stackoverflow.com/questions/16725907/google-app-engine-serving-mp3-for-audio-element-needs-content-range-header I was trying to use HTML5 audio tag to display audios like: ```html <audio id="elem_audio" src="myfile.mp3" controls></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063321-565b5500-7c77-11ea-9f8d-6e1df6f07892.png) But the progress bar seek was not working. After researching, I found the problem was the HTML server not properly responding to the HTML5 header requests. The HTML server should respond to quite complicated things as 206 partial, properly handle keep-alive, provide media ranges and other HTTP headers: https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime To implement all these on the Simple HTTP server would be quite complicated. Then, instead, I imported the `flask` web server, which is quite simple and straight forward to use. Now, the back-end is using a secure complaint HTTP back-end: 1. https://palletsprojects.com/p/flask/ > Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. > > Flask offers suggestions, but doesn't enforce any dependencies or project layout. It is up to the developer to choose the tools and libraries they want to use. There are many extensions provided by the community that make adding new functionality easy. 1. https://docs.pylonsproject.org/projects/waitress/en/latest/ > Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 2.7+ and Python 3.5+. It is also known to run on PyPy 1.6.0 on UNIX. It supports HTTP/1.0 and HTTP/1.1. Right now, anki does not support fields passing file names directly to HTML audio tags, but this can be easily done with (https://github.com/ankitects/anki/pull 540 - Added arguments to the sound tag) plus the commit https://github.com/evandroforks/anki/commit/826a97df61b99814041c41c0f2c84268280ed8ad, the HTML5 audio tag can be used like this: ```html // Audio = [sound:myfile.mp3|onlyfilename] <audio id="elem_audio" src="{{Audio}}" controls controlsList="nodownload"></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063736-c539ad80-7c79-11ea-8420-40b72185f4e7.png) # Conflicts: # qt/aqt/mediasrv.py
2020-04-29 12:38:35 +02:00
if base == "jquery":
base = "jquery.min"
additional_prefix = "js/vendor/"
Replaced the mediasrv.py SimpleHttp server by flask and waitress, fixing HTML5 media support. https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server https://stackoverflow.com/questions/5052635/what-is-relation-between-content-length-and-byte-ranges-in-http-1-1 https://stackoverflow.com/questions/16725907/google-app-engine-serving-mp3-for-audio-element-needs-content-range-header I was trying to use HTML5 audio tag to display audios like: ```html <audio id="elem_audio" src="myfile.mp3" controls></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063321-565b5500-7c77-11ea-9f8d-6e1df6f07892.png) But the progress bar seek was not working. After researching, I found the problem was the HTML server not properly responding to the HTML5 header requests. The HTML server should respond to quite complicated things as 206 partial, properly handle keep-alive, provide media ranges and other HTTP headers: https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime To implement all these on the Simple HTTP server would be quite complicated. Then, instead, I imported the `flask` web server, which is quite simple and straight forward to use. Now, the back-end is using a secure complaint HTTP back-end: 1. https://palletsprojects.com/p/flask/ > Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. > > Flask offers suggestions, but doesn't enforce any dependencies or project layout. It is up to the developer to choose the tools and libraries they want to use. There are many extensions provided by the community that make adding new functionality easy. 1. https://docs.pylonsproject.org/projects/waitress/en/latest/ > Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 2.7+ and Python 3.5+. It is also known to run on PyPy 1.6.0 on UNIX. It supports HTTP/1.0 and HTTP/1.1. Right now, anki does not support fields passing file names directly to HTML audio tags, but this can be easily done with (https://github.com/ankitects/anki/pull 540 - Added arguments to the sound tag) plus the commit https://github.com/evandroforks/anki/commit/826a97df61b99814041c41c0f2c84268280ed8ad, the HTML5 audio tag can be used like this: ```html // Audio = [sound:myfile.mp3|onlyfilename] <audio id="elem_audio" src="{{Audio}}" controls controlsList="nodownload"></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063736-c539ad80-7c79-11ea-8420-40b72185f4e7.png) # Conflicts: # qt/aqt/mediasrv.py
2020-04-29 12:38:35 +02:00
elif base == "jquery-ui":
base = "jquery-ui.min"
additional_prefix = "js/vendor/"
elif base == "browsersel":
base = "css_browser_selector.min"
additional_prefix = "js/vendor/"
Replaced the mediasrv.py SimpleHttp server by flask and waitress, fixing HTML5 media support. https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server https://stackoverflow.com/questions/5052635/what-is-relation-between-content-length-and-byte-ranges-in-http-1-1 https://stackoverflow.com/questions/16725907/google-app-engine-serving-mp3-for-audio-element-needs-content-range-header I was trying to use HTML5 audio tag to display audios like: ```html <audio id="elem_audio" src="myfile.mp3" controls></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063321-565b5500-7c77-11ea-9f8d-6e1df6f07892.png) But the progress bar seek was not working. After researching, I found the problem was the HTML server not properly responding to the HTML5 header requests. The HTML server should respond to quite complicated things as 206 partial, properly handle keep-alive, provide media ranges and other HTTP headers: https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime To implement all these on the Simple HTTP server would be quite complicated. Then, instead, I imported the `flask` web server, which is quite simple and straight forward to use. Now, the back-end is using a secure complaint HTTP back-end: 1. https://palletsprojects.com/p/flask/ > Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. > > Flask offers suggestions, but doesn't enforce any dependencies or project layout. It is up to the developer to choose the tools and libraries they want to use. There are many extensions provided by the community that make adding new functionality easy. 1. https://docs.pylonsproject.org/projects/waitress/en/latest/ > Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 2.7+ and Python 3.5+. It is also known to run on PyPy 1.6.0 on UNIX. It supports HTTP/1.0 and HTTP/1.1. Right now, anki does not support fields passing file names directly to HTML audio tags, but this can be easily done with (https://github.com/ankitects/anki/pull 540 - Added arguments to the sound tag) plus the commit https://github.com/evandroforks/anki/commit/826a97df61b99814041c41c0f2c84268280ed8ad, the HTML5 audio tag can be used like this: ```html // Audio = [sound:myfile.mp3|onlyfilename] <audio id="elem_audio" src="{{Audio}}" controls controlsList="nodownload"></audio> ``` ![image](https://user-images.githubusercontent.com/5332158/79063736-c539ad80-7c79-11ea-8420-40b72185f4e7.png) # Conflicts: # qt/aqt/mediasrv.py
2020-04-29 12:38:35 +02:00
if additional_prefix:
oldpath = path
path = f"{prefix}{additional_prefix}{base}{ext}"
print(f"legacy {oldpath} remapped to {path}")
return BundledFileRequest(path=path[len(prefix) :])
2019-12-23 01:34:10 +01:00
def _extract_addon_request(path: str) -> LocalFileRequest | NotFound | None:
"Catch /_addons references and rewrite them to addons folder."
prefix = "_addons/"
if not path.startswith(prefix):
return None
addon_path = path[len(prefix) :]
try:
manager = aqt.mw.addonManager
except AttributeError as error:
if dev_mode:
print(f"_redirectWebExports: {error}")
return None
try:
addon, sub_path = addon_path.split("/", 1)
except ValueError:
return None
if not addon:
return None
2020-11-09 10:45:14 +01:00
pattern = manager.getWebExports(addon)
if not pattern:
return None
if re.fullmatch(pattern, sub_path):
return LocalFileRequest(root=manager.addonsFolder(), path=addon_path)
return NotFound(message=f"couldn't locate item in add-on folder {path}")
def _extract_request(
path: str,
) -> LocalFileRequest | BundledFileRequest | DynamicRequest | NotFound:
if internal := _extract_internal_request(path):
return internal
elif addon := _extract_addon_request(path):
return addon
if not aqt.mw.col:
return NotFound(message=f"collection not open, ignore request for {path}")
path = hooks.media_file_filter(path)
return LocalFileRequest(root=aqt.mw.col.media.dir(), path=path)
def congrats_info() -> bytes:
if not aqt.mw.col.sched._is_finished():
aqt.mw.taskman.run_on_main(lambda: aqt.mw.moveToState("review"))
return raw_backend_request("congrats_info")()
def get_deck_configs_for_update() -> bytes:
config_bytes = aqt.mw.col._backend.get_deck_configs_for_update_raw(request.data)
configs = DeckConfigsForUpdate.FromString(config_bytes)
configs.have_addons = aqt.mw.addonManager.dirty
return configs.SerializeToString()
def update_deck_configs() -> bytes:
2021-04-20 11:50:05 +02:00
# the regular change tracking machinery expects to be started on the main
# thread and uses a callback on success, so we need to run this op on
# main, and return immediately from the web request
input = UpdateDeckConfigs()
input.ParseFromString(request.data)
def on_success(changes: OpChanges) -> None:
if isinstance(window := aqt.mw.app.activeWindow(), DeckOptionsDialog):
window.reject()
2021-04-20 11:50:05 +02:00
def handle_on_main() -> None:
update_deck_configs_op(parent=aqt.mw, input=input).success(
2021-04-20 11:50:05 +02:00
on_success
).run_in_background()
aqt.mw.taskman.run_on_main(handle_on_main)
return b""
def get_custom_scheduling() -> bytes:
if scheduling := aqt.mw.reviewer.get_custom_scheduling():
return scheduling.SerializeToString()
else:
return b""
def set_custom_scheduling() -> bytes:
key = request.headers.get("key", "")
input = CustomScheduling()
input.ParseFromString(request.data)
aqt.mw.reviewer.set_custom_scheduling(key, input)
return b""
def change_notetype() -> bytes:
data = request.data
def handle_on_main() -> None:
window = aqt.mw.app.activeWindow()
if isinstance(window, ChangeNotetypeDialog):
window.save(data)
aqt.mw.taskman.run_on_main(handle_on_main)
return b""
Plaintext import/export (#1850) * Add crate csv * Add start of csv importing on backend * Add Menomosyne serializer * Add csv and json importing on backend * Add plaintext importing on frontend * Add csv metadata extraction on backend * Add csv importing with GUI * Fix missing dfa file in build Added compile_data_attr, then re-ran cargo/update.py. * Don't use doubly buffered reader in csv * Escape HTML entities if CSV is not HTML Also use name 'is_html' consistently. * Use decimal number as foreign ease (like '2.5') * ForeignCard.ivl → ForeignCard.interval * Only allow fixed set of CSV delimiters * Map timestamp of ForeignCard to native due time * Don't trim CSV records * Document use of empty strings for defaults * Avoid creating CardGenContexts for every note This requires CardGenContext to be generic, so it works both with an owned and borrowed notetype. * Show all accepted file types in import file picker * Add import_json_file() * factor → ease_factor * delimter_from_value → delimiter_from_value * Map columns to fields, not the other way around * Fallback to current config for csv metadata * Add start of new import csv screen * Temporary fix for compilation issue on Linux/Mac * Disable jest bazel action for import-csv Jest fails with an error code if no tests are available, but this would not be noticable on Windows as Jest is not run there. * Fix field mapping issue * Revert "Temporary fix for compilation issue on Linux/Mac" This reverts commit 21f8a261408cdae49ec031aa21a1b659c4f66d82. * Add HtmlSwitch and move Switch to components * Fix spacing and make selectors consistent * Fix shortcut tooltip * Place import button at the top with path * Fix meta column indices * Remove NotetypeForString * Fix queue and type of foreign cards * Support different dupe resolution strategies * Allow dupe resolution selection when importing CSV * Test import of unnormalized text Close #1863. * Fix logging of foreign notes * Implement CSV exports * Use db_scalar() in notes_table_len() * Rework CSV metadata - Notetypes and decks are either defined by a global id or by a column. - If a notetype id is provided, its field map must also be specified. - If a notetype column is provided, fields are now mapped by index instead of name at import time. So the first non-meta column is used for the first field of every note, regardless of notetype. This makes importing easier and should improve compatiblity with files without a notetype column. - Ensure first field can be mapped to a column. - Meta columns must be defined as `#[meta name]:[column index]` instead of in the `#columns` tag. - Column labels contain the raw names defined by the file and must be prettified by the frontend. * Adjust frontend to new backend column mapping * Add force flags for is_html and delimiter * Detect if CSV is HTML by field content * Update dupe resolution labels * Simplify selectors * Fix coalescence of oneofs in TS * Disable meta columns from selection Plus a lot of refactoring. * Make import button stick to the bottom * Write delimiter and html flag into csv * Refetch field map after notetype change * Fix log labels for csv import * Log notes whose deck/notetype was missing * Fix hiding of empty log queues * Implement adding tags to all notes of a csv * Fix dupe resolution not being set in log * Implement adding tags to updated notes of a csv * Check first note field is not empty * Temporary fix for build on Linux/Mac * Fix inverted html check (dae) * Remove unused ftl string * Delimiter → Separator * Remove commented-out line * Don't accept .json files * Tweak tag ftl strings * Remove redundant blur call * Strip sound and add spaces in csv export * Export HTML by default * Fix unset deck in Mnemosyne import Also accept both numbers and strings for notetypes and decks in JSON. * Make DupeResolution::Update the default * Fix missing dot in extension * Make column indices 1-based * Remove StickContainer from TagEditor Fixes line breaking, border and z index on ImportCsvPage. * Assign different key combos to tag editors * Log all updated duplicates Add a log field for the true number of found notes. * Show identical notes as skipped * Split tag-editor into separate ts module (dae) * Add progress for CSV export * Add progress for text import * Tidy-ups after tag-editor split (dae) - import-csv no longer depends on editor - remove some commented lines
2022-06-01 12:26:16 +02:00
def import_csv() -> bytes:
data = request.data
def handle_on_main() -> None:
window = aqt.mw.app.activeWindow()
if isinstance(window, ImportCsvDialog):
window.do_import(data)
aqt.mw.taskman.run_on_main(handle_on_main)
return b""
post_handler_list = [
congrats_info,
get_deck_configs_for_update,
update_deck_configs,
get_custom_scheduling,
set_custom_scheduling,
change_notetype,
Plaintext import/export (#1850) * Add crate csv * Add start of csv importing on backend * Add Menomosyne serializer * Add csv and json importing on backend * Add plaintext importing on frontend * Add csv metadata extraction on backend * Add csv importing with GUI * Fix missing dfa file in build Added compile_data_attr, then re-ran cargo/update.py. * Don't use doubly buffered reader in csv * Escape HTML entities if CSV is not HTML Also use name 'is_html' consistently. * Use decimal number as foreign ease (like '2.5') * ForeignCard.ivl → ForeignCard.interval * Only allow fixed set of CSV delimiters * Map timestamp of ForeignCard to native due time * Don't trim CSV records * Document use of empty strings for defaults * Avoid creating CardGenContexts for every note This requires CardGenContext to be generic, so it works both with an owned and borrowed notetype. * Show all accepted file types in import file picker * Add import_json_file() * factor → ease_factor * delimter_from_value → delimiter_from_value * Map columns to fields, not the other way around * Fallback to current config for csv metadata * Add start of new import csv screen * Temporary fix for compilation issue on Linux/Mac * Disable jest bazel action for import-csv Jest fails with an error code if no tests are available, but this would not be noticable on Windows as Jest is not run there. * Fix field mapping issue * Revert "Temporary fix for compilation issue on Linux/Mac" This reverts commit 21f8a261408cdae49ec031aa21a1b659c4f66d82. * Add HtmlSwitch and move Switch to components * Fix spacing and make selectors consistent * Fix shortcut tooltip * Place import button at the top with path * Fix meta column indices * Remove NotetypeForString * Fix queue and type of foreign cards * Support different dupe resolution strategies * Allow dupe resolution selection when importing CSV * Test import of unnormalized text Close #1863. * Fix logging of foreign notes * Implement CSV exports * Use db_scalar() in notes_table_len() * Rework CSV metadata - Notetypes and decks are either defined by a global id or by a column. - If a notetype id is provided, its field map must also be specified. - If a notetype column is provided, fields are now mapped by index instead of name at import time. So the first non-meta column is used for the first field of every note, regardless of notetype. This makes importing easier and should improve compatiblity with files without a notetype column. - Ensure first field can be mapped to a column. - Meta columns must be defined as `#[meta name]:[column index]` instead of in the `#columns` tag. - Column labels contain the raw names defined by the file and must be prettified by the frontend. * Adjust frontend to new backend column mapping * Add force flags for is_html and delimiter * Detect if CSV is HTML by field content * Update dupe resolution labels * Simplify selectors * Fix coalescence of oneofs in TS * Disable meta columns from selection Plus a lot of refactoring. * Make import button stick to the bottom * Write delimiter and html flag into csv * Refetch field map after notetype change * Fix log labels for csv import * Log notes whose deck/notetype was missing * Fix hiding of empty log queues * Implement adding tags to all notes of a csv * Fix dupe resolution not being set in log * Implement adding tags to updated notes of a csv * Check first note field is not empty * Temporary fix for build on Linux/Mac * Fix inverted html check (dae) * Remove unused ftl string * Delimiter → Separator * Remove commented-out line * Don't accept .json files * Tweak tag ftl strings * Remove redundant blur call * Strip sound and add spaces in csv export * Export HTML by default * Fix unset deck in Mnemosyne import Also accept both numbers and strings for notetypes and decks in JSON. * Make DupeResolution::Update the default * Fix missing dot in extension * Make column indices 1-based * Remove StickContainer from TagEditor Fixes line breaking, border and z index on ImportCsvPage. * Assign different key combos to tag editors * Log all updated duplicates Add a log field for the true number of found notes. * Show identical notes as skipped * Split tag-editor into separate ts module (dae) * Add progress for CSV export * Add progress for text import * Tidy-ups after tag-editor split (dae) - import-csv no longer depends on editor - remove some commented lines
2022-06-01 12:26:16 +02:00
import_csv,
]
exposed_backend_list = [
Plaintext import/export (#1850) * Add crate csv * Add start of csv importing on backend * Add Menomosyne serializer * Add csv and json importing on backend * Add plaintext importing on frontend * Add csv metadata extraction on backend * Add csv importing with GUI * Fix missing dfa file in build Added compile_data_attr, then re-ran cargo/update.py. * Don't use doubly buffered reader in csv * Escape HTML entities if CSV is not HTML Also use name 'is_html' consistently. * Use decimal number as foreign ease (like '2.5') * ForeignCard.ivl → ForeignCard.interval * Only allow fixed set of CSV delimiters * Map timestamp of ForeignCard to native due time * Don't trim CSV records * Document use of empty strings for defaults * Avoid creating CardGenContexts for every note This requires CardGenContext to be generic, so it works both with an owned and borrowed notetype. * Show all accepted file types in import file picker * Add import_json_file() * factor → ease_factor * delimter_from_value → delimiter_from_value * Map columns to fields, not the other way around * Fallback to current config for csv metadata * Add start of new import csv screen * Temporary fix for compilation issue on Linux/Mac * Disable jest bazel action for import-csv Jest fails with an error code if no tests are available, but this would not be noticable on Windows as Jest is not run there. * Fix field mapping issue * Revert "Temporary fix for compilation issue on Linux/Mac" This reverts commit 21f8a261408cdae49ec031aa21a1b659c4f66d82. * Add HtmlSwitch and move Switch to components * Fix spacing and make selectors consistent * Fix shortcut tooltip * Place import button at the top with path * Fix meta column indices * Remove NotetypeForString * Fix queue and type of foreign cards * Support different dupe resolution strategies * Allow dupe resolution selection when importing CSV * Test import of unnormalized text Close #1863. * Fix logging of foreign notes * Implement CSV exports * Use db_scalar() in notes_table_len() * Rework CSV metadata - Notetypes and decks are either defined by a global id or by a column. - If a notetype id is provided, its field map must also be specified. - If a notetype column is provided, fields are now mapped by index instead of name at import time. So the first non-meta column is used for the first field of every note, regardless of notetype. This makes importing easier and should improve compatiblity with files without a notetype column. - Ensure first field can be mapped to a column. - Meta columns must be defined as `#[meta name]:[column index]` instead of in the `#columns` tag. - Column labels contain the raw names defined by the file and must be prettified by the frontend. * Adjust frontend to new backend column mapping * Add force flags for is_html and delimiter * Detect if CSV is HTML by field content * Update dupe resolution labels * Simplify selectors * Fix coalescence of oneofs in TS * Disable meta columns from selection Plus a lot of refactoring. * Make import button stick to the bottom * Write delimiter and html flag into csv * Refetch field map after notetype change * Fix log labels for csv import * Log notes whose deck/notetype was missing * Fix hiding of empty log queues * Implement adding tags to all notes of a csv * Fix dupe resolution not being set in log * Implement adding tags to updated notes of a csv * Check first note field is not empty * Temporary fix for build on Linux/Mac * Fix inverted html check (dae) * Remove unused ftl string * Delimiter → Separator * Remove commented-out line * Don't accept .json files * Tweak tag ftl strings * Remove redundant blur call * Strip sound and add spaces in csv export * Export HTML by default * Fix unset deck in Mnemosyne import Also accept both numbers and strings for notetypes and decks in JSON. * Make DupeResolution::Update the default * Fix missing dot in extension * Make column indices 1-based * Remove StickContainer from TagEditor Fixes line breaking, border and z index on ImportCsvPage. * Assign different key combos to tag editors * Log all updated duplicates Add a log field for the true number of found notes. * Show identical notes as skipped * Split tag-editor into separate ts module (dae) * Add progress for CSV export * Add progress for text import * Tidy-ups after tag-editor split (dae) - import-csv no longer depends on editor - remove some commented lines
2022-06-01 12:26:16 +02:00
# DeckService
"get_deck_names",
# I18nService
"i18n_resources",
Plaintext import/export (#1850) * Add crate csv * Add start of csv importing on backend * Add Menomosyne serializer * Add csv and json importing on backend * Add plaintext importing on frontend * Add csv metadata extraction on backend * Add csv importing with GUI * Fix missing dfa file in build Added compile_data_attr, then re-ran cargo/update.py. * Don't use doubly buffered reader in csv * Escape HTML entities if CSV is not HTML Also use name 'is_html' consistently. * Use decimal number as foreign ease (like '2.5') * ForeignCard.ivl → ForeignCard.interval * Only allow fixed set of CSV delimiters * Map timestamp of ForeignCard to native due time * Don't trim CSV records * Document use of empty strings for defaults * Avoid creating CardGenContexts for every note This requires CardGenContext to be generic, so it works both with an owned and borrowed notetype. * Show all accepted file types in import file picker * Add import_json_file() * factor → ease_factor * delimter_from_value → delimiter_from_value * Map columns to fields, not the other way around * Fallback to current config for csv metadata * Add start of new import csv screen * Temporary fix for compilation issue on Linux/Mac * Disable jest bazel action for import-csv Jest fails with an error code if no tests are available, but this would not be noticable on Windows as Jest is not run there. * Fix field mapping issue * Revert "Temporary fix for compilation issue on Linux/Mac" This reverts commit 21f8a261408cdae49ec031aa21a1b659c4f66d82. * Add HtmlSwitch and move Switch to components * Fix spacing and make selectors consistent * Fix shortcut tooltip * Place import button at the top with path * Fix meta column indices * Remove NotetypeForString * Fix queue and type of foreign cards * Support different dupe resolution strategies * Allow dupe resolution selection when importing CSV * Test import of unnormalized text Close #1863. * Fix logging of foreign notes * Implement CSV exports * Use db_scalar() in notes_table_len() * Rework CSV metadata - Notetypes and decks are either defined by a global id or by a column. - If a notetype id is provided, its field map must also be specified. - If a notetype column is provided, fields are now mapped by index instead of name at import time. So the first non-meta column is used for the first field of every note, regardless of notetype. This makes importing easier and should improve compatiblity with files without a notetype column. - Ensure first field can be mapped to a column. - Meta columns must be defined as `#[meta name]:[column index]` instead of in the `#columns` tag. - Column labels contain the raw names defined by the file and must be prettified by the frontend. * Adjust frontend to new backend column mapping * Add force flags for is_html and delimiter * Detect if CSV is HTML by field content * Update dupe resolution labels * Simplify selectors * Fix coalescence of oneofs in TS * Disable meta columns from selection Plus a lot of refactoring. * Make import button stick to the bottom * Write delimiter and html flag into csv * Refetch field map after notetype change * Fix log labels for csv import * Log notes whose deck/notetype was missing * Fix hiding of empty log queues * Implement adding tags to all notes of a csv * Fix dupe resolution not being set in log * Implement adding tags to updated notes of a csv * Check first note field is not empty * Temporary fix for build on Linux/Mac * Fix inverted html check (dae) * Remove unused ftl string * Delimiter → Separator * Remove commented-out line * Don't accept .json files * Tweak tag ftl strings * Remove redundant blur call * Strip sound and add spaces in csv export * Export HTML by default * Fix unset deck in Mnemosyne import Also accept both numbers and strings for notetypes and decks in JSON. * Make DupeResolution::Update the default * Fix missing dot in extension * Make column indices 1-based * Remove StickContainer from TagEditor Fixes line breaking, border and z index on ImportCsvPage. * Assign different key combos to tag editors * Log all updated duplicates Add a log field for the true number of found notes. * Show identical notes as skipped * Split tag-editor into separate ts module (dae) * Add progress for CSV export * Add progress for text import * Tidy-ups after tag-editor split (dae) - import-csv no longer depends on editor - remove some commented lines
2022-06-01 12:26:16 +02:00
# ImportExportService
"get_csv_metadata",
# NotesService
Plaintext import/export (#1850) * Add crate csv * Add start of csv importing on backend * Add Menomosyne serializer * Add csv and json importing on backend * Add plaintext importing on frontend * Add csv metadata extraction on backend * Add csv importing with GUI * Fix missing dfa file in build Added compile_data_attr, then re-ran cargo/update.py. * Don't use doubly buffered reader in csv * Escape HTML entities if CSV is not HTML Also use name 'is_html' consistently. * Use decimal number as foreign ease (like '2.5') * ForeignCard.ivl → ForeignCard.interval * Only allow fixed set of CSV delimiters * Map timestamp of ForeignCard to native due time * Don't trim CSV records * Document use of empty strings for defaults * Avoid creating CardGenContexts for every note This requires CardGenContext to be generic, so it works both with an owned and borrowed notetype. * Show all accepted file types in import file picker * Add import_json_file() * factor → ease_factor * delimter_from_value → delimiter_from_value * Map columns to fields, not the other way around * Fallback to current config for csv metadata * Add start of new import csv screen * Temporary fix for compilation issue on Linux/Mac * Disable jest bazel action for import-csv Jest fails with an error code if no tests are available, but this would not be noticable on Windows as Jest is not run there. * Fix field mapping issue * Revert "Temporary fix for compilation issue on Linux/Mac" This reverts commit 21f8a261408cdae49ec031aa21a1b659c4f66d82. * Add HtmlSwitch and move Switch to components * Fix spacing and make selectors consistent * Fix shortcut tooltip * Place import button at the top with path * Fix meta column indices * Remove NotetypeForString * Fix queue and type of foreign cards * Support different dupe resolution strategies * Allow dupe resolution selection when importing CSV * Test import of unnormalized text Close #1863. * Fix logging of foreign notes * Implement CSV exports * Use db_scalar() in notes_table_len() * Rework CSV metadata - Notetypes and decks are either defined by a global id or by a column. - If a notetype id is provided, its field map must also be specified. - If a notetype column is provided, fields are now mapped by index instead of name at import time. So the first non-meta column is used for the first field of every note, regardless of notetype. This makes importing easier and should improve compatiblity with files without a notetype column. - Ensure first field can be mapped to a column. - Meta columns must be defined as `#[meta name]:[column index]` instead of in the `#columns` tag. - Column labels contain the raw names defined by the file and must be prettified by the frontend. * Adjust frontend to new backend column mapping * Add force flags for is_html and delimiter * Detect if CSV is HTML by field content * Update dupe resolution labels * Simplify selectors * Fix coalescence of oneofs in TS * Disable meta columns from selection Plus a lot of refactoring. * Make import button stick to the bottom * Write delimiter and html flag into csv * Refetch field map after notetype change * Fix log labels for csv import * Log notes whose deck/notetype was missing * Fix hiding of empty log queues * Implement adding tags to all notes of a csv * Fix dupe resolution not being set in log * Implement adding tags to updated notes of a csv * Check first note field is not empty * Temporary fix for build on Linux/Mac * Fix inverted html check (dae) * Remove unused ftl string * Delimiter → Separator * Remove commented-out line * Don't accept .json files * Tweak tag ftl strings * Remove redundant blur call * Strip sound and add spaces in csv export * Export HTML by default * Fix unset deck in Mnemosyne import Also accept both numbers and strings for notetypes and decks in JSON. * Make DupeResolution::Update the default * Fix missing dot in extension * Make column indices 1-based * Remove StickContainer from TagEditor Fixes line breaking, border and z index on ImportCsvPage. * Assign different key combos to tag editors * Log all updated duplicates Add a log field for the true number of found notes. * Show identical notes as skipped * Split tag-editor into separate ts module (dae) * Add progress for CSV export * Add progress for text import * Tidy-ups after tag-editor split (dae) - import-csv no longer depends on editor - remove some commented lines
2022-06-01 12:26:16 +02:00
"get_field_names",
"get_note",
# NotetypesService
"get_notetype_names",
"get_change_notetype_info",
# StatsService
"card_stats",
"graphs",
"get_graph_preferences",
"set_graph_preferences",
# TagsService
"complete_tag",
]
def raw_backend_request(endpoint: str) -> Callable[[], bytes]:
# check for key at startup
from anki._backend import RustBackend
assert hasattr(RustBackend, f"{endpoint}_raw")
return lambda: getattr(aqt.mw.col._backend, f"{endpoint}_raw")(request.data)
# all methods in here require a collection
2021-01-22 14:37:24 +01:00
post_handlers = {
stringcase.camelcase(handler.__name__): handler for handler in post_handler_list
} | {
stringcase.camelcase(handler): raw_backend_request(handler)
for handler in exposed_backend_list
2021-01-22 14:37:24 +01:00
}
def _extract_collection_post_request(path: str) -> DynamicRequest | NotFound:
if not aqt.mw.col:
return NotFound(message=f"collection not open, ignore request for {path}")
if handler := post_handlers.get(path):
# convert bytes/None into response
def wrapped() -> Response:
try:
if data := handler():
response = flask.make_response(data)
response.headers["Content-Type"] = "application/binary"
else:
response = flask.make_response("", HTTPStatus.NO_CONTENT)
except:
print(traceback.format_exc())
response = flask.make_response("", HTTPStatus.INTERNAL_SERVER_ERROR)
return response
return wrapped
else:
return NotFound(message=f"{path} not found")
2021-01-22 14:37:24 +01:00
def _handle_dynamic_request(request: DynamicRequest) -> Response:
try:
return request()
except Exception as e:
return flask.make_response(str(e), HTTPStatus.INTERNAL_SERVER_ERROR)
def legacy_page_data() -> Response:
id = int(request.args["id"])
if html := aqt.mw.mediaServer.get_page_html(id):
return Response(html, mimetype="text/html")
else:
return flask.make_response("page not found", HTTPStatus.NOT_FOUND)
# this currently only handles a single method; in the future, idempotent
# requests like i18nResources should probably be moved here
def _extract_dynamic_get_request(path: str) -> DynamicRequest | None:
if path == "legacyPageData":
return legacy_page_data
else:
return None