Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 65 additions & 41 deletions lycheesync/lycheedao.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def getUniqPhotoId(self):
nbtry = 1
while (self.photoIdExists(id)):
id = self.getUniqTimeBasedId()
time.sleep(1)
nbtry += 1
if (nbtry == 5):
raise Exception("didn't manage to create uniq id")
Expand All @@ -80,6 +81,7 @@ def getUniqAlbumId(self):
nbtry = 1
while (self.albumIdExists(id)):
id = self.getUniqTimeBasedId()
time.sleep(1)
nbtry += 1
if (nbtry == 5):
raise Exception("didn't manage to create uniq id")
Expand All @@ -90,8 +92,8 @@ def getUniqTimeBasedId(self):
id = str(int(time.time()))
# not precise enough
length = len(id)
if length < 14:
missing_char = 14 - length
if length < 10:
missing_char = 10 - length
r = random.random()
r = str(r)
# last missing_char char
Expand All @@ -101,7 +103,7 @@ def getUniqTimeBasedId(self):

def getAlbumNameDBWidth(self):
res = 50 # default value
query = "show columns from lychee_albums where Field='title'"
query = "show columns from albums where Field='title'"
cur = self.db.cursor()
try:
cur.execute(query)
Expand All @@ -127,8 +129,8 @@ def getAlbumMinMaxIds(self):
"""
returns min, max album ids
"""
min_album_query = "select min(id) as min from lychee_albums"
max_album_query = "select max(id) as max from lychee_albums"
min_album_query = "select min(id) as min from albums"
max_album_query = "select max(id) as max from albums"
try:
min = -1
max = -1
Expand Down Expand Up @@ -166,7 +168,7 @@ def updateAlbumDate(self, albumid, newdate):

res = True
try:
qry = "update lychee_albums set sysstamp= '" + str(newdate) + "' where id=" + str(albumid)
qry = "update albums set created_at= '" + str(newdate) + "' where id=" + str(albumid)
cur = self.db.cursor()
cur.execute(qry)
self.db.commit()
Expand All @@ -183,8 +185,8 @@ def changeAlbumId(self, oldid, newid):
Change albums id based on album titles (to affect display order)
"""
res = True
photo_query = "update lychee_photos set album = " + str(newid) + " where album = " + str(oldid)
album_query = "update lychee_albums set id = " + str(newid) + " where id = " + str(oldid)
photo_query = "update photos set album = " + str(newid) + " where album = " + str(oldid)
album_query = "update albums set id = " + str(newid) + " where id = " + str(oldid)
try:
cur = self.db.cursor()
cur.execute(photo_query)
Expand All @@ -206,7 +208,7 @@ def loadAlbumList(self):
"""
# Load album list
cur = self.db.cursor()
cur.execute("SELECT title,id from lychee_albums")
cur.execute("SELECT title,id from albums")
rows = cur.fetchall()
for row in rows:
self.albumslist[row['title']] = row['id']
Expand All @@ -218,7 +220,7 @@ def albumIdExists(self, album_id):
res = False
try:
cur = self.db.cursor()
cur.execute("select * from lychee_albums where id=%s", (album_id))
cur.execute("select * from albums where id=%s", (album_id))
row = cur.fetchall()
if len(row) != 0:
res = True
Expand All @@ -243,7 +245,7 @@ def getAlbumNameFromIdsList(self, list_id):
album_names = ''
try:
albumids = ','.join(list_id)
query = ("select title from lychee_albums where id in(" + albumids + ")")
query = ("select title from albums where id in(" + albumids + ")")
cur = self.db.cursor()
cur.execute(query)
rows = cur.fetchall()
Expand All @@ -255,11 +257,27 @@ def getAlbumNameFromIdsList(self, list_id):
finally:
return album_names

def getAlbumIdFromName(self, title, parent_id = "NULL"):
id = 0
try:
query = ("select id from albums where parent_id" + ("=" + str(parent_id) if (parent_id!= "NULL") else " is NULL") + " and title='" + title + "'")
cur = self.db.cursor()
cur.execute(query)
rows = cur.fetchall()
if len(rows) != 0:
id = rows[0]['id']
except Exception as e:
album_names = ''
logger.error('impossible to execute ' + query)
logger.exception(e)
finally:
return id

