Initial commit

This commit is contained in:
Midgard 2023-05-02 17:38:05 +02:00
commit 96141fc6ea
Signed by: midgard
GPG key ID: 511C112F1331BBB4
16 changed files with 725 additions and 0 deletions

104
.gitignore vendored Normal file
View file

@ -0,0 +1,104 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
# Translations
*.mo
*.pot
# Django stuff:
*.log
.static_storage/
.media/
local_settings.py
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/

13
CHANGELOG.md Normal file
View file

@ -0,0 +1,13 @@
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Added
### Changed
### Deprecated
### Removed
### Fixed
### Security

17
README.md Normal file
View file

@ -0,0 +1,17 @@
# wolkje_files
## Development
To create a virtualenv and install the dependencies in it:
```
tools/create_venv.sh
```
Activate the virtualenv with `source venv/bin/activate`. To make this easier, you could create
an [alias][] `alias venv='source venv/bin/activate'` in your shell.
[alias]: https://www.computerworld.com/article/2598087/how-to-use-aliases-in-linux-shell-commands.html
If you introduce dependencies, list them in `setup.py` under `install_requires`, and run
`tools/update_requirements.sh`.

139
requirements.txt Normal file
View file

@ -0,0 +1,139 @@
#
# This file is autogenerated. To update, run:
# tools/update_requirements.sh
#
certifi==2022.12.7 \
--hash=sha256:35824b4c3a97115964b408844d64aa14db1cc518f6562e8d7261699d1350a9e3 \
--hash=sha256:4ad3232f5e926d6718ec31cfc1fcadfde020920e278684144551c91769c7bc18
# via requests
charset-normalizer==3.1.0 \
--hash=sha256:04afa6387e2b282cf78ff3dbce20f0cc071c12dc8f685bd40960cc68644cfea6 \
--hash=sha256:04eefcee095f58eaabe6dc3cc2262f3bcd776d2c67005880894f447b3f2cb9c1 \
--hash=sha256:0be65ccf618c1e7ac9b849c315cc2e8a8751d9cfdaa43027d4f6624bd587ab7e \
--hash=sha256:0c95f12b74681e9ae127728f7e5409cbbef9cd914d5896ef238cc779b8152373 \
--hash=sha256:0ca564606d2caafb0abe6d1b5311c2649e8071eb241b2d64e75a0d0065107e62 \
--hash=sha256:10c93628d7497c81686e8e5e557aafa78f230cd9e77dd0c40032ef90c18f2230 \
--hash=sha256:11d117e6c63e8f495412d37e7dc2e2fff09c34b2d09dbe2bee3c6229577818be \
--hash=sha256:11d3bcb7be35e7b1bba2c23beedac81ee893ac9871d0ba79effc7fc01167db6c \
--hash=sha256:12a2b561af122e3d94cdb97fe6fb2bb2b82cef0cdca131646fdb940a1eda04f0 \
--hash=sha256:12d1a39aa6b8c6f6248bb54550efcc1c38ce0d8096a146638fd4738e42284448 \
--hash=sha256:1435ae15108b1cb6fffbcea2af3d468683b7afed0169ad718451f8db5d1aff6f \
--hash=sha256:1c60b9c202d00052183c9be85e5eaf18a4ada0a47d188a83c8f5c5b23252f649 \
--hash=sha256:1e8fcdd8f672a1c4fc8d0bd3a2b576b152d2a349782d1eb0f6b8e52e9954731d \
--hash=sha256:20064ead0717cf9a73a6d1e779b23d149b53daf971169289ed2ed43a71e8d3b0 \
--hash=sha256:21fa558996782fc226b529fdd2ed7866c2c6ec91cee82735c98a197fae39f706 \
--hash=sha256:22908891a380d50738e1f978667536f6c6b526a2064156203d418f4856d6e86a \
--hash=sha256:3160a0fd9754aab7d47f95a6b63ab355388d890163eb03b2d2b87ab0a30cfa59 \
--hash=sha256:322102cdf1ab682ecc7d9b1c5eed4ec59657a65e1c146a0da342b78f4112db23 \
--hash=sha256:34e0a2f9c370eb95597aae63bf85eb5e96826d81e3dcf88b8886012906f509b5 \
--hash=sha256:3573d376454d956553c356df45bb824262c397c6e26ce43e8203c4c540ee0acb \
--hash=sha256:3747443b6a904001473370d7810aa19c3a180ccd52a7157aacc264a5ac79265e \
--hash=sha256:38e812a197bf8e71a59fe55b757a84c1f946d0ac114acafaafaf21667a7e169e \
--hash=sha256:3a06f32c9634a8705f4ca9946d667609f52cf130d5548881401f1eb2c39b1e2c \
--hash=sha256:3a5fc78f9e3f501a1614a98f7c54d3969f3ad9bba8ba3d9b438c3bc5d047dd28 \
--hash=sha256:3d9098b479e78c85080c98e1e35ff40b4a31d8953102bb0fd7d1b6f8a2111a3d \
--hash=sha256:3dc5b6a8ecfdc5748a7e429782598e4f17ef378e3e272eeb1340ea57c9109f41 \
--hash=sha256:4155b51ae05ed47199dc5b2a4e62abccb274cee6b01da5b895099b61b1982974 \
--hash=sha256:49919f8400b5e49e961f320c735388ee686a62327e773fa5b3ce6721f7e785ce \
--hash=sha256:53d0a3fa5f8af98a1e261de6a3943ca631c526635eb5817a87a59d9a57ebf48f \
--hash=sha256:5f008525e02908b20e04707a4f704cd286d94718f48bb33edddc7d7b584dddc1 \
--hash=sha256:628c985afb2c7d27a4800bfb609e03985aaecb42f955049957814e0491d4006d \
--hash=sha256:65ed923f84a6844de5fd29726b888e58c62820e0769b76565480e1fdc3d062f8 \
--hash=sha256:6734e606355834f13445b6adc38b53c0fd45f1a56a9ba06c2058f86893ae8017 \
--hash=sha256:6baf0baf0d5d265fa7944feb9f7451cc316bfe30e8df1a61b1bb08577c554f31 \
--hash=sha256:6f4f4668e1831850ebcc2fd0b1cd11721947b6dc7c00bf1c6bd3c929ae14f2c7 \
--hash=sha256:6f5c2e7bc8a4bf7c426599765b1bd33217ec84023033672c1e9a8b35eaeaaaf8 \
--hash=sha256:6f6c7a8a57e9405cad7485f4c9d3172ae486cfef1344b5ddd8e5239582d7355e \
--hash=sha256:7381c66e0561c5757ffe616af869b916c8b4e42b367ab29fedc98481d1e74e14 \
--hash=sha256:73dc03a6a7e30b7edc5b01b601e53e7fc924b04e1835e8e407c12c037e81adbd \
--hash=sha256:74db0052d985cf37fa111828d0dd230776ac99c740e1a758ad99094be4f1803d \
--hash=sha256:75f2568b4189dda1c567339b48cba4ac7384accb9c2a7ed655cd86b04055c795 \
--hash=sha256:78cacd03e79d009d95635e7d6ff12c21eb89b894c354bd2b2ed0b4763373693b \
--hash=sha256:80d1543d58bd3d6c271b66abf454d437a438dff01c3e62fdbcd68f2a11310d4b \
--hash=sha256:830d2948a5ec37c386d3170c483063798d7879037492540f10a475e3fd6f244b \
--hash=sha256:891cf9b48776b5c61c700b55a598621fdb7b1e301a550365571e9624f270c203 \
--hash=sha256:8f25e17ab3039b05f762b0a55ae0b3632b2e073d9c8fc88e89aca31a6198e88f \
--hash=sha256:9a3267620866c9d17b959a84dd0bd2d45719b817245e49371ead79ed4f710d19 \
--hash=sha256:a04f86f41a8916fe45ac5024ec477f41f886b3c435da2d4e3d2709b22ab02af1 \
--hash=sha256:aaf53a6cebad0eae578f062c7d462155eada9c172bd8c4d250b8c1d8eb7f916a \
--hash=sha256:abc1185d79f47c0a7aaf7e2412a0eb2c03b724581139193d2d82b3ad8cbb00ac \
--hash=sha256:ac0aa6cd53ab9a31d397f8303f92c42f534693528fafbdb997c82bae6e477ad9 \
--hash=sha256:ac3775e3311661d4adace3697a52ac0bab17edd166087d493b52d4f4f553f9f0 \
--hash=sha256:b06f0d3bf045158d2fb8837c5785fe9ff9b8c93358be64461a1089f5da983137 \
--hash=sha256:b116502087ce8a6b7a5f1814568ccbd0e9f6cfd99948aa59b0e241dc57cf739f \
--hash=sha256:b82fab78e0b1329e183a65260581de4375f619167478dddab510c6c6fb04d9b6 \
--hash=sha256:bd7163182133c0c7701b25e604cf1611c0d87712e56e88e7ee5d72deab3e76b5 \
--hash=sha256:c36bcbc0d5174a80d6cccf43a0ecaca44e81d25be4b7f90f0ed7bcfbb5a00909 \
--hash=sha256:c3af8e0f07399d3176b179f2e2634c3ce9c1301379a6b8c9c9aeecd481da494f \
--hash=sha256:c84132a54c750fda57729d1e2599bb598f5fa0344085dbde5003ba429a4798c0 \
--hash=sha256:cb7b2ab0188829593b9de646545175547a70d9a6e2b63bf2cd87a0a391599324 \
--hash=sha256:cca4def576f47a09a943666b8f829606bcb17e2bc2d5911a46c8f8da45f56755 \
--hash=sha256:cf6511efa4801b9b38dc5546d7547d5b5c6ef4b081c60b23e4d941d0eba9cbeb \
--hash=sha256:d16fd5252f883eb074ca55cb622bc0bee49b979ae4e8639fff6ca3ff44f9f854 \
--hash=sha256:d2686f91611f9e17f4548dbf050e75b079bbc2a82be565832bc8ea9047b61c8c \
--hash=sha256:d7fc3fca01da18fbabe4625d64bb612b533533ed10045a2ac3dd194bfa656b60 \
--hash=sha256:dd5653e67b149503c68c4018bf07e42eeed6b4e956b24c00ccdf93ac79cdff84 \
--hash=sha256:de5695a6f1d8340b12a5d6d4484290ee74d61e467c39ff03b39e30df62cf83a0 \
--hash=sha256:e0ac8959c929593fee38da1c2b64ee9778733cdf03c482c9ff1d508b6b593b2b \
--hash=sha256:e1b25e3ad6c909f398df8921780d6a3d120d8c09466720226fc621605b6f92b1 \
--hash=sha256:e633940f28c1e913615fd624fcdd72fdba807bf53ea6925d6a588e84e1151531 \
--hash=sha256:e89df2958e5159b811af9ff0f92614dabf4ff617c03a4c1c6ff53bf1c399e0e1 \
--hash=sha256:ea9f9c6034ea2d93d9147818f17c2a0860d41b71c38b9ce4d55f21b6f9165a11 \
--hash=sha256:f645caaf0008bacf349875a974220f1f1da349c5dbe7c4ec93048cdc785a3326 \
--hash=sha256:f8303414c7b03f794347ad062c0516cee0e15f7a612abd0ce1e25caf6ceb47df \
--hash=sha256:fca62a8301b605b954ad2e9c3666f9d97f63872aa4efcae5492baca2056b74ab
# via requests
falcon==3.1.1 \
--hash=sha256:00e6c6b3ec846193cfd30be26b10dbb7cc31ee3442f80f1d5ffd14c410619156 \
--hash=sha256:016fe952a526045292fb591f4c724d5fdf4127e88d0369e2dc147925dc51835c \
--hash=sha256:10ff3080aebe84fb45955cb02375ce13b6a3556c73edad282325eb67aeb42a46 \
--hash=sha256:13121ab6a86597ec849e397272662f5cafcbe534e12c01e2913035fe4120dcd1 \
--hash=sha256:1b8dfce6c379ba14d962abf479137258c694017752bc5b585ab366e2e8106a3e \
--hash=sha256:1cf50b9a2dcf9c8f6ae8de94e2e6ac082449380784fb9d1a1fc80fade052aead \
--hash=sha256:1d9c3dc6c5a8a2f2c3f1fd433a6b4e4bcef22c52166b91e2d6d985fbcadcc62b \
--hash=sha256:271fa0c4b0634e4e238dc7c2fcd57be5f9dd0f200553e46677ff704f6a8090e6 \
--hash=sha256:2abecd50121ed969fa34d5c035a340ee4b21afc84dcd354acd548ab2edcc67b2 \
--hash=sha256:394e16249d9b61dcdbb6653311c4a208f9fc68b696d0123d29f781fbd338cfd4 \
--hash=sha256:550566250ac2bc0418075f2ad177b7e01adef1815459c2d962e579dff07162fb \
--hash=sha256:5af63f2d7f509353552b2436501449065f30f27542d1e58c864656bd3a7a9ef1 \
--hash=sha256:5dd393dbf01cbaf99493893de4832121bd495dc49a46c571915b79c59aad7ef4 \
--hash=sha256:6245344fab1a7faeb9267c75b8f4fd6c4bda35e1a2fe8f547b832b547c7f2128 \
--hash=sha256:66d937b7b796b68640d63e006e475d9268f68dfb3f1468415259507db72ee065 \
--hash=sha256:686a0167af40565a2057f3902a9fb8f15a423ad17a80c9caee932b668478c9ad \
--hash=sha256:6fbc130a12e35ca76d782201af7a558ac57d4e5e66ba3a8017f5a3baaed64f8b \
--hash=sha256:762854cc9f13082166c166c93fd6f2178ba1787170bacee9a4b37fab412f602e \
--hash=sha256:7a7ecb8eafada89389c19eda44811e14786599c1d86c6cffa58c65150b24bc43 \
--hash=sha256:8a5fa02feaf67a2bd0407201dfec92edb0eee59803c3e1e717cfa5a2232ffc77 \
--hash=sha256:8fc0ef213d6e66bb997d172ceaa04f6daa309cac47e2fcd4320234806c806467 \
--hash=sha256:a7e6e1e6af16d1055454eaed5ceaceabca97656b28a8a924b426fbf0e26ec0f0 \
--hash=sha256:b7aab2dd6683437d8739a0cc9d6ab6542f48e05445a0138b356f63983a7c98fe \
--hash=sha256:b8302953d72405750450d4f8b7651dc6c5a5199dbb104b598036818f917b1d8c \
--hash=sha256:bec014dc19a38d5a525ab948a8eccc885f28d2611bdf3f73842fadc44b185702 \
--hash=sha256:ca798f3240283a89881209dfa8eb20e2eaf8d01c50b33be5f70865c0902577ec \
--hash=sha256:d53dabcf8212c38137e40a61795e312224dc7a437b03d7fb0a1b0dc3ed8d4b5b \
--hash=sha256:f049eeeeea08e0a5fbb87d1fe131f85c7a0310c3a0a4226146463709fbfe12eb \
--hash=sha256:f187040b6632ed434c3f6bcedb98fb6559973123d1799e77718502d2b693701e \
--hash=sha256:f6e3c42f3c20af33c040affe0a3e8cd358153304b48eb441adfd261c3bfd51d3 \
--hash=sha256:fd1eaf1a5d9d936f29f9aca3f268cf375621d1ffcbf27a6e14c187b489bf5f26 \
--hash=sha256:ff2eaf9807ea357ced1cc60e1d2871f55aa6ea29162386efb95fb4e5a730e6de
# via wolkje-files (setup.py)
idna==3.4 \
--hash=sha256:814f528e8dead7d329833b91c5faa87d60bf71824cd12a7530b5526063d02cb4 \
--hash=sha256:90b77e79eaa3eba6de819a0c442c0b4ceefc341a7a2ab77d7562bf49f425c5c2
# via requests
python-slugify==8.0.1 \
--hash=sha256:70ca6ea68fe63ecc8fa4fcf00ae651fc8a5d02d93dcd12ae6d4fc7ca46c4d395 \
--hash=sha256:ce0d46ddb668b3be82f4ed5e503dbc33dd815d83e2eb6824211310d3fb172a27
# via wolkje-files (setup.py)
requests==2.29.0 \
--hash=sha256:e8f3c9be120d3333921d213eef078af392fba3933ab7ed2d1cba3b56f2568c3b \
--hash=sha256:f2e34a75f4749019bb0e3effb66683630e4ffeaf75819fb51bebef1bf5aef059
# via wolkje-files (setup.py)
text-unidecode==1.3 \
--hash=sha256:1311f10e8b895935241623731c2ba64f4c455287888b18189350b67134a822e8 \
--hash=sha256:bad6603bb14d279193107714b288be206cac565dfa49aa5b105294dd5c4aab93
# via python-slugify
urllib3==1.26.15 \
--hash=sha256:8a388717b9476f934a21484e8c8e61875ab60644d29b9b39e11e4b9dc1c6b305 \
--hash=sha256:aa751d169e23c7479ce47a0cb0da579e3ede798f994f5816a74e4f4500dcea42
# via requests

