Skip to content

Commit

Permalink
session and path
Browse files Browse the repository at this point in the history
  • Loading branch information
fnwinter committed Sep 18, 2024
1 parent a49d344 commit 2658bee
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 79 deletions.
6 changes: 6 additions & 0 deletions cnas/cnas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

from route.route import route
from service.background import background
from util.config import CONFIG

app = Flask(__name__)

# please set this value in config(~/.cnas/config.json)
_secret_key = CONFIG.get("secret_key")
app.secret_key = 'your_secret_key'\
if _secret_key is None else _secret_key

background()
route(app)
64 changes: 40 additions & 24 deletions cnas/pages/gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,49 @@
class gallery(page):
def __init__(self):
super().__init__()
self.pictures = []

def __str__(self):
if self.json_data:
print(self.json_data.get("path"))
def get_path(self):
_gallery_path = None
if self.json_data and self.json_data.get("path"):
_gallery_path = self.json_data.get("path")
else:
_gallery_path = get_gallery_path()
print(_gallery_path)

_gallery_path = get_gallery_path()
if _gallery_path is None or not os.path.exists(_gallery_path):
return str(error("No gallery path"))

def get_list(self):
# folder
for _folder in os.listdir(self.current_path):
_path = os.path.join(self.current_path, _folder)
if os.path.isdir(_path):
_div.append(
photo_builder(
src="static/images/folder.png",
path=_folder))

# images
for _file in os.listdir(self.current_path):
_path = os.path.join(self.current_path, _file)
_rel_path = ""
_thumb_nail_path = ""
if not is_image_file(_file):
continue
if not os.path.exists(_path):
continue
if os.path.exist(_thumb_nail_path):
_div.append(
photo_builder(
src="gallery_thumbnail/" + _rel_path,
path=_file))
else:
_div.append(
photo_builder(src="static/images/no_cache.png"))


def __str__(self):
_title_div = div(
para(class_="'title is-1 is-spaced'").set_content("Gallery"),
para(class_="'subtitle is-3'").set_content("/root"),
Expand All @@ -41,26 +75,8 @@ def __str__(self):
class_="'columns is-multiline'"
)

for _f in os.listdir(_gallery_path):
_path = os.path.join(_gallery_path, _f)
if not os.path.isdir(_path):
continue
_div.append(
photo_builder(
src="static/images/folder.png",
path=_path))

for _f in os.listdir(_gallery_path):
_path = os.path.join(_gallery_path, _f)
if not os.path.isfile(_path) and is_image_file(_f):
_div.append(
photo_builder(src="static/images/no_cache.png"))
continue
if is_image_file(_f):
_div.append(
photo_builder(
src="gallery_file/" + _f,
path=_path))
for pic in self.pictures:
_div.append(pic)

_section = section(
div(_title_div, class_="container").append(_div),
Expand Down
11 changes: 10 additions & 1 deletion cnas/pages/page.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
from flask import request
from flask import request, session

class page:
def __init__(self):
self.json_data = None
if request.is_json:
self.json_data = request.get_json()

def set_session(key, value):
session[key] = value

def get_session(key):
return session.get(key)

def clear_session(key):
session.pop(key, None)
3 changes: 3 additions & 0 deletions cnas/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
astroid==3.2.4
blinker==1.8.2
cachelib==0.13.0
click==8.1.7
dill==0.3.8
distlib==0.3.8
filelock==3.15.4
Flask==3.0.3
Flask-Session==0.8.0
isort==5.13.2
itsdangerous==2.2.0
Jinja2==3.1.4
MarkupSafe==2.1.5
mccabe==0.7.0
msgspec==0.18.6
pillow==10.4.0
platformdirs==4.2.2
pylint==3.2.6
Expand Down
55 changes: 1 addition & 54 deletions cnas/service/background.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,7 @@
import os
import threading
import time

from PIL import Image

from util.system_path import get_gallery_thumbnail_path
from util.config_path import get_gallery_path
from util.file_util import is_image_file

def correct_image_orientation(image):
try:
_exif = image.getexif()
if _exif is None:
return image

ORIENTATION = "Orientation"
_orientation = _exif.get(ORIENTATION)

if _orientation == 3:
image = image.rotate(180, expand=True)
elif _orientation == 6:
image = image.rotate(270, expand=True)
elif _orientation == 8:
image = image.rotate(90, expand=True)
except (AttributeError, KeyError, IndexError) as e:
print(f"error while rotating tumbnail {e}")

return image


def create_thumbnail(root_, rel_, file_, size=(256, 256)):
full_path = os.path.join(*[root_, rel_, file_])

thumb_ = get_gallery_thumbnail_path()

full_thumb_ = os.path.join(thumb_, rel_)

os.makedirs(full_thumb_, exist_ok=True)

output_ = os.path.join(*[thumb_, rel_, file_])

if os.path.exists(full_path):
with Image.open(full_path) as img:
img.thumbnail(size)
img = correct_image_orientation(img)
img.save(output_)


def generate_thumbnail():
gallery_path = get_gallery_path()
assert gallery_path, "No gallery root"
for _root, _, files in os.walk(gallery_path):
for _file in files:
rel_path = os.path.relpath(_root, gallery_path)
if is_image_file(_file):
create_thumbnail(gallery_path, rel_path, _file)
from service.thumbnail_service import generate_thumbnail

def background_task():
while True:
Expand Down
55 changes: 55 additions & 0 deletions cnas/service/thumbnail_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import os

from PIL import Image

from util.system_path import get_gallery_thumbnail_path
from util.config_path import get_gallery_path
from util.file_util import is_image_file

def correct_image_orientation(image):
try:
_exif = image.getexif()
if _exif is None:
return image

ORIENTATION = "Orientation"
_orientation = _exif.get(ORIENTATION)

if _orientation == 3:
image = image.rotate(180, expand=True)
elif _orientation == 6:
image = image.rotate(270, expand=True)
elif _orientation == 8:
image = image.rotate(90, expand=True)
except (AttributeError, KeyError, IndexError) as e:
print(f"error while rotating tumbnail {e}")

return image


def create_thumbnail(root_, rel_, file_, size=(256, 256)):
full_path = os.path.join(*[root_, rel_, file_])

thumb_ = get_gallery_thumbnail_path()

full_thumb_ = os.path.join(thumb_, rel_)

os.makedirs(full_thumb_, exist_ok=True)

output_ = os.path.join(*[thumb_, rel_, file_])

if os.path.exists(full_path):
with Image.open(full_path) as img:
img.thumbnail(size)
img = correct_image_orientation(img)
img.save(output_)


def generate_thumbnail():
gallery_path = get_gallery_path()
assert gallery_path, "No gallery root"
for _root, _, files in os.walk(gallery_path):
for _file in files:
rel_path = os.path.relpath(_root, gallery_path)
if is_image_file(_file):
create_thumbnail(gallery_path, rel_path, _file)
1 change: 1 addition & 0 deletions cnas/static/javascripts/cnas_gallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ $(document).ready(function(){
data: JSON.stringify(data),
success: function(response) {
console.log(response)
document.body.innerHTML = response
},
error: function(xhr, status, error) {
console.log(error)
Expand Down

0 comments on commit 2658bee

Please sign in to comment.