This commit is contained in:
Felipe Martin 2022-10-08 17:55:12 +02:00
commit 7907c755ac
Signed by: fmartingr
GPG Key ID: 716BC147715E716F
45 changed files with 2468 additions and 0 deletions

13
.gitignore vendored Normal file
View File

@ -0,0 +1,13 @@
# Editor
.vscode
# Python
*.py[o|c]
# Test files
testphotos/
.DS_Store
db.sqlite3
*.egg-*

22
.pre-commit-config.yaml Normal file
View File

@ -0,0 +1,22 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.2.3
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: flake8
- repo: https://github.com/asottile/seed-isort-config
rev: v1.9.2
hooks:
- id: seed-isort-config
- repo: https://github.com/pre-commit/mirrors-isort
rev: v4.3.20
hooks:
- id: isort
- repo: https://github.com/ambv/black
rev: stable
hooks:
- id: black
language_version: python3.7

12
Dockerfile Normal file
View File

@ -0,0 +1,12 @@
FROM alpine:latest
ENV PYTHON_VERSION=3.8.2-r0
ENV BUILD_DIR "/tmp/build"
WORKDIR ${BUILD_DIR}
COPY . ${BUILD_DIR}
RUN apk --update add python3-dev==${PYTHON_VERSION} gcc musl-dev libffi-dev openssl-dev && \
pip3 install poetry && \
poetry install
CMD ["poetry", "run", "python", "memories/manage.py", "runserver"]

13
README.md Normal file
View File

@ -0,0 +1,13 @@
# Memories
Proof of concept for a photo gallery manager.
## Sumary
- **Python** 3.7+
- Tests: [![builds.sr.ht status](https://builds.sr.ht/~fmartingr/memories/test.yml.svg)](https://builds.sr.ht/~fmartingr/memories/test.yml?)
- Documentation: TODO
## References
- 360 video: https://github.com/google/spatial-media/blob/master/docs/spherical-video-v2-rfc.md

17
docker-compose.yml Normal file
View File

@ -0,0 +1,17 @@
version: '3.3'
services:
# web:
# build: .
# ports:
# - "8000:8000"
db:
image: fmartingr/postgres
ports:
- "5432:5432"
environment:
DB_USER: memories
DB_PASS: memories
DB_NAME: memories
DB_EXTENSION: hstore

0
memories/__init__.py Normal file
View File

View File

View File

@ -0,0 +1,10 @@
from django.contrib import admin
from .models import Picture
class PictureAdmin(admin.ModelAdmin):
list_display = ("file_path", "creation_date", "checksum", "metadata", )
admin.site.register(Picture, PictureAdmin)

View File

@ -0,0 +1,5 @@
from django.apps import AppConfig
class CollectionConfig(AppConfig):
name = 'collection'

View File

@ -0,0 +1,22 @@
# Generated by Django 3.0.6 on 2020-05-10 18:30
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Picture',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('file_path', models.FilePathField()),
('creation_date', models.DateTimeField()),
],
),
]

View File

@ -0,0 +1,24 @@
# Generated by Django 3.0.6 on 2020-05-10 18:42
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('collection', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='picture',
name='metadata',
field=models.TextField(blank=True, null=True),
),
migrations.AddField(
model_name='picture',
name='checksum',
field=models.CharField(default=123, max_length=40),
preserve_default=False,
),
]

View File