4
run_dev.sh Executable file
View file

@ -0,0 +1,4 @@
#!/bin/sh
if ! [ -f venv/bin/gunicorn ]; then venv/bin/pip install gunicorn; fi
venv/bin/gunicorn --reload wolkje_files.app:app

58
setup.py Executable file
View file

@ -0,0 +1,58 @@
#!/usr/bin/env python3
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="wolkje_files",
version="0.1",
author="",
author_email="",
description="",
long_description=long_description,
long_description_content_type="text/markdown",
#scripts=["bin/…"],
url="",
project_urls={
"Source": "",
"Change log": "…/-/blob/master/CHANGELOG.md",
"Bug tracker": "…/-/issues",
},
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
"Operating System :: OS Independent",
"Natural Language :: English",
"Environment :: Console",
"Development Status :: 1 - Planning",
#"Development Status :: 2 - Pre-Alpha",
#"Development Status :: 3 - Alpha",
#"Development Status :: 4 - Beta",
#"Development Status :: 5 - Production/Stable",
#"Development Status :: 6 - Mature",
#"Development Status :: 7 - Inactive",
"Intended Audience :: End Users/Desktop",
"Topic :: Utilities",
],
packages=setuptools.find_packages(),
python_requires=">=3.6",
install_requires=[
"requests",
"falcon",
"python-slugify",
],
extras_require={
"dev": [
"pytest",
]
}
)

