def test_unique_filename(): original_filename = 'filename.txt' _, original_ext = os.path.splitext(original_filename) result = unique_filename(original_filename) result_name, result_ext = os.path.splitext(result) assert result != original_filename assert result_ext == original_ext # UUID is a string from 32 hexadecimal digits assert len(result_name) == 32
def save_file(file_obj, where='uploaded'): """ Saves file to where directory on the server """ # todo: check for errors, types or file_obj properties folder = os.path.join(config.STATIC_FOLDER, where) if not os.path.exists(folder): os.makedirs(folder) new_filename = unique_filename(file_obj.filename) file_path = os.path.join(folder, new_filename) with open(file_path, 'wb') as open_file: open_file.write(file_obj.file.read()) return new_filename # returns new filename
def find_missing_images(): print('Fetching images names from database...') images_list = get_images_list() images_names = map(os.path.basename, images_list) found = [] print('Starting local search...') local_files = get_files_under_dir('/', ('.jpg', '.png', '.gif')) print('Found %s local images' % len(local_files)) for local_image in local_files: try: encoded_name = unique_filename(local_image) if encoded_name in images_names: found.append((local_image, encoded_name)) except UnicodeDecodeError as e: print('Error with %s, skipping...' % local_image) print('Found: %d images of %d' % (len(found), len(images_list))) return found
def up_file(): """ Uploads a picture for the article """ up_file = request.files.get('file') web_folder = 'img/article/' pictures_folder = static_path(web_folder) if not os.path.exists(pictures_folder): os.makedirs(pictures_folder) new_filename = unique_filename(up_file.filename) file_path = os.path.join(pictures_folder, new_filename) web_path = join_all_path(['/', web_folder, new_filename]) if os.path.exists(file_path): # Just return it, already here return web_path with open(file_path, 'wb') as open_file: open_file.write(up_file.file.read()) return web_path
def test_unique_filename_works_for_unicode(): result = unique_filename(u'should_be_fine.jpg') assert isinstance(result, str) assert result.endswith('.jpg')