def save_image_file(field, folder, rfile): """ Upload and save image file to field storage """ path = generate_filename(folder, get_file_extension(rfile.name)) new_file = field.storage.save(path, rfile) new_file_path = "%s%s" % (settings.MEDIA_ROOT, new_file) new_file_url = "%s%s" % (settings.MEDIA_URL, new_file) if not is_file_image(new_file_path): os.remove(new_file_path) return None return {"name" : new_file, "url" : new_file_url, "path" : new_file_path, }
def upload_image_file(folder, rfile): """ Upload image file to target folder. Generate filename and create needed path """ path, name = generate_filename(folder, get_file_extension(rfile["filename"]), get_name=True) fullpath = settings.MEDIA_ROOT + path filedir = os.path.dirname(fullpath) if not os.path.exists(filedir): os.makedirs(filedir) new_file = open(fullpath, 'w+') new_file.write(rfile['body']) new_file.close() if not is_file_image(fullpath): os.remove(fullpath) return None return {"name" : name, "url" : "%s%s" % (settings.MEDIA_URL, path), "path" : fullpath, }