11
static/index.html Normal file
View file

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Wolkje gedeelde mappen</title>
</head>
<body>
</body>
</html>

24
tests/test_file_name.py Normal file
View file

@ -0,0 +1,24 @@
#!/usr/bin/env python3
from wolkje_files import file_name
def test_set_extension():
assert file_name.set_extension("", ".jpg") == ".jpg"
assert file_name.set_extension("foo", ".jpg") == "foo.jpg"
assert file_name.set_extension("foo.", ".jpg") == "foo.jpg"
assert file_name.set_extension("foo.bar", ".jpg") == "foo.jpg"
assert file_name.set_extension("foo.tar.gz", ".jpg") == "foo.jpg"
assert file_name.set_extension("foo.tafel", ".jpg") == "foo.jpg"
assert file_name.set_extension("foo.tafels", ".jpg") == "foo.tafels.jpg"
def test_sanitize():
assert file_name.sanitize("foo.jpg") == "foo.jpg"
assert file_name.sanitize("foo/bar.jpg") == "foo-bar.jpg"
assert file_name.sanitize("foo bar (1).jpg") == "foo bar (1).jpg"
assert file_name.sanitize("foo/bar (1).jpg") == "foo-bar (1).jpg"
assert file_name.sanitize(".foo/bar (1).jpg") == "foo-bar (1).jpg"
assert file_name.sanitize("..foo/bar (1).jpg") == "foo-bar (1).jpg"
assert file_name.sanitize("..") is None
assert file_name.sanitize("") is None

