-
Notifications
You must be signed in to change notification settings - Fork 0
/
post_multimedia.py
343 lines (307 loc) · 9.68 KB
/
post_multimedia.py
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# VIA API
# Title needs the extension (.png, .jpg....)
__author__ = 'Randal'
import boto3, os
import datetime
import hashlib
import json
import base64
def return_error(statusCode, message):
response = {
"statusCode": statusCode,
"headers": {
"Access-Control-Allow-Origin": "*"
},
"body": message
}
return response
def handler(event, context):
tags = []
#Values checker
print("Event Initial: "+str(event))
if 'body' in event:
# Si el evento se llama desde apigateway(Lambda Proxy), el evento original vendra en el body
# Y nos los quedaremos. Si no, usamos el evento original ya que traera todos los datos
event = json.loads(event['body'])
print("Event took(Body): "+str(event))
if 'file' not in event:
return return_error(400, "File to upload not given")
if 'title' not in event:
return return_error(400, "Title not given")
if 'session_token' not in event:
return return_error(400, "Session Token not given")
if 'event' not in event:
return return_error(400, "Event not given")
if 'event_date' not in event:
return return_error(400, "Event_date not given")
# Values checker
# Values exists?
event['username'] = get_user_given_token(event['session_token'])
if event['username'] is False:
return return_error(401, "Invalid aws session token ")
if not exist_event(title=event['event'], event_date=event['event_date']):
#rollback(event['picture_key'])
return return_error(404, "Event don't exists")
try:
s3_path = upload_file_to_s3(event=event['event'], date=event['event_date'], title=event['title'], file_encoded=event['file'])
except:
return return_error(500, "Error inserting the file in S3")
response = insert_file_event_DynamoDB(event, s3_path)
if response['ResponseMetadata']['HTTPStatusCode'] is not 200:
rollback(event['picture_key'])
return return_error(500, "Error inserting the file. File deleted from s3")
else:
classrooms = get_classrooms_of_the_event(event['event'], event['event_date'])
send_notification(classrooms, event)
return {
"statusCode": 200,
"headers": {
"Access-Control-Allow-Origin": "*"
},
"body": "File uploaded and saved correctly in dynamoDB"
}
#return "File uploaded and saved correctly in dynamoDB"
"""
Comrpobar que la imagen existe en s3
Enviar notificaciones. Primero se consulta las clases invitadas al evento. Y luego para cada una de las clases se manda una notificaicon
"""
# Notification
def upload_file_to_s3(event, file_encoded, title, date):
s3_client = boto3.client('s3')
file_path = '/tmp/'+title
fh = open(file_path, "wb")
fh.write(file_encoded.decode('base64'))
fh.close()
print('Correctly created in ' +file_path)
try:
with open(file_path, "wb") as fh:
fh.write(file_encoded.decode('base64'))
print('Correctly saved ' +file_path)
except Exception:
print("Error decoding and creating the file")
raise
s3_path = "Events/"+event+"/"+date.replace('/', '-')+"/"+title
try:
s3_client.upload_file(file_path, os.environ['originalBucket'], s3_path)
except Exception:
print("Error uploading the picture "+file_path+" to "+s3_path+" in the bucket "+os.environ['originalBucket'] +"")
raise
return s3_path
def get_user_given_token(session_token):
client = boto3.client('dynamodb')
response = client.scan(
TableName=os.environ['tableUsersLogged'],
Select='ALL_ATTRIBUTES',
ScanFilter={
'Session Token':{
'AttributeValueList': [{
'S': hashlib.sha1(session_token).hexdigest()
}
],
'ComparisonOperator': 'EQ'
}
}
)
try:
return response["Items"][0]['Username']['S']
except IndexError:
return False
except KeyError:
return False
def get_classrooms_of_the_event(event, event_date):
client = boto3.client('dynamodb')
response = client.scan(
TableName=os.environ['tableEvents'],
Select='ALL_ATTRIBUTES',
ScanFilter={
'Title':{
'AttributeValueList': [{
'S': event
}
],
'ComparisonOperator': 'EQ'
},
'Date':{
'AttributeValueList': [{
'S': event_date
}
],
'ComparisonOperator': 'EQ'
}
}
)
try:
return response["Items"][0]["Classrooms"]["SS"]
except IndexError:
return False
except KeyError:
return False
def send_notification(classrooms, event):
sns = boto3.resource('sns')
message = generate_message (event)
subject = event['event']+" : "+event['title']
for classroom in classrooms:
print classroom
arn = get_arn_of_classroom(classroom.split()[0], classroom.split()[1])
topic = sns.Topic(arn)
response = topic.publish(
Message=message,
Subject=subject
)
print(response)
def generate_message(event):
messagge = "Se ha agregado un nuevo archivo :\n\n" \
"\t User: " + event['username'] + "\n" \
"\t Title : " + event['title'] + "\n"\
"\t Event: " + event['event'] + "\n" \
"\t Date: " + event['event_date'] + "\n" \
"Esperamos que tengas un buen dia \n\n" \
"TalkClass"
return messagge
def get_arn_of_classroom(classs, level):
client = boto3.client('dynamodb')
response = client.scan(
TableName=os.environ['tableClassroom'],
Select='ALL_ATTRIBUTES',
ScanFilter={
'Class':{
'AttributeValueList': [{
'S': classs
}
],
'ComparisonOperator': 'EQ'
},
'Level':{
'AttributeValueList': [{
'S': level
}
],
'ComparisonOperator': 'EQ'
}
}
)
try:
return response["Items"][0]["Topic"]["S"]
except IndexError:
return False
except KeyError:
return False
# Notification
def insert_file_event_DynamoDB(event, s3_path):
client = boto3.client('dynamodb')
response = client.put_item(
TableName=os.environ['tableMultimedia'],
Item={
'Username': {
'S': event["username"]
},
'Title': {
'S': event["title"]
},
'Event': {
'S': event["event"]
},
'Date': {
'S': event["event_date"]
},
'Picture Key': {
'S': s3_path
},
'Tags': {
'SS': getLabels(s3_path)
}
}
)
return response
def insert_file_DynamoDB(event):
client = boto3.client('dynamodb')
response = client.put_item(
TableName=os.environ['tableMultimedia'],
Item={
'Username': {
'S': event["username"]
},
'Title': {
'S': event["title"]
},
'Picture Key': {
'S': event["picture_key"]
},
'Date': {
'S': getDate()
},
'Tags': {
'SS': getLabels()
}
}
)
return response
def getDate():
return str(datetime.datetime.now()).split()[0]
def rollback(picture_key):
client = boto3.client('s3')
response = client.delete_object(
Bucket=os.environ['originalBucket'],
Key=picture_key,
)
response2 = client.delete_object(
Bucket=os.environ['resizedBucket'],
Key=picture_key,
)
print response
print response2
return (200 < response['ResponseMetadata']['HTTPStatusCode'] < 205) & (200 < response2['ResponseMetadata']['HTTPStatusCode'] < 205)
def exist_user(username):
client = boto3.client('dynamodb')
response = client.scan(
TableName=os.environ['tableUsers'],
Select='ALL_ATTRIBUTES',
ScanFilter={
'Username':{
'AttributeValueList':[{
'S': username
}
],
'ComparisonOperator': 'EQ'
}
}
)
return response["Count"] > 0
def exist_event(title, event_date):
client = boto3.client('dynamodb')
response = client.scan(
TableName=os.environ['tableEvents'],
Select='ALL_ATTRIBUTES',
ScanFilter={
'Title': {
'AttributeValueList':[{
'S': title
}
],
'ComparisonOperator': 'EQ'
},
'Date': {
'AttributeValueList':[{
'S': event_date
}
],
'ComparisonOperator': 'EQ'
}
}
)
return response["Count"] > 0
def getLabels(picture_key):
labels = []
client = boto3.client('rekognition')
response = client.detect_labels(
Image={
'S3Object': {
'Bucket': os.environ['originalBucket'],
'Name': picture_key,
}
},
MaxLabels=int(os.environ['maxLabel'])
)
for label in response['Labels']:
labels.append(label['Name'])
return labels