Skip to content

Commit

Permalink
Merge pull request #88 from aaronwmorris/dev
Browse files Browse the repository at this point in the history
Allow images to be stored completely separate from the docroot
  • Loading branch information
aaronwmorris authored Feb 21, 2022
2 parents 7845f22 + f016f4c commit bc91413
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 22 deletions.
1 change: 1 addition & 0 deletions flask.json_template
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

"INDI_ALLSKY_CONFIG" : "%ALLSKY_ETC%/config.json",
"INDI_ALLSKY_DOCROOT" : "%HTDOCS_FOLDER%",
"INDI_ALLSKY_IMAGE_FOLDER" : "%IMAGE_FOLDER%",
"INDI_ALLSKY_STATUS" : "%DB_FOLDER%/indi_allsky_status.json",
"INDI_ALLSKY_PID" : "%DB_FOLDER%/indi-allsky.pid",

Expand Down
53 changes: 46 additions & 7 deletions indi_allsky/flask/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,26 @@ def IMAGE_FILE_COMPRESSION__PNG_validator(form, field):


def IMAGE_FOLDER_validator(form, field):
folder_regex = r'^[a-zA-Z0-9_\.\-\/]+$'

if not re.search(folder_regex, field.data):
raise ValidationError('Invalid folder name')

if re.search(r'\/$', field.data):
raise ValidationError('Directory cannot end with slash')


image_folder_p = Path(field.data)

try:
if not image_folder_p.exists():
raise ValidationError('Directory does not exist')
image_folder_p.mkdir(mode=0o755, parents=True)

except PermissionError as e:
raise ValidationError(str(e))


try:
if not image_folder_p.is_dir():
raise ValidationError('Path is not a directory')
except PermissionError as e:
Expand Down Expand Up @@ -741,9 +755,14 @@ def getImages(self, year, month, day, hour):
images_choices = []
for i in images_query:
filename_p = Path(i.filename)
rel_filename_p = filename_p.relative_to(app.config['INDI_ALLSKY_DOCROOT'])

entry = (str(rel_filename_p), str(i.createDate.strftime('%H:%M:%S')))
try:
rel_filename_p = filename_p.relative_to(app.config['INDI_ALLSKY_IMAGE_FOLDER'])
except ValueError as e:
app.logger.error('Error determining relative file name: %s', str(e))
continue

entry = (str(Path('images').joinpath(rel_filename_p)), str(i.createDate.strftime('%H:%M:%S')))
images_choices.append(entry)


Expand Down Expand Up @@ -864,10 +883,15 @@ def getVideos(self, year, month, timeofday):
videos_data = []
for v in videos_query:
filename_p = Path(v.filename)
rel_filename_p = filename_p.relative_to(app.config['INDI_ALLSKY_DOCROOT'])

try:
rel_filename_p = filename_p.relative_to(app.config['INDI_ALLSKY_IMAGE_FOLDER'])
except ValueError as e:
app.logger.error('Error determining relative file name: %s', str(e))
continue

entry = {
'url' : str(rel_filename_p),
'url' : str(Path('images').joinpath(rel_filename_p)),
'dayDate' : v.dayDate.strftime('%B %d, %Y'),
'night' : v.night,
}
Expand All @@ -888,8 +912,16 @@ def getVideos(self, year, month, timeofday):
.order_by(IndiAllSkyDbKeogramTable.createDate.asc())\
.first() # use the oldest (asc)


if keogram_entry:
keogram_url = str(Path(keogram_entry.filename).relative_to(app.config['INDI_ALLSKY_DOCROOT']))
keogram_p = Path(keogram_entry.filename)

try:
rel_keogram_p = keogram_p.relative_to(app.config['INDI_ALLSKY_IMAGE_FOLDER'])
keogram_url = str(Path('images').joinpath(rel_keogram_p))
except ValueError as e:
app.logger.error('Error determining relative file name: %s', str(e))
keogram_url = None
else:
keogram_url = None

Expand All @@ -904,7 +936,14 @@ def getVideos(self, year, month, timeofday):


if startrail_entry:
startrail_url = str(Path(startrail_entry.filename).relative_to(app.config['INDI_ALLSKY_DOCROOT']))
startrail_p = Path(startrail_entry.filename)

try:
rel_startrail_p = startrail_p.relative_to(app.config['INDI_ALLSKY_IMAGE_FOLDER'])
startrail_url = str(Path('images').joinpath(rel_startrail_p))
except ValueError as e:
app.logger.error('Error determining relative file name: %s', str(e))
startrail_url = None
else:
startrail_url = None