4
tools/clean.sh Executable file
View file

@ -0,0 +1,4 @@
#!/bin/sh
cd "`dirname "$0"`"/..
rm -rf ./build/ ./*.egg-info/ ./dist/ ./__pycache__/ ./*/__pycache__/

8
tools/create_venv.sh Executable file
View file

@ -0,0 +1,8 @@
#!/bin/sh
cd "`dirname "$0"`"/..
# Create virtualenv
python3 -m virtualenv venv/
# Install dependencies
venv/bin/pip install -e .[dev]

42
tools/release.sh Executable file
View file

@ -0,0 +1,42 @@
#!/bin/bash
set -e
cd $(dirname "$0")/..
tools/test.sh
if [ ! -t 0 ] ; then
echo "release.sh should be run with a terminal attached to stdin" >&2
exit 1
fi
git status
echo -n "Previous version: v"
prev_version="$(./setup.py --version)"
echo "$prev_version"
read -p "Enter new version: v" version
tagid=v"$version"
if [ "$version" != "$prev_version" ]; then
sed -i 's/version=".*"/version="'"$version"'"/' setup.py
sed -i 's/## \[Unreleased\]/&\n### Added\n### Changed\n### Deprecated\n### Removed\n### Fixed\n### Security\n\n## ['"$version"'] - '"$(date --utc +%Y-%m-%d)"'/' CHANGELOG.md
echo; echo "Inspect CHANGELOG..."
${EDITOR:-nano} CHANGELOG.md
git add setup.py CHANGELOG.md
git commit -m "Bump version to $version"
echo "Creating git tag $tagid"
git tag -s -m "Version $version" "$tagid"
else
echo "Version already created; building wheel and uploading"
fi
./setup.py sdist bdist_wheel
read -p "Upload to Git and PyPI? (y/N) " confirm
if [ ! "$confirm" = y ]; then "Abort"; exit 1; fi
python3 -m twine upload dist/*-${version}*
git push origin "$tagid" master

17
tools/update_requirements.sh Executable file
View file

@ -0,0 +1,17 @@
#!/bin/sh
cd "`dirname "$0"`"/..
if [ ! -f venv/bin/pip-compile ]; then
venv/bin/pip install pip-tools
fi
echo "Generating requirements.txt..."
cat <<EOF > requirements.txt
#
# This file is autogenerated. To update, run:
# tools/update_requirements.sh
#
EOF
venv/bin/pip-compile --quiet --generate-hashes --annotate --no-header --output-file="-" >> requirements.txt
echo "Updated requirements.txt"

0
wolkje_files/__init__.py Normal file
View file

93
wolkje_files/app.py Normal file
View file

@ -0,0 +1,93 @@
#!/usr/bin/env python3
# TODO maxlength on folder name
# TODO maxlength on file name
# TODO maxlength on file contents
import json
import falcon
from slugify import slugify
from wolkje_files import img_store
def get_slug(folder: dict):
if "name" in folder:
return slugify(folder["name"])
else:
return "untitled"
def ensure_slug(folder: dict, folder_slug_in_url: str):
expected_slug = get_slug(folder)
if folder_slug_in_url != expected_slug:
raise falcon.HTTPPermanentRedirect(f"/folder/{folder['folder_id']}/{expected_slug}")
def redirect_to_folder(folder: dict):
assert folder
raise falcon.HTTPFound(f"/folder/{folder['folder_id']}/{get_slug(folder)}")
class FolderIndex:
def on_post(self, req: falcon.Request, resp: falcon.Response):
try:
data = json.load(req.bounded_stream)
except Exception as exc:
raise falcon.HTTPBadRequest(description="Could not read body as JSON") from exc
if "folder_name" not in data:
raise falcon.HTTPBadRequest(description="folder_name not given in body")
folder = img_store.new_folder(data["folder_name"])
if not folder:
raise falcon.HTTPInternalServerError(description="Failed to create a new folder")
redirect_to_folder(folder)
class FolderResource:
def on_get(self, req: falcon.Request, resp: falcon.Response, folder_id: str, folder_slug=None):
folder = img_store.get_folder(folder_id)
if not folder:
raise falcon.HTTPNotFound()
ensure_slug(folder, folder_slug)
resp.media = {
"name": folder.get("name"),
"created": folder.get("created"),
"files": img_store.ls(folder_id),
}
def on_post(self, req: falcon.Request, resp: falcon.Response, folder_id: str, folder_slug: str):
# TODO check authorization
form = req.get_media()
for part in form:
if part.filename is None:
pass
try:
img_store.save_file(folder_id, part.filename, part.stream, part.content_type)
except img_store.NoSuchFolderError:
raise falcon.HTTPNotFound(description="Folder does not exist")
folder = img_store.get_folder(folder_id)
redirect_to_folder(folder)
class FileResource:
def on_get(
self,
req: falcon.Request, resp: falcon.Response,
folder_id: str, folder_slug: str, file_name: str
):
try:
resp.stream, resp.content_length, resp.content_type = img_store.open_file(folder_id, file_name)
except img_store.NoSuchFolderError:
raise falcon.HTTPNotFound(description="Folder does not exist")
except img_store.NoSuchFileError:
raise falcon.HTTPNotFound(description="File does not exist")
app = falcon.App()
app.add_route("/", FolderIndex())
app.add_route("/{folder_id}/{folder_slug}", FolderResource())
app.add_route("/{folder_id}/{folder_slug}/{file_name}", FileResource())

40
wolkje_files/file_name.py Normal file
View file

@ -0,0 +1,40 @@
#!/usr/bin/env python3
import re
from slugify import slugify
_REGEX_PART_ALLOWED_FILE_NAME_CHARACTERS = r"-_a-zA-Z0-9 ()."
_DISALLOWED_FILE_NAME_CHARACTERS = re.compile(rf"[^{_REGEX_PART_ALLOWED_FILE_NAME_CHARACTERS}]+")
ALLOWED_FILE_NAMES = re.compile(rf"^[{_REGEX_PART_ALLOWED_FILE_NAME_CHARACTERS}]+$")
def set_extension(file_name, extension):
m = re.fullmatch(r"""
^
(.*?) # Match anything, non-greedily because otherwise it would also match extension
(?:
(?:\.tar)? # Optionally match .tar
\.[^.]{0,5}
)? # Optionally match an extension (0 to 5 long)
$
""", file_name, flags=re.VERBOSE)
if not m:
return file_name + extension
else:
return m.group(1) + extension
def sanitize(file_name):
# Strip dots
while file_name.startswith("."):
file_name = file_name[1:]
if not file_name:
return None
if ALLOWED_FILE_NAMES.fullmatch(file_name):
return file_name
else:
return slugify(file_name, lowercase=False, regex_pattern=_DISALLOWED_FILE_NAME_CHARACTERS)

151
wolkje_files/img_store.py Normal file
View file

@ -0,0 +1,151 @@
#!/usr/bin/env python3
import os
import time
import mimetypes
import random
import json
import re
from slugify import slugify
from wolkje_files import file_name as fn
class UnknownMimeTypeError(Exception):
pass
class NoSuchFolderError(Exception):
pass
class NoSuchFileError(Exception):
pass
class IllegalFileNameError(Exception):
pass
_SECRETS_CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
_FOLDER_ID_LENGTH = 22
_PASSWORD_LENGTH = 32
FOLDER_ID_PATTERN = re.compile(r"^[A-Za-z0-9]{%d}$" % _FOLDER_ID_LENGTH)
def generate_secret(length):
return "".join(
_SECRETS_CHARACTERS[random.randint(0, len(_SECRETS_CHARACTERS) - 1)]
for _ in range(length)
)
def generate_new_folder_id():
folder_id = generate_secret(_FOLDER_ID_LENGTH)
while os.path.exists(_get_folder_path(folder_id)):
folder_id = generate_secret(_FOLDER_ID_LENGTH)
return folder_id
def generate_password():
return generate_secret(_PASSWORD_LENGTH)
_BASE_STORAGE_DIR_ENVNAME = "WOLKJE_STORAGE_DIR"
_BASE_STORAGE_DIR = os.getenv(_BASE_STORAGE_DIR_ENVNAME)
if not _BASE_STORAGE_DIR:
raise Exception(f"{_BASE_STORAGE_DIR_ENVNAME} not configured")
if not os.path.isdir(_BASE_STORAGE_DIR):
raise Exception(f"{_BASE_STORAGE_DIR_ENVNAME} ({_BASE_STORAGE_DIR}) is not an existing directory")
_STORAGE_DIR = os.path.join(_BASE_STORAGE_DIR, "files")
os.makedirs(_STORAGE_DIR, exist_ok=True)
_INFO_FILE_NAME = ".folder.json"
_DATA_FILE_ENCODING = "utf-8"
def _get_folder_path(folder_id):
return os.path.join(_STORAGE_DIR, folder_id)
def get_folder(folder_id):
if not FOLDER_ID_PATTERN.fullmatch(folder_id):
return None
folder_path = _get_folder_path(folder_id)
if not os.path.exists(folder_path):
return None
folder_info_path = os.path.join(folder_path, _INFO_FILE_NAME)
if not os.path.exists(folder_info_path):
return None
with open(folder_info_path, "r", encoding=_DATA_FILE_ENCODING) as fh:
data = {
**json.load(fh),
"folder_id": folder_id,
"folder_path": folder_path,
"folder_info_path": folder_info_path,
}
return data
def new_folder(folder_name):
folder_id = generate_new_folder_id()
folder_path = _get_folder_path(folder_id)
folder_info_path = os.path.join(folder_path, _INFO_FILE_NAME)
os.makedirs(folder_path)
with open(folder_info_path, "x", encoding=_DATA_FILE_ENCODING) as fh:
json.dump({
"name": folder_name,
"created": int(time.time()),
"editing_password": generate_password(),
}, fh, ensure_ascii=False)
return get_folder(folder_id)
def ls(folder_id):
folder = get_folder(folder_id)
if not folder:
return None
return [
filename
for filename in os.listdir(folder["folder_path"])
if not filename.startswith(".")
]
_CHUNK_SIZE_BYTES = 4096
def save_file(folder_id, desired_file_name, file_stream, mime_type):
folder = get_folder(folder_id)
if not folder:
raise NoSuchFolderError()
ext = mimetypes.guess_extension(mime_type)
if ext is None:
raise UnknownMimeTypeError()
file_name = fn.sanitize(fn.set_extension(desired_file_name, ext))
if file_name is None:
raise IllegalFileNameError()
file_path = os.path.join(_get_folder_path(folder_id), file_name)
with open(file_path, "wb") as fp:
while True:
chunk = file_stream.read(_CHUNK_SIZE_BYTES)
if not chunk:
break
fp.write(chunk)
return file_name
def open_file(folder_id, file_name):
folder = get_folder(folder_id)
if not folder:
raise NoSuchFolderError()
file_name = fn.sanitize(file_name)
if file_name is None:
raise IllegalFileNameError()
file_path = os.path.join(_get_folder_path(folder_id), file_name)
mime_type = mimetypes.guess_type(file_name)
fp = open(file_path, "rb")
content_length = os.path.getsize(file_path)
return fp, content_length, mime_type