def photoIdExists(self, photoid):
res = None
try:
cur = self.db.cursor()
cur.execute("select id from lychee_photos where id=%s", (photoid))
cur.execute("select id from photos where id=%s", (photoid))
row = cur.fetchall()
if len(row) != 0:
logger.debug("photoExistsById %s", row)
Expand All @@ -273,7 +291,7 @@ def photoExistsByName(self, photo_name):
res = None
try:
cur = self.db.cursor()
cur.execute("select id from lychee_photos where title=%s", (photo_name))
cur.execute("select id from photos where title=%s", (photo_name))
row = cur.fetchall()
if len(row) != 0:
logger.debug("photoExistsByName %s", row)
Expand All @@ -294,7 +312,7 @@ def photoExists(self, photo):
try:
cur = self.db.cursor()
cur.execute(
"select * from lychee_photos where album=%s AND (title=%s OR checksum=%s)",
"select * from photos where album_id=%s AND (title=%s OR checksum=%s)",
(photo.albumid,
photo.originalname,
photo.checksum))
Expand All @@ -306,11 +324,11 @@ def photoExists(self, photo):

cur = self.db.cursor()
cur.execute(
"select album from lychee_photos where (title=%s OR checksum=%s)",
"select album_id from photos where (title=%s OR checksum=%s)",
(photo.originalname,
photo.checksum))
rows = cur.fetchall()
album_ids = [r['album'] for r in rows]
album_ids = [r['album_id'] for r in rows]
if len(album_ids) > 0:
logger.warn(
"a photo with this name: %s or checksum: %s already exists in at least another album: %s",
Expand All @@ -334,10 +352,11 @@ def createAlbum(self, album):
"""
album['id'] = str(self.getUniqAlbumId())

query = ("insert into lychee_albums (id, title, sysstamp, public, password) values ({},'{}',{},'{}',NULL)".format(
query = ("insert into albums (id, title, parent_id, created_at, public, password) values ({},'{}',{},'{}','{}',NULL)".format(
album['id'],
album['name'],
datetime.datetime.now().strftime('%s'),
album['parent_id'],
datetime.datetime.now().strftime('%Y-%m-%d %H:%M'),
str(self.conf["publicAlbum"]))
)

Expand All @@ -346,10 +365,14 @@ def createAlbum(self, album):
cur = self.db.cursor()
logger.debug("try to createAlbum: %s", query)
# duplicate of previous query to use driver quote protection features
cur.execute("insert into lychee_albums (id, title, sysstamp, public, password) values (%s,%s,%s,%s,NULL)", (album['id'], album['name'], datetime.datetime.now().strftime('%s'), str(self.conf["publicAlbum"])))
if album['parent_id']=="0":
query = "insert into albums (id, title, parent_id, created_at, updated_at, public, password, description) values ({},'{}',NULL,'{}','{}','{}',NULL,'')".format(album['id'], album['name'], datetime.datetime.now().strftime('%Y-%m-%d %H:%M'), datetime.datetime.now().strftime('%Y-%m-%d %H:%M'), str(self.conf["publicAlbum"]))
else:
query = "insert into albums (id, title, parent_id, created_at, updated_at, public, password, description) values ({},'{}','{}','{}','{}','{}',NULL,'')".format(album['id'], album['name'], album['parent_id'], datetime.datetime.now().strftime('%Y-%m-%d %H:%M'), datetime.datetime.now().strftime('%Y-%m-%d %H:%M'), str(self.conf["publicAlbum"]))
cur.execute(query)
self.db.commit()

cur.execute("select id from lychee_albums where title=%s", (album['name']))
cur.execute("select id from albums where title=%s", (album['name']))
row = cur.fetchone()
self.albumslist['name'] = row['id']
album['id'] = row['id']
Expand All @@ -369,8 +392,8 @@ def eraseAlbum(self, album_id):
Return list of the erased photo url
"""
res = []
query = "delete from lychee_photos where album = " + str(album_id) + ''
selquery = "select url from lychee_photos where album = " + str(album_id) + ''
query = "delete from photos where album = " + str(album_id) + ''
selquery = "select url from photos where album = " + str(album_id) + ''
try:
cur = self.db.cursor()
cur.execute(selquery)
Expand All @@ -388,7 +411,7 @@ def eraseAlbum(self, album_id):

def dropAlbum(self, album_id):
res = False
query = "delete from lychee_albums where id = " + str(album_id) + ''
query = "delete from albums where id = " + str(album_id) + ''
try:
cur = self.db.cursor()
cur.execute(query)
Expand All @@ -403,7 +426,7 @@ def dropAlbum(self, album_id):
def dropPhoto(self, photo_id):
""" delete a photo. parameter: photo_id """
res = False
query = "delete from lychee_photos where id = " + str(photo_id) + ''
query = "delete from photos where id = " + str(photo_id) + ''
try:
cur = self.db.cursor()
cur.execute(query)
Expand All @@ -422,9 +445,9 @@ def get_all_photos(self, album_id=None):
"""
res = []
if not(album_id):
selquery = "select id, url, album from lychee_photos"
selquery = "select id, url, album from photos"
else:
selquery = "select id, url, album from lychee_photos where album={}".format(album_id)
selquery = "select id, url, album from photos where album={}".format(album_id)

try:
cur = self.db.cursor()
Expand All @@ -445,7 +468,7 @@ def get_empty_albums(self):
res = []
try:
# check if exists in db
sql = "select id from lychee_albums where id not in(select distinct album from lychee_photos)"
sql = "select id from albums where id not in(select distinct album from photos)"
with self.db.cursor() as cursor:
cursor.execute(sql)
rows = cursor.fetchall()
Expand All @@ -462,7 +485,7 @@ def get_album_ids_titles(self):
res = None
try:
# check if exists in db
sql = "select id, title from lychee_albums"
sql = "select id, title from albums"
with self.db.cursor() as cursor:
cursor.execute(sql)
rows = cursor.fetchall()
Expand All @@ -483,31 +506,32 @@ def addFileToAlbum(self, photo):
"""
res = True
try:
stamp = parse(photo.exif.takedate + ' ' + photo.exif.taketime).strftime('%s')
stamp = parse(photo.exif.takedate + ' ' + photo.exif.taketime).strftime('%Y-%m-%d %H:%M')
except Exception as e:
stamp = datetime.datetime.now().strftime('%s')
stamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M')

query = ("insert into lychee_photos " +
query = ("insert into photos " +
"(id, url, public, type, width, height, " +
"size, star, " +
"thumbUrl, album,iso, aperture, make, " +
"thumbUrl, album_id,iso, aperture, make, " +
"model, shutter, focal, takestamp, " +
"description, title, checksum) " +
"description, title, checksum, tags, " +
"created_at, updated_at )" +
"values " +
"({}, '{}', {}, '{}', {}, {}, " +
"'{}', {}, " +
"'{}', '{}', '{}', '{}'," +
" '{}', " +
"'{}', '{}', '{}', '{}', " +
"'{}', %s, '{}')"
"'{}', %s, '{}', '{}'," +
"'{}', '{}' )"
).format(photo.id, photo.url, self.conf["publicAlbum"], photo.type, photo.width, photo.height,
photo.size, photo.star,
photo.thumbUrl, photo.albumid,
photo.exif.iso,
photo.exif.aperture,
photo.thumbUrl, photo.albumid, photo.exif.iso, photo.exif.aperture,
photo.exif.make,
photo.exif.model, photo.exif.shutter, photo.exif.focal, stamp,
photo.description, photo.checksum)
photo.description, photo.checksum, '',
stamp, stamp)
try:
logger.debug(query)
cur = self.db.cursor()
Expand All @@ -525,7 +549,7 @@ def reinitAlbumAutoIncrement(self):

min, max = self.getAlbumMinMaxIds()
if max:
qry = "alter table lychee_albums AUTO_INCREMENT=" + str(max + 1)
qry = "alter table albums AUTO_INCREMENT=" + str(max + 1)
try:
cur = self.db.cursor()
cur.execute(qry)
Expand All @@ -549,8 +573,8 @@ def dropAll(self):
"""
try:
cur = self.db.cursor()
cur.execute("delete from lychee_albums")
cur.execute("delete from lychee_photos")
cur.execute("delete from albums")
cur.execute("delete from photos")
self.db.commit()
except Exception as e:
logger.exception(e)
2 changes: 1 addition & 1 deletion lycheesync/lycheemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def __init__(self, id, conf, photoname, album):
if ('star' in self.originalname) or ('cover' in self.originalname):
self.star = 1

assert len(self.id) == 14, "id {} is not 14 character long: {}".format(self.id, str(len(self.id)))
assert len(self.id) == 10, "id {} is not 10 character long: {}".format(self.id, str(len(self.id)))

# Compute file storage url
m = hashlib.md5()
Expand Down
24 changes: 23 additions & 1 deletion lycheesync/lycheesyncer.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,29 @@ def sync(self):
# Fill in other album properties
# albumnames start at srcdir (to avoid absolute path albumname)
album['relpath'] = os.path.relpath(album['path'], self.conf['srcdir'])
album['name'] = self.getAlbumNameFromPath(album)
# album['name'] = self.getAlbumNameFromPath(album)
album['name'] = (album['relpath'].split(os.sep))[-1]
album['parent_id']="0"
if (len(album['relpath'].split(os.sep)) != 1):
#Get parent_id
path = album['relpath'].split(os.sep)

tmpparentid = "0"

for name in path:
if name==path[-1]:
album['parent_id'] = tmpparentid
else:
tmp = self.dao.getAlbumIdFromName(name, "NULL" if name==path[0] else tmpparentid)
if tmp==0:
albumtmp={}
albumtmp['name']=name
albumtmp['parent_id']=tmpparentid
tmpparentid=self.createAlbum(albumtmp)
else:
tmpparentid=tmp



if len(album['name']) > album_name_max_width:
logger.warn("album name too long, will be truncated " + album['name'])
Expand Down
Loading