Expand Down
6 changes: 5 additions & 1 deletion indi_allsky/flask/templates/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,11 @@
{{ form_config.IMAGE_FOLDER(class='form-control bg-secondary') }}
<div id="IMAGE_FOLDER-error" class="invalid-feedback text-danger" style="display: none;"></div>
</div>
<div class="col-sm-4">Directory to store timelapse images</div>
<div class="col-sm-4">
<div>Directory to store timelapse images</div>
<div>Default: <pre>/var/www/html/allsky/images</pre></div>
<div>If you change this setting, please re-run setup.sh to update the web server config</div>
</div>
</div>

<div class="form-group row">
Expand Down
9 changes: 7 additions & 2 deletions indi_allsky/flask/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,15 @@ def getLatestImages(self, history_seconds):
image_list = list()
for i in latest_images:
filename_p = Path(i.filename)
rel_filename_p = filename_p.relative_to(app.config['INDI_ALLSKY_DOCROOT'])

try:
rel_filename_p = filename_p.relative_to(app.config['INDI_ALLSKY_IMAGE_FOLDER'])
except ValueError as e:
app.logger.error('Error determining relative file name: %s', str(e))
continue

data = {
'file' : str(rel_filename_p),
'file' : str(Path('images').joinpath(rel_filename_p)),
'sqm' : i.sqm,
'stars' : i.stars,
}
Expand Down
2 changes: 1 addition & 1 deletion indi_allsky/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# This file is monitored for changes via inotify
# Updates should restart gunicorn automatically
#
# Version 00004
# Version 00005
#
import logging

Expand Down
7 changes: 6 additions & 1 deletion service/apache_indi-allsky.conf
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
Require all granted
</Directory>

<Directory %IMAGE_FOLDER%>
Require all granted
Options +Indexes
</Directory>

<Location />
AuthType Basic
AuthName "Allsky"
Expand All @@ -26,8 +31,8 @@
#SetEnv proxy-chain-auth On
</Location>

Alias /indi-allsky/images %IMAGE_FOLDER%
Alias /indi-allsky/static %ALLSKY_DIRECTORY%/indi_allsky/flask/static
Alias /indi-allsky/images /var/www/html/allsky/images

