def export_ical_task(self, event_id, temp=True): event = safe_query(db, Event, 'id', event_id, 'event_id') try: if temp: filedir = os.path.join(current_app.config.get('BASE_DIR'), 'static/uploads/temp/' + event_id + '/') else: filedir = os.path.join(current_app.config.get('BASE_DIR'), 'static/uploads/' + event_id + '/') if not os.path.isdir(filedir): os.makedirs(filedir) filename = "ical.ics" file_path = os.path.join(filedir, filename) with open(file_path, "w") as temp_file: temp_file.write(str(ICalExporter.export(event_id), 'utf-8')) ical_file = UploadedFile(file_path=file_path, filename=filename) if temp: ical_url = upload( ical_file, UPLOAD_PATHS['exports-temp']['ical'].format(event_id=event_id)) else: ical_url = upload( ical_file, UPLOAD_PATHS['exports']['ical'].format(event_id=event_id)) result = {'download_url': ical_url} if not temp: event.ical_url = ical_url save_to_db(event) except Exception as e: result = {'__error': True, 'result': str(e)} logging.error('Error in ical download') return result
def create_save_resized_image(image_file, basewidth=None, maintain_aspect=None, height_size=None, upload_path=None, ext='jpg', remove_after_upload=False, resize=True): """ Create and Save the resized version of the background image :param resize: :param upload_path: :param ext: :param remove_after_upload: :param height_size: :param maintain_aspect: :param basewidth: :param image_file: :return: """ if not image_file: return None filename = '{filename}.{ext}'.format(filename=get_file_name(), ext=ext) data = urllib.request.urlopen(image_file).read() image_file = io.BytesIO(data) try: im = Image.open(image_file) except IOError: raise IOError("Corrupt/Invalid Image") # Convert to jpeg for lower file size. if im.format is not 'JPEG': img = im.convert('RGB') else: img = im if resize: if maintain_aspect: width_percent = (basewidth / float(img.size[0])) height_size = int((float(img.size[1]) * float(width_percent))) img = img.resize((basewidth, height_size), PIL.Image.ANTIALIAS) temp_file_relative_path = 'static/media/temp/' + generate_hash( str(image_file)) + get_file_name() + '.jpg' temp_file_path = app.config['BASE_DIR'] + '/' + temp_file_relative_path dir_path = temp_file_path.rsplit('/', 1)[0] # create dirs if not present if not os.path.isdir(dir_path): os.makedirs(dir_path) img.save(temp_file_path) upfile = UploadedFile(file_path=temp_file_path, filename=filename) if remove_after_upload: # os.remove(image_file) No point in removing in memory file pass uploaded_url = upload(upfile, upload_path) os.remove(temp_file_path) return uploaded_url
def _upload_media(task_handle, event_id, base_path): """ Actually uploads the resources """ global UPLOAD_QUEUE total = len(UPLOAD_QUEUE) ct = 0 for i in UPLOAD_QUEUE: # update progress ct += 1 update_state(task_handle, 'Uploading media (%d/%d)' % (ct, total)) # get upload infos name, model = i['srv'] id_ = i['id'] if name == 'event': item = db.session.query(model).filter_by(id=event_id).first() else: item = db.session.query(model).filter_by(event_id=event_id).filter_by(id=id_).first() # get cur file if i['field'] in ['original', 'large', 'thumbnail', 'small', 'icon']: field = '{}_image_url'.format(i['field']) else: field = '{}_url'.format(i['field']) path = getattr(item, field) if path.startswith('/'): # relative files path = base_path + path if os.path.isfile(path): filename = path.rsplit('/', 1)[1] file = UploadedFile(path, filename) else: file = '' # remove current file setting else: # absolute links try: filename = UPLOAD_PATHS[name][field].rsplit('/', 1)[1] if is_downloadable(path): r = requests.get(path, allow_redirects=True) file = UploadedMemory(r.content, filename) else: file = None except: file = None # don't update current file setting if file is None: continue # upload try: if file == '': raise Exception() key = UPLOAD_PATHS[name][field] if name == 'event': key = key.format(event_id=event_id) else: key = key.format(event_id=event_id, id=id_) print(key) new_url = upload(file, key) except Exception: print(traceback.format_exc()) new_url = None setattr(item, field, new_url) save_to_db(item, msg='Url updated') # clear queue UPLOAD_QUEUE = [] return
def _uploaded_file(self, folder, filename='testfile.bin'): path = os.path.join(folder, filename) with open(path, 'wb') as f: f.write(self.test_data) return UploadedFile(path, filename)