##// END OF EJS Templates
helpers: allow save-on-disk svg gravatars instead of the on-the-fly generated ones
marcink -
r4477:599c69c4 default
parent child
Show More
@@ -90,7 +90,7 from rhodecode.lib.vcs.conf.settings imp
90 from rhodecode.lib.index.search_utils import get_matching_line_offsets
90 from rhodecode.lib.index.search_utils import get_matching_line_offsets
91 from rhodecode.config.conf import DATE_FORMAT, DATETIME_FORMAT
91 from rhodecode.config.conf import DATE_FORMAT, DATETIME_FORMAT
92 from rhodecode.model.changeset_status import ChangesetStatusModel
92 from rhodecode.model.changeset_status import ChangesetStatusModel
93 from rhodecode.model.db import Permission, User, Repository, UserApiKeys
93 from rhodecode.model.db import Permission, User, Repository, UserApiKeys, FileStore
94 from rhodecode.model.repo_group import RepoGroupModel
94 from rhodecode.model.repo_group import RepoGroupModel
95 from rhodecode.model.settings import IssueTrackerSettingsModel
95 from rhodecode.model.settings import IssueTrackerSettingsModel
96
96
@@ -1357,12 +1357,63 class InitialsGravatar(object):
1357 return "data:image/svg+xml;base64,%s" % base64.b64encode(img_data)
1357 return "data:image/svg+xml;base64,%s" % base64.b64encode(img_data)
1358
1358
1359
1359
1360 def initials_gravatar(email_address, first_name, last_name, size=30):
1360 def initials_gravatar(email_address, first_name, last_name, size=30, store_on_disk=False):
1361 request = get_current_request()
1362
1361 svg_type = None
1363 svg_type = None
1362 if email_address == User.DEFAULT_USER_EMAIL:
1364 if email_address == User.DEFAULT_USER_EMAIL:
1363 svg_type = 'default_user'
1365 svg_type = 'default_user'
1366
1364 klass = InitialsGravatar(email_address, first_name, last_name, size)
1367 klass = InitialsGravatar(email_address, first_name, last_name, size)
1365 return klass.generate_svg(svg_type=svg_type)
1368
1369 if store_on_disk:
1370 from rhodecode.apps.file_store import utils as store_utils
1371 from rhodecode.apps.file_store.exceptions import FileNotAllowedException, \
1372 FileOverSizeException
1373 from rhodecode.model.db import Session
1374
1375 image_key = md5_safe(email_address.lower()
1376 + first_name.lower() + last_name.lower())
1377
1378 storage = store_utils.get_file_storage(request.registry.settings)
1379 filename = '{}.svg'.format(image_key)
1380 subdir = 'gravatars'
1381 # since final name has a counter, we apply the 0
1382 uid = storage.apply_counter(0, store_utils.uid_filename(filename, randomized=False))
1383 store_uid = os.path.join(subdir, uid)
1384
1385 db_entry = FileStore.get_by_store_uid(store_uid)
1386 if db_entry:
1387 return request.route_path('download_file', fid=store_uid)
1388
1389 img_data = klass.get_img_data(svg_type=svg_type)
1390 img_file = store_utils.bytes_to_file_obj(img_data)
1391
1392 try:
1393 store_uid, metadata = storage.save_file(
1394 img_file, filename, directory=subdir,
1395 extensions=['.svg'], randomized_name=False)
1396 except (FileNotAllowedException, FileOverSizeException):
1397 raise
1398
1399 try:
1400 entry = FileStore.create(
1401 file_uid=store_uid, filename=metadata["filename"],
1402 file_hash=metadata["sha256"], file_size=metadata["size"],
1403 file_display_name=filename,
1404 file_description=u'user gravatar `{}`'.format(safe_unicode(filename)),
1405 hidden=True, check_acl=False, user_id=1
1406 )
1407 Session().add(entry)
1408 Session().commit()
1409 log.debug('Stored upload in DB as %s', entry)
1410 except Exception:
1411 raise
1412
1413 return request.route_path('download_file', fid=store_uid)
1414
1415 else:
1416 return klass.generate_svg(svg_type=svg_type)
1366
1417
1367
1418
1368 def gravatar_url(email_address, size=30, request=None):
1419 def gravatar_url(email_address, size=30, request=None):
General Comments 0
You need to be logged in to leave comments. Login now