SSLEngine on
SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
Expand Down
4 changes: 2 additions & 2 deletions service/nginx_astroberry_ssl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ server {


location /indi-allsky/images {
alias /var/www/html/allsky/images;
autoindex off;
alias %IMAGE_FOLDER%;
autoindex on;
}

location /indi-allsky/static {
Expand Down
42 changes: 34 additions & 8 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ HTDOCS_FILES="
js/settings_latestDb.js
js/settings_loop.js
.htaccess
images/.htaccess
images/darks/.htaccess
"

IMAGE_FOLDER_FILES="
.htaccess
darks/.htaccess
"


DISTRO_NAME=$(lsb_release -s -i)
DISTRO_RELEASE=$(lsb_release -s -r)
CPU_ARCH=$(uname -m)
Expand Down Expand Up @@ -81,6 +85,7 @@ echo
echo
echo "Distribution: $DISTRO_NAME"
echo "Release: $DISTRO_RELEASE"
echo "Arch: $CPU_ARCH"
echo
echo "INDI_DRIVER_PATH: $INDI_DRIVER_PATH"
echo "INDISERVER_SERVICE_NAME: $INDISEVER_SERVICE_NAME"
Expand Down Expand Up @@ -186,6 +191,7 @@ if [[ "$DISTRO_NAME" == "Raspbian" && "$DISTRO_RELEASE" == "11" ]]; then
pkg-config \
ffmpeg \
gifsicle \
jq \
sqlite3


Expand Down Expand Up @@ -246,6 +252,7 @@ elif [[ "$DISTRO_NAME" == "Raspbian" && "$DISTRO_RELEASE" == "10" ]]; then
pkg-config \
ffmpeg \
gifsicle \
jq \
sqlite3


Expand Down Expand Up @@ -319,6 +326,7 @@ elif [[ "$DISTRO_NAME" == "Debian" && "$DISTRO_RELEASE" == "11" ]]; then
pkg-config \
ffmpeg \
gifsicle \
jq \
sqlite3


Expand Down Expand Up @@ -372,6 +380,7 @@ elif [[ "$DISTRO_NAME" == "Debian" && "$DISTRO_RELEASE" == "10" ]]; then
pkg-config \
ffmpeg \
gifsicle \
jq \
sqlite3


Expand Down Expand Up @@ -429,6 +438,7 @@ elif [[ "$DISTRO_NAME" == "Ubuntu" && "$DISTRO_RELEASE" == "20.04" ]]; then
pkg-config \
ffmpeg \
gifsicle \
jq \
sqlite3


Expand Down Expand Up @@ -487,6 +497,7 @@ elif [[ "$DISTRO_NAME" == "Ubuntu" && "$DISTRO_RELEASE" == "18.04" ]]; then
pkg-config \
ffmpeg \
gifsicle \
jq \
sqlite3


Expand Down Expand Up @@ -667,6 +678,10 @@ fi
sudo chown "$USER":"$PGRP" "${ALLSKY_ETC}/config.json"
sudo chmod 660 "${ALLSKY_ETC}/config.json"

# Detect IMAGE_FOLDER
IMAGE_FOLDER=$(jq -r '.IMAGE_FOLDER' /etc/indi-allsky/config.json)
echo "Detected image folder: $IMAGE_FOLDER"


echo "**** Flask config ****"
TMP4=$(mktemp)
Expand All @@ -677,6 +692,7 @@ sed \
-e "s|%SECRET_KEY%|$SECRET_KEY|g" \
-e "s|%ALLSKY_ETC%|$ALLSKY_ETC|g" \
-e "s|%HTDOCS_FOLDER%|$HTDOCS_FOLDER|g" \
-e "s|%IMAGE_FOLDER%|$IMAGE_FOLDER|g" \
-e "s|%INDISEVER_SERVICE_NAME%|$INDISEVER_SERVICE_NAME|g" \
-e "s|%ALLSKY_SERVICE_NAME%|$ALLSKY_SERVICE_NAME|g" \
-e "s|%GUNICORN_SERVICE_NAME%|$GUNICORN_SERVICE_NAME|g" \
Expand Down Expand Up @@ -718,6 +734,7 @@ if [[ "$ASTROBERRY" == "true" ]]; then
-e "s|%GUNICORN_SERVICE_NAME%|$GUNICORN_SERVICE_NAME|g" \
-e "s|%DB_FOLDER%|$DB_FOLDER|g" \
-e "s|%ALLSKY_ETC%|$ALLSKY_ETC|g" \
-e "s|%IMAGE_FOLDER%|$IMAGE_FOLDER|g" \
${ALLSKY_DIRECTORY}/service/nginx_astroberry_ssl > $TMP3


Expand Down Expand Up @@ -754,6 +771,7 @@ else
-e "s|%GUNICORN_SERVICE_NAME%|$GUNICORN_SERVICE_NAME|g" \
-e "s|%DB_FOLDER%|$DB_FOLDER|g" \
-e "s|%ALLSKY_ETC%|$ALLSKY_ETC|g" \
-e "s|%IMAGE_FOLDER%|$IMAGE_FOLDER|g" \
${ALLSKY_DIRECTORY}/service/apache_indi-allsky.conf > $TMP3


Expand Down Expand Up @@ -795,15 +813,10 @@ fi



echo "**** Setup image folder ****"
echo "**** Setup HTDOCS folder ****"
[[ ! -d "$HTDOCS_FOLDER" ]] && sudo mkdir "$HTDOCS_FOLDER"
sudo chmod 755 "$HTDOCS_FOLDER"
sudo chown -R "$USER":"$PGRP" "$HTDOCS_FOLDER"

[[ ! -d "$HTDOCS_FOLDER/images" ]] && mkdir "$HTDOCS_FOLDER/images"
chmod 775 "$HTDOCS_FOLDER/images"
[[ ! -d "$HTDOCS_FOLDER/images/darks" ]] && mkdir "$HTDOCS_FOLDER/images/darks"
chmod 775 "$HTDOCS_FOLDER/images/darks"
[[ ! -d "$HTDOCS_FOLDER/js" ]] && mkdir "$HTDOCS_FOLDER/js"
chmod 775 "$HTDOCS_FOLDER/js"

Expand All @@ -813,6 +826,19 @@ for F in $HTDOCS_FILES; do
done


echo "**** Setup image folder ****"
[[ ! -d "$IMAGE_FOLDER" ]] && sudo mkdir -p "$IMAGE_FOLDER"
sudo chmod 775 "$IMAGE_FOLDER"
sudo chown -R "$USER":"$PGRP" "$IMAGE_FOLDER"
[[ ! -d "${IMAGE_FOLDER}/darks" ]] && mkdir "${IMAGE_FOLDER}/darks"
chmod 775 "${IMAGE_FOLDER}/darks"

for F in $IMAGE_FOLDER_FILES; do
cp -f "${ALLSKY_DIRECTORY}/html/images/${F}" "${IMAGE_FOLDER}/${F}"
chmod 664 "${IMAGE_FOLDER}/${F}"
done


echo "**** Setup DB ****"
[[ ! -d "$DB_FOLDER" ]] && sudo mkdir "$DB_FOLDER"
sudo chmod 775 "$DB_FOLDER"
Expand Down

0 comments on commit bc91413

Please sign in to comment.