@ -0,0 +1,19 @@
# Generated by Django 3.0.6 on 2020-05-10 19:08
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('collection', '0002_auto_20200510_1842'),
]
operations = [
migrations.AddField(
model_name='picture',
name='mimetype',
field=models.CharField(default=123, max_length=64),
preserve_default=False,
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 3.0.6 on 2020-05-10 21:56
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('collection', '0003_picture_mimetype'),
]
operations = [
migrations.AddField(
model_name='picture',
name='kind',
field=models.CharField(choices=[('picture', 'Picture'), ('video', 'Video')], default='picture', max_length=24),
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 3.0.6 on 2020-05-10 22:01
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('collection', '0004_picture_kind'),
]
operations = [
migrations.AddField(
model_name='picture',
name='raw',
field=models.BooleanField(default=False),
),
]

View File

@ -0,0 +1,22 @@
# Generated by Django 3.0.6 on 2020-05-11 11:11
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('collection', '0005_picture_raw'),
]
operations = [
migrations.AlterModelOptions(
name='picture',
options={'ordering': ('creation_date',)},
),
migrations.AlterField(
model_name='picture',
name='creation_date',
field=models.DateTimeField(db_index=True),
),
]

View File

@ -0,0 +1,23 @@
# Generated by Django 3.0.6 on 2020-05-11 11:14
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('collection', '0006_auto_20200511_1111'),
]
operations = [
migrations.AddField(
model_name='picture',
name='exif',
field=models.TextField(blank=True, null=True),
),
migrations.AddField(
model_name='picture',
name='stat',
field=models.TextField(blank=True, null=True),
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 3.0.6 on 2020-05-11 18:55
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('collection', '0007_auto_20200511_1114'),
]
operations = [
migrations.AlterField(
model_name='picture',
name='file_path',
field=models.FilePathField(max_length=255),
),
]

View File

@ -0,0 +1,17 @@
# Generated by Django 3.0.6 on 2020-05-31 19:26
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('collection', '0008_auto_20200511_1855'),
]
operations = [
migrations.AlterModelOptions(
name='picture',
options={},
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 3.0.6 on 2020-05-31 19:27
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('collection', '0009_auto_20200531_1926'),
]
operations = [
migrations.RenameField(
model_name='picture',
old_name='checksum',
new_name='checksum',
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 3.0.6 on 2020-05-31 19:28
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('collection', '0010_auto_20200531_1927'),
]
operations = [
migrations.AlterField(
model_name='picture',
name='checksum',
field=models.CharField(max_length=64),
),
]

View File

@ -0,0 +1,83 @@
import os
import os.path
import stat
import json
from datetime import datetime
from django.db import models
class Stat:
stat_result: os.stat_result
def __init__(self, stat_result):
self.stat_result = stat_result
def __dict__(self):
return {
"mode": self.mode,
"access_time": self.access_time,
"modified_time": self.modified_time,
"creation_time": self.creation_time,
"birth_time": self.birth_time,
}
def as_dict(self):
return self.__dict__()
@property
def mode(self):
return self.stat_result["st_mode"]
@property
def access_time(self):
return datetime.fromtimestamp(self.stat_result["st_atime"])
@property
def modified_time(self):
return datetime.fromtimestamp(self.stat_result["st_mtime"])
@property
def creation_time(self):
return datetime.fromtimestamp(self.stat_result["st_ctime"])
@property
def birth_time(self):
if "st_birthtime" in self.stat_result:
return datetime.fromtimestamp(self.stat_result["st_birthtime"])
return None
class Picture(models.Model):
PICTURE = "picture"
# PICTURE_360 = 'picture-360'
# PANORAMA = 'picture-panorama'
VIDEO = "video"
# VIDEO_360 = 'video-360'
KIND_CHOICES = (
(PICTURE, PICTURE.capitalize()),
# (PICTURE_360, "Picture 360"),
# (PANORAMA, PANORAMA.capitalize()),
(VIDEO, VIDEO.capitalize()),
# (VIDEO_360, "Video 360"),
)
file_path = models.FilePathField(max_length=255)
creation_date = models.DateTimeField(db_index=True)
checksum = models.CharField(max_length=64)
metadata = models.TextField(blank=True, null=True)
exif = models.TextField(blank=True, null=True)
stat = models.TextField(blank=True, null=True)
mimetype = models.CharField(max_length=64)
raw = models.BooleanField(default=False)
kind = models.CharField(choices=KIND_CHOICES, default=PICTURE, max_length=24)
@property
def filename(self):
return os.path.basename(self.file_path)
def get_stat(self):
return Stat(json.loads(self.stat))
# class Meta:
# ordering = ("creation_date", )

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

177
memories/collection/poc.py Normal file
View File

@ -0,0 +1,177 @@
import os
from datetime import datetime
import mimetypes
from typing import Text
from dataclasses import dataclass
import hashlib
import subprocess
import mutagen
LIBRARY_PATH = "/home/fmartingr/Code/memories/testphotos"
RAW_MIMETYPES = {
# RAW pictures
"ARW": "image/x-sony-arw",
"CR2": "image/x-canon-cr2",
"CR3": "image/x-canon-cr3",
"CRW": "image/x-canon-crw",
"DCR": "image/x-kodak-dcr",
"DNG": "image/x-adobe-dng",
"ERF": "image/x-epson-erf",
"K25": "image/x-kodak-k25",
"KDC": "image/x-kodak-kdc",
"MRW": "image/x-minolta-mrw",
"NEF": "image/x-nikon-nef",
"ORF": "image/x-olympus-orf",
"PEF": "image/x-pentax-pef",
"RAF": "image/x-fuji-raf",
"RAW": "image/x-panasonic-raw",
"SR2": "image/x-sony-sr2",
"SRF": "image/x-sony-srf",
"X3F": "image/x-sigma-x3f",
}
# High Efficiency Image/Video (apple)
APPLE_MIMETYPES = {"HEIC": "image/heic", "HEIF": "image/heif", "HEVC": "video/hevc"}
CUSTOM_MIMETYPES = {}
CUSTOM_MIMETYPES.update(RAW_MIMETYPES)
CUSTOM_MIMETYPES.update(APPLE_MIMETYPES)
for extension, mimetype in CUSTOM_MIMETYPES.items():
mimetypes.add_type(mimetype, f".{extension}")
mimetypes.add_type(mimetype, f".{extension.lower()}")
def read_exif(path):
output = {}
with subprocess.Popen(["exiftool", path], stdout=subprocess.PIPE) as proc:
for line in proc.stdout.readlines():
key, value = line.decode("utf-8").strip().split(":", maxsplit=1)
output[key.strip()] = value.strip()
return output
@dataclass
class File:
__mro__ = {"path", "_type"}
path: str
@property
def mimetype(self) -> Text:
"""Retrieves the file mimetype by extension"""
if not getattr(self, "_mimetype", False):
self._mimetype, _ = mimetypes.guess_type(self.path)
if not self._mimetype:
print(f"Can't guess type of file {self.path}")
return self._mimetype
@property
def is_raw(self) -> bool:
return self.mimetype in RAW_MIMETYPES.values()
@property
def is_image(self) -> bool:
return "image" in self.mimetype
@property
def is_video(self) -> bool:
return "video" in self.mimetype
@property
def stat(self):
stat = os.stat(self.path)
return {k: getattr(stat, k) for k in dir(stat) if k.startswith("st_")}
@property
def exif(self) -> dict:
"""
Retrieve EXIF data from the file and merge it with wathever mutagen finds in there for video files.
"""
if not getattr(self, "_exif", False):
self._exif = read_exif(self.path)
if self.is_video:
self._exif.update(mutagen.File(self.path))
return self._exif
def get_datetime(self) -> datetime:
"""
Tries to guess the original datetime for the provided file.
This is done extracting several EXIF values and the file birthdate/modification date.
The oldest one is the winner.
"""
CREATION_DATE_EXIF_KEYS = (
"Content Create Date",
"Date/Time Original",
"Create Date",
"Date Created",
"File Modification Date/Time",
)
datetimes = []
for key in CREATION_DATE_EXIF_KEYS:
try:
dt = datetime.strptime(self.exif[key], "%Y:%m:%d %H:%M:%S%z")
datetimes.append(dt.replace(tzinfo=None))
except KeyError:
pass
except ValueError:
try:
cleaned = self.exif[key].rsplit(".", maxsplit=1)
datetimes.append(datetime.strptime(cleaned[0], "%Y:%m:%d %H:%M:%S"))
except ValueError:
pass
# Last resort, use file creation/modification date
stat = os.stat(self.path)
try:
datetimes.append(datetime.fromtimestamp(stat.st_birthtime))
except AttributeError:
# Linux: No easy way to get creation dates here,
# so we'll settle for when its content was last modified.
datetimes.append(datetime.fromtimestamp(stat.st_mtime))
sorted_datetimes = sorted(datetimes)
return sorted_datetimes[0]
@property
def datetime(self):
if not getattr(self, "_datetime", False):
self._datetime = self.get_datetime()
return self._datetime
@property
def filename(self):
return os.path.splitext(os.path.basename(self.path))[0]
@property
def extension(self):
return os.path.splitext(self.path)[1][1:].lower()
@property
def checksum(self) -> Text:
if not getattr(self, "_checksum", False):
digest = hashlib.sha224()
with open(self.path, "rb") as handler:
digest.update(handler.read())
self._checksum = f"sha224-{digest.hexdigest()}"
return self._checksum
def as_dict(self):
return {
"path": self.path,
"filename": self.filename,
"extension": self.extension,
"checksum": self.checksum,
"datetime": self.datetime,
"exif": self.exif,
}
def get_files():
for root, dirs, files in os.walk(LIBRARY_PATH):
for filename in files:
yield File(path=os.path.join(root, filename))

View File

@ -0,0 +1,133 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.8.2/css/bulma.min.css" />
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
.item-list {
display: flex;
flex-wrap: wrap;
flex-direction: row;
flex-grow: 1;
justify-content: flex-start;
align-items: baseline;
}
.item-list .item {
flex: 1;
margin: 10px;
padding: 10px;
text-align: center;
}
.item-list .item .container {
margin: 0 auto;
vertical-align: middle;
display: inline-block;
position: relative;
}
.item-list .item .container .floaty {
position: absolute;
right: 10px;
bottom: 10px;
}
.item-list .item .container img {
border-radius: 12px;
max-height: 150px;
max-width: 225px;
}
.item-list .item img {
box-shadow: 0px 0px 6px black;
}
.show-on-anchor {
display: none;
}
.show-on-anchor:target {
display: block
}
.preview {
max-height: 100%;
}
</style>
<title>Memories</title>
</head>
<body>
<nav class="navbar is-link" role="navigation" aria-label="main navigation">
<div class="navbar-brand">
<a class="navbar-item has-text-weight-bold" href="/">
Memories
</a>
<a role="button" class="navbar-burger burger" aria-label="menu" aria-expanded="false"
data-target="navbarBasicExample">
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
</a>
</div>
<div id="navbarBasicExample" class="navbar-menu">
<div class="navbar-start">
<a href="{% url "list" %}" class="navbar-item is-active"><i class="material-icons">photo_library</i> Gallery</a>
<a href="{% url "list" %}" class="navbar-item"><i class="material-icons">folder</i> Albums</a>
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link">
Dropdown
</a>
<div class="navbar-dropdown">
<a class="navbar-item">
About
</a>
<a class="navbar-item">
Jobs
</a>
<a class="navbar-item">
Contact
</a>
<hr class="navbar-divider">
<a class="navbar-item">
Report an issue
</a>
</div>
</div>
</div>
<div class="navbar-end">
<div class="navbar-item">
<form action="">
<input type="text" class="input" placeholder="Search...">
</form>
</div>
<div class="navbar-item">
<div class="buttons">
<a class="button is-primary">
<strong>Sign up</strong>
</a>
<a class="button is-light">
Log in
</a>
</div>
</div>
</div>
</div>
</nav>
{% block content %}{% endblock %}
</body>
</html>

View File

@ -0,0 +1,59 @@
{% extends "base.html" %}
{% block content %}
<h2>Picture detail</h2>
<div>
<a href="#preview">Preview</a> | <a href="#details">Details</a> | <a href="#stat">Stat</a> | <a href="#exif">EXIF</a>
</div>
<div id="preview" class="show-on-anchor">
<h3>Preview</h3>
{% if picture.raw %}
RAW display in browser not available
{% elif "video" in picture.mimetype %}
<video class="preview" src="{% url 'video' picture.id %}" controls="on"></video>
{% else %}
<img class="preview" src="{% url 'image' picture.id %}" />
{% endif %}
</div>
<div id="details" class="show-on-anchor">
<h3>Details</h3>
<ul>
<li><b>Filename:</b> {{ picture.filename }}</li>
<li><b>File path:</b> {{ picture.file_path }}</li>
<li><b>Type:</b> {{ picture.kind }}</li>
<li><b>Mimetype</b>: {{ picture.mimetype }}</li>
<li><b>RAW File:</b> {{ picture.raw }}</li>
<li><b>Creation date:</b> {{ picture.creation_date }}</li>
<li><b>SHA1 Checksum:</b> {{ picture.checksum }}</li>
</ul>
</div>
<div id="stat" class="show-on-anchor">
<h3>Stat (parsed)</h3>
<ul>
{% for key, value in picture.get_stat.as_dict.items %}
<li><b>{{ key }}</b>: {{ value }}</li>
{% endfor %}
</ul>
<hr>
<h3>Stat (raw)</h3>
<ul>
{% for key, value in picture.get_stat.stat_result.items %}
<li><b>{{ key }}</b>: {{ value }}</li>
{% endfor %}
</ul>
</div>
<div id="exif" class="show-on-anchor">
<h3>EXIF</h3>
<ul>
{% for key, value in exif.items %}
<li><b>{{ key }}</b>: {{ value }}</li>
{% endfor %}
</ul>
</div>
{% endblock %}

View File

@ -0,0 +1,36 @@
{% extends "base.html" %}
{% block content %}
<h2>Gallery</h2>
<div>
<b>Sort by</b> {% for sort_by_key, sort_by_name in sort_by_options.items %} | {% if sort_by != sort_by_key %}<a
href="?sort_by={{ sort_by_key }}">{% else %}<b>{% endif %}{{ sort_by_name }}{% if sort_by != sort_by_key %}</a>{% else %}</b>{% endif %}{% endfor %}
</div>
<div>
<b>Year</b> | {% for year in years %}{% if selected_year != year %}<a
href="{% url "list-year" year %}?sort_by={{ sort_by }}">{% endif %}{{ year }}{% if selected_year != year %}</a>{% endif %}{% if not forloop.last %}
| {% endif %}{% endfor %}
</div>
{% if months %}
<div>
<b>Month</b> | {% for month in months %}{% if selected_month != month %}<a
href="{% url "list-year-month" selected_year month %}?sort_by={{ sort_by }}">{% endif %}{{ month }}{% if selected_month != month %}</a>{% endif %}{% if not forloop.last %}
| {% endif %}{% endfor %}
</div>
{% endif %}
<div class="item-list">
{% for picture in pictures %}
<div class="item">
<div class="container">
<div class="floaty tag is-black">{{ picture.mimetype }}</div>
<a href="{% url "detail" picture.id %}#preview">
<div>{{ picture.creation_date}}</div>
<img src="{% url "thumbnail" picture.id %}" />
</a>
</div>
</div>
{% endfor %}
</div>
{% endblock %}

View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View File

@ -0,0 +1,15 @@
import subprocess
from PIL import Image
def get_thumbnail_from_video(path, checksum):
cmd = ["ffmpeg", "-y", f"-i", f"{path}", "-vframes", "1", "-vf", "scale=480:-2", "-q:v", "3", f"/tmp/{checksum}-thumb-320.jpg"]
subprocess.call(cmd)
image = Image.open(f"/tmp/{checksum}-thumb-320.jpg")
play = Image.open("/home/fmartingr/Code/memories/memories/collection/play.png")
play = play.resize((round(play.size[0] * 0.5), round(play.size[1] * 0.5)))
image.paste(
play, (int(image.size[0] / 2 - play.size[0] / 2), int(image.size[1] / 2 - play.size[1] / 2)), play
)
image.save(f"/tmp/{checksum}-thumb-320.jpg")
return open(f"/tmp/{checksum}-thumb-320.jpg", "rb").read()

View File

@ -0,0 +1,173 @@
import json
import os
import pyheif
import rawpy
from django.shortcuts import render, get_object_or_404, reverse
from django.http.response import HttpResponse, HttpResponseRedirect
from django.views.decorators.cache import cache_page
from PIL import Image
from collection.poc import get_files
from collection.models import Picture
from collection.video import get_thumbnail_from_video
def gather_view(request):
Picture.objects.all().delete()
for file in get_files():
try:
Picture.objects.get(file_path=file.path, checksum=file.checksum)
except Picture.DoesNotExist:
Picture.objects.create(
file_path=file.path,
kind=Picture.VIDEO if file.is_video else Picture.PICTURE,
raw=file.is_raw,
creation_date=file.datetime,
checksum=file.checksum,
exif=json.dumps(file.exif),
stat=json.dumps(file.stat),
mimetype=file.mimetype,
)
for file in Picture.objects.all():
if not os.path.exists(file.file_path):
file.delete()
return HttpResponse("ok")
DEFAULT_SORT_BY = "-creation_date"
SORT_BY_OPTIONS = {
"creation_date": "Creation date ASC",
"-creation_date": "Creation date DESC",
}
def list_view(request):
sort_by = request.GET.get("sort_by")
if sort_by not in SORT_BY_OPTIONS.keys():
return HttpResponseRedirect(reverse("list") + f"?sort_by={DEFAULT_SORT_BY}")
pictures = Picture.objects.all().order_by(DEFAULT_SORT_BY)
years = tuple(
Picture.objects.all()
.distinct("creation_date__year")
.values_list("creation_date__year", flat=True)
)
return render(
template_name="list.html",
request=request,
context=dict(
pictures=pictures,
years=years,
sort_by=sort_by,
sort_by_options=SORT_BY_OPTIONS,
),
)
def list_year_month(request, year: int, month: int = None):
sort_by = request.GET.get("sort_by")
pictures = Picture.objects.filter(creation_date__year=year)
years = tuple(
Picture.objects.all()
.distinct("creation_date__year")
.values_list("creation_date__year", flat=True)
)
months = tuple(
pictures.distinct("creation_date__month").values_list(
"creation_date__month", flat=True
)
)
if month:
pictures = pictures.filter(creation_date__month=month)
return render(
template_name="list.html",
request=request,
context=dict(
pictures=pictures.order_by(DEFAULT_SORT_BY),
years=years,
months=months,
selected_year=year,
selected_month=month,
),
)
def detail_view(request, picture_id):
picture = get_object_or_404(Picture, id=picture_id)
exif = {
k: v
for k, v in sorted(json.loads(picture.exif).items(), key=lambda item: item[0])
}
file_stat = {
k: v
for k, v in sorted(json.loads(picture.stat).items(), key=lambda item: item[0])
}
return render(
template_name="detail.html",
request=request,
context=dict(picture=picture, exif=exif, file_stat=file_stat),
)
def image_view(request, picture_id):
picture = get_object_or_404(Picture, id=picture_id)
return HttpResponse(open(picture.file_path, "rb").read())
def thumbnail_view(request, picture_id):
picture = get_object_or_404(Picture, id=picture_id)
if picture.kind == picture.VIDEO:
thumbnail = get_thumbnail_from_video(picture.file_path, picture.checksum)
return HttpResponse(thumbnail, content_type="image/jpg")
if picture.raw:
try:
with rawpy.imread(picture.file_path) as raw:
# raises rawpy.LibRawNoThumbnailError if thumbnail missing
# raises rawpy.LibRawUnsupportedThumbnailError if unsupported format
thumb = raw.extract_thumb()
if thumb.format == rawpy.ThumbFormat.JPEG:
# thumb.data is already in JPEG format, save as-is
return HttpResponse(thumb.data, content_type="image/jpeg")
elif thumb.format == rawpy.ThumbFormat.BITMAP:
# thumb.data is an RGB numpy array, convert with imageio
return HttpResponse(thumb.data, content_type="image/bitmap")
except rawpy.LibRawFileUnsupportedError:
return HttpResponse(open("photo.png", "rb"), content_type="image/png")
if picture.mimetype == "image/heic":
heif_file = pyheif.read_heif(picture.file_path)
image = Image.frombytes(
mode=heif_file.mode, size=heif_file.size, data=heif_file.data
)
image.thumbnail((480, 480))
image.save(f"/tmp/thumb-480-{picture.checksum}.jpg")
return HttpResponse(
open(f"/tmp/thumb-480-{picture.checksum}.jpg", "rb").read(),
content_type="image/jpg",
)
if picture.kind == picture.VIDEO:
return HttpResponse(open("video.png", "rb"), content_type="image/png")
if picture.kind == picture.PICTURE:
image = Image.open(picture.file_path)
image.thumbnail((480, 480))
image.save(f"/tmp/thumb-480-{picture.checksum}.jpg")
return HttpResponse(
open(f"/tmp/thumb-480-{picture.checksum}.jpg", "rb").read(),
content_type="image/jpg",
)
video_view = image_view

View File

81
memories/core/db.py Normal file
View File

@ -0,0 +1,81 @@
import os
from datetime import datetime
from playhouse.postgres_ext import PostgresqlExtDatabase, HStoreField, Model
from peewee import CharField, DateTimeField
database = PostgresqlExtDatabase(
"memories",
user="memories",
password="memories",
host="127.0.0.1",
port=5432,
register_hstore=True,
)
class Item(Model):
PICTURE = "picture"
VIDEO = "video"
KIND_CHOICES = ((PICTURE, "Picture"), (VIDEO, "Video"))
file_path = CharField()
kind = CharField(choices=KIND_CHOICES, default=PICTURE, index=True)
creation_date = DateTimeField()
checksum = CharField(max_length=63)
metadata = HStoreField()
exif = HStoreField()
stat = HStoreField()
mimetype = CharField()
class Meta:
database = database
@property
def filename(self):
return os.path.basename(self.file_path)
def get_stat(self):
return Stat(self.stat)
class Stat:
stat_result: os.stat_result
def __init__(self, stat_result):
self.stat_result = stat_result
def __dict__(self):
return {
"mode": self.mode,
"access_time": self.access_time,
"modified_time": self.modified_time,
"creation_time": self.creation_time,
"birth_time": self.birth_time,
}
def as_dict(self):
return self.__dict__()
@property
def mode(self):
return self.stat_result["st_mode"]
@property
def access_time(self):
return datetime.fromtimestamp(self.stat_result["st_atime"])
@property
def modified_time(self):
return datetime.fromtimestamp(self.stat_result["st_mtime"])
@property
def creation_time(self):
return datetime.fromtimestamp(self.stat_result["st_ctime"])
@property
def birth_time(self):
if "st_birthtime" in self.stat_result:
return datetime.fromtimestamp(self.stat_result["st_birthtime"])
return None

21
memories/manage.py Executable file
View File

@ -0,0 +1,21 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'memories.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()

View File

16
memories/memories/asgi.py Normal file
View File

@ -0,0 +1,16 @@
"""
ASGI config for memories project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/
"""
import os
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'memories.settings')
application = get_asgi_application()

View File

@ -0,0 +1,118 @@
"""
Django settings for memories project.
Generated by 'django-admin startproject' using Django 3.0.6.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "^-jw5=h-ockcmwgj!vj2q+23!&q$sf#+h!134pg()_x3w#d+6o"
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"collection",
]
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]
ROOT_URLCONF = "memories.urls"
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
]
},
}
]
WSGI_APPLICATION = "memories.wsgi.application"
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "memories",
"USER": "memories",
"PASSWORD": "memories",
"HOST": "localhost",
}
}
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator"
},
{"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator"},
{"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator"},
{"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator"},
]
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
LANGUAGE_CODE = "en-us"
TIME_ZONE = "UTC"
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = "/static/"

39
memories/memories/urls.py Normal file
View File

@ -0,0 +1,39 @@
"""memories URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from collection.views import (
gather_view,
list_view,
detail_view,
image_view,
thumbnail_view,
video_view,
list_year_month,
)
urlpatterns = [
path("admin/", admin.site.urls),
path("gather/", gather_view),
path("", list_view, name="list"),
path("year/<int:year>", list_year_month, name="list-year"),
path("year/<int:year>/<int:month>", list_year_month, name="list-year-month"),
path("<int:picture_id>", detail_view, name="detail"),
path("img/<int:picture_id>", image_view, name="image"),
path("thumb/<int:picture_id>", thumbnail_view, name="thumbnail"),
path("video/<int:picture_id>", video_view, name="video"),
]

16
memories/memories/wsgi.py Normal file
View File

@ -0,0 +1,16 @@
"""
WSGI config for memories project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'memories.settings')
application = get_wsgi_application()

View File

@ -0,0 +1,4 @@
from memories.server.app import app
__all__ = ("app", )

56
memories/server/app.py Normal file
View File

@ -0,0 +1,56 @@
import os.path
from fastapi import FastAPI, APIRouter
from pydantic.types import UUID4
from memories.collection.poc import get_files
from memories.core.db import Item, database
app = FastAPI(title="Memories")
# database.connect()
# database.create_tables([Item])
# database.close()
router_items = APIRouter()
@app.get("/healthz")
async def healthz():
return {"status": "ok"}
@router_items.get("/")
async def items_list():
return []
@router_items.post("/")
async def item_create():
return {}
@router_items.get("/{item_id}")
async def item_detail(item_id: UUID4):
return {"item_id": item_id}
app.include_router(router_items, prefix="/items", tags=["items"])
@app.get("/test/sync")
async def test_sync_view():
with database.atomic():
for item in Item.select():
if not os.path.exists(item.file_path):
item.delete()
for file in get_files():
if not Item.select().where(Item.file_path == file.path).exists():
Item.create(
file_path=file.path,
kind=Item.VIDEO if file.is_video else Item.PICTURE,
metadata={"raw": file.is_raw, "panorama": False},
creation_date=file.datetime,
checksum=file.checksum,
exif=file.exif,
stat=file.stat,
mimetype=file.mimetype,
)

BIN
photo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 592 B

1083
poetry.lock generated Normal file

File diff suppressed because it is too large Load Diff

30
pyproject.toml Normal file
View File

@ -0,0 +1,30 @@
[tool.poetry]
name = "memories"
version = "0.1.0a1"
description = ""
authors = ["Felipe Martin <me@fmartingr.com>"]
[tool.poetry.dependencies]
python = "^3.7"
mutagen = "^1.44.0"
django = "^3.0.6"
rawpy = "^0.14.0"
psycopg2 = "^2.8.5"
numpy = "^1.18.4"
Pillow = "^7.1.2"
pyheif = "^0.4"
peewee = "^3.13.3"
fastapi = "^0.54.2"
uvicorn = "^0.11.5"
[tool.poetry.dev-dependencies]
black = {version = "^18.3-alpha.0", allow-prereleases = true}
flake8 = "^3.7"
isort = "^4.3"
pre-commit = "^1.18"
rope = "^0.14.0"
ipdb = "^0.12.2"
[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"

16
setup.cfg Normal file
View File

@ -0,0 +1,16 @@
[flake8]
ignore = E203, E266, E501, W503, F403
max-line-length = 88
max-complexity = 18
select = B,C,E,F,W,T4,B9
[isort]
use_parentheses = True
multi_line_output = 3
include_trailing_comma = True
length_sort = 1
lines_between_types = 0
line_length = 88
known_third_party = click,django,docker,factory,pydantic,pytest,requests,toml
sections = FUTURE, STDLIB, THIRDPARTY, FIRSTPARTY, LOCALFOLDER
no_lines_before = LOCALFOLDER

BIN
video.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 583 B