-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocess.py
More file actions
215 lines (186 loc) · 7.18 KB
/
Copy pathprocess.py
File metadata and controls
215 lines (186 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#! /usr/bin/env python
"""file processing methods"""
import os, sqlite3, shutil, time, datetime, logging
import util, aws
from db import *
from app import app
# set up logging
logger = logging.getLogger('cigarbox')
def photosetsCreate(title,description=None):
"""create a photoset - takes title and optional description. returns id"""
try:
photoset = Photoset.get(Photoset.title == title)
except Photoset.DoesNotExist:
logger.info('creating photoset: %s',title)
photoset = Photoset.create(title=title,description=description)
return photoset.id
def saveImportMeta(photo_id,filename,importSource,sha1,S3=False,clientfilename=None):
importPath = os.path.abspath(filename)
fileDate = time.ctime(os.path.getmtime(filename))
try:
meta = ImportMeta.get(ImportMeta.photo == photo_id)
except ImportMeta.DoesNotExist:
meta = ImportMeta.create(photo=photo_id,filedate=fileDate,importpath=clientfilename,importsource=importSource,s3=S3,sha1=sha1)
logger.info('recording import meta for photo id: %s sha1: %s' % (photo_id,sha1))
return meta.id
def checkImportStatusS3(photo_id):
try:
importStatusS3 = ImportMeta.get(ImportMeta.photo == photo_id,ImportMeta.s3 == True)
return True
except ImportMeta.DoesNotExist:
return False
def photosetsAddPhoto(photoset_id,photo_id):
try:
PhotoPhotoset.get(PhotoPhotoset.photoset == photoset_id,PhotoPhotoset.photo == photo_id)
return True
except PhotoPhotoset.DoesNotExist:
PhotoPhotoset.create(photoset=photoset_id,photo=photo_id)
logger.info('adding photo id: %s to photoset id: %s',photo_id,photoset_id,)
except Exception as e:
return e
def getSha1FromPhotoID (photo_id):
photo = Photo.get(Photo.id == photo_id)
return(photo.sha1)
def getPhotoIDFromSha1 (sha1):
photo = Photo.get(Photo.sha1 == sha1)
return(photo.id)
def getOriginalPhotoName (photo_id):
sha1 = getSha1FromPhotoID(photo_id)
secret_key = app.config['SECRET_KEY']
# some as yet undefined sorcery
return(originalPhotoName)
def setPhotoPrivacy(photo_id,privacy):
"""set privacy"""
privacyNum = app.config['PRIVACYFLAGS'][privacy]
logger.info('privacy: %s for photo id: %s', privacy,photo_id)
try:
q = Photo.update(privacy=privacyNum).where(Photo.id == photo_id)
q.execute()
return True
except Exception as e:
return e
def addPhotoToDB(sha1,fileType,dateTaken):
"""adds photo to photos table, returns photo_id"""
try:
photo = Photo.get(Photo.sha1 == sha1)
return photo.id
except Photo.DoesNotExist:
logger.info('Adding to DB: %s %s %s', sha1,fileType,dateTaken)
photo = Photo.create(sha1=sha1,filetype=fileType,datetaken=dateTaken)
return photo.id
except Exception as e:
return e
def replacePhoto(photo_id,sha1,fileType,dateTaken):
"""replaces a photo based on photo_id, returns new sha1"""
try:
logger.info('Replacing photo_id %s with %s %s', photo_id, sha1, dateTaken)
q = Photo.update(sha1=sha1,filetype=fileType,datetaken=dateTaken).where(Photo.id == photo_id)
q.execute()
return sha1
except Exception as e:
return e
def photosAddTag(photo_id,tag):
"""add tags to a photo: takes photo id and tag. normalizes tag. returns tag id"""
normalizedtag = util.normalizeString(tag)
# create the tag first
try:
tagobject = Tag.get(Tag.name == normalizedtag)
except Tag.DoesNotExist:
tagobject = Tag.create(name = normalizedtag)
logger.info('created tag: {} id: {}'.format(tag, tagobject.id))
except Exception as e:
raise e
# ok now we have the tag_id and photo_id, let's do this
try:
phototag = PhotoTag.get(PhotoTag.photo == photo_id,PhotoTag.tag == tagobject.id)
return phototag.id
except PhotoTag.DoesNotExist:
logger.info('tagging photo id: {} tag: {}'.format(photo_id, tag))
phototag = PhotoTag.create(photo=photo_id,tag=tagobject.id)
return phototag.id
except Exception as e:
raise e
def photosRemoveTag(photo_id,tag):
"""remove tags from a photo: takes photo id and tag. normalizes tag. returns tuple of (photo_id, tag)"""
normalizedtag = util.normalizeString(tag)
# get the tag first
try:
deleteTag = Tag.get(Tag.name == normalizedtag)
except Tag.DoesNotExist:
logger.info('Tag Does not Exist: {}'.format(normalizedtag))
return (photo_id, tag)
except Exception as e:
raise e
# ok now we have the tag_id (in deleteTag.id) and photo_id
try:
deletePhotoTag = PhotoTag.get(PhotoTag.photo == photo_id,PhotoTag.tag == deleteTag.id)
deletePhotoTag = PhotoTag.delete().where(PhotoTag.id == deletePhotoTag.id)
deletePhotoTag.execute()
except PhotoTag.DoesNotExist:
logger.info('Tag not associated with photo: {}'.format(normalizedtag))
except Exception as e:
raise e
return (photo_id, tag)
def getfileType(filename):
fileType = filename.split('.')[-1].lower()
return fileType
def archivePhoto(file,sha1,fileType,localArchivePath,uploadToS3,photo_id):
"""store the photo in the archive"""
(sha1Path,sha1Filename)=util.getSha1Path(sha1)
archivedPhoto='%s/%s/%s.%s' % (localArchivePath,sha1Path,sha1Filename,fileType)
if not os.path.isdir(localArchivePath+'/'+sha1Path):
os.makedirs(localArchivePath+'/'+sha1Path)
if not os.path.isfile(archivedPhoto):
try:
logger.info('Copying %s -> %s',file,archivedPhoto)
shutil.copy2(file,archivedPhoto)
except Exception as e:
raise e
if uploadToS3 == True:
if checkImportStatusS3(photo_id) == False:
S3Key='%s/%s.%s' % (sha1Path,sha1Filename,fileType)
aws.uploadToS3(file,S3Key,app.config,policy=app.config['AWSPOLICY'])
return(archivedPhoto)
def dirTags(photo_id,file,ignoreTags):
"""add tags based on directory structure"""
osPathDirnames = os.path.dirname(file).split('/')
dirTags = []
for osPathDirname in osPathDirnames:
tag = str(osPathDirname)
if tag != '':
if tag not in ignoreTags:
dirTags.append(tag)
photosAddTag(photo_id,tag)
return dirTags
def parentDirPhotoSet(photo_id,file):
"""add tags based on parent directory"""
parentDir = os.path.dirname(file).split('/')[-1]
photoset_id = photosetsCreate(parentDir)
photosetsAddPhoto(photoset_id,photo_id)
return True
def getDateTaken(filename):
"""get a date from exif or file date
Tries to extract date from EXIF DateTimeOriginal field first.
If EXIF data is not available, falls back to file modification time.
Args:
filename: Path to the image file
Returns:
datetime object or None
"""
# Try to get date from EXIF data first
try:
exifDateTaken = util.getExifTags(filename)['DateTimeOriginal']
dateTaken = datetime.datetime.strptime(exifDateTaken, "%Y:%m:%d %H:%M:%S")
logger.info('Using EXIF date for %s: %s', os.path.basename(filename), dateTaken)
return dateTaken
except Exception as e:
logger.debug('No EXIF date for %s, trying file modification time', os.path.basename(filename))
# Fall back to file modification time
try:
file_mtime = os.path.getmtime(filename)
dateTaken = datetime.datetime.fromtimestamp(file_mtime)
logger.info('Using file modification time for %s: %s', os.path.basename(filename), dateTaken)
return dateTaken
except Exception as e:
logger.warning('Could not determine date for %s: %s', os.path.basename(filename), str(e))
return None