Created
February 19, 2015 11:02
-
-
Save fernandojsg/3f8231b16497b0d8c06a to your computer and use it in GitHub Desktop.
Huge amount of image storage in volumes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import pyexiv2 | |
import os | |
import shutil | |
from models import StorageVolume | |
STORAGE_CONFIG = { | |
'bits_levels': [ 2, 1 ], | |
'volumes': [ | |
{ | |
'id': 1, | |
'name': 'data01', | |
'path': '/srv/images_volumes/data01', | |
'range': [ 0, 30 ], | |
}, | |
{ | |
'id': 2, | |
'name': 'data02', | |
'path': '/srv/images_volumes/data02', | |
'range': [ 31, 100 ], | |
}, | |
{ | |
'id': 3, | |
'name': 'data03', | |
'path': '/srv/images_volumes/data03', | |
'range': [ 100, 150 ], | |
}, | |
] | |
} | |
def get_folder( id ): | |
for volume in STORAGE_CONFIG['volumes']: | |
if id >= volume['range'][0] and id <= volume['range'][1]: | |
return volume | |
print "CRITICAL: NO VOLUME FOUND" | |
return None | |
def get_id_folders(id, levels_bits): | |
previous_bits = 0 | |
directories = [] | |
for level_bits in reversed(levels_bits): | |
mask = 2**level_bits - 1 | |
id = id >> previous_bits | |
#directory = format(id & mask, 'x').zfill(levels_bits/) | |
directory = id & mask | |
directories.insert(0, directory) | |
previous_bits = level_bits | |
return directories | |
def get_image_store_dst ( id ): | |
volume = get_folder(id) | |
if volume == None: | |
return None | |
directories = get_id_folders( id, STORAGE_CONFIG['bits_levels']) | |
relative_path = ('/'.join(str(x) for x in directories)) | |
full_path = volume['path']+'/' + relative_path | |
return { | |
'relative_path': relative_path, | |
'full_path': full_path, | |
'image_path': full_path+'/'+str(id)+'.jpg', | |
'volume_id': volume['id'] | |
} | |
def save_image_metadata (image_path, metadata): | |
metadata = pyexiv2.ImageMetadata(image_path) | |
metadata.read() | |
metadata['Exif.Photo.UserComment'] = json.dumps(metadata) | |
metadata.write() | |
def store_image( id, src_path, move = False, metadata = None): | |
# if metadata != None: | |
# save_image_metadata(src_path, metadata) | |
store_data = get_image_store_dst( id ) | |
if not os.path.exists(store_data['full_path']): | |
os.makedirs(store_data['full_path']) | |
dst_filename = store_data['full_path'] + "/" + str(id) + '.jpg' | |
if move: | |
shutil.move(src_path, dst_filename) | |
else: | |
shutil.copyfile(src_path, dst_filename) | |
return store_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment