def index():
    current_app.logger.info("upload_file, method = %s, folder=%s",
                            request.method,
                            current_app.config['UPLOAD_FOLDER'])
    if request.method == "GET":
        return render_template('admin/uploads.html')
    elif request.method == "POST":
        #generating json response array
        result = []
        for inputname, fileobj in request.files.items():
            # filename = fileobj.filename
            # current_app.logger.info("filename = %s" % (filename))
            # dump(fileobj)
            # fileobj = request.files[filename]
            # current_app.logger.info("filename = %s, file size = %d" % (filename, get_file_size(fileobj.file)))
            if fileobj and allowed_file(fileobj.filename):
                if Photo.query.filter(
                        Photo.filename == fileobj.filename).count() > 0:
                    result.append({
                        "name": fileobj.filename,
                        "size": 0,
                        "error": "Duplicated filename",
                        "url": "",
                        "thumbnail_url": "",
                        "delete_url": "",
                        "delete_type": "POST"
                    })
                    continue

                # save file
                saved_filename = get_saved_filename(fileobj.filename)
                filesize = get_file_size(fileobj.stream)
                current_app.logger.info(
                    "filename original = %s, escaped = %s, size = %d" %
                    (fileobj.filename, saved_filename, filesize))
                fileobj.save(
                    os.path.join(current_app.config['UPLOAD_FOLDER'],
                                 saved_filename))

                # process file to get noexif image, thumb image
                noexif_filename = get_noexif_filename(fileobj.filename)
                thumb_filename = get_thumb_filename(fileobj.filename)
                save_transposed_imagefiles(saved_filename, noexif_filename,
                                           thumb_filename,
                                           current_app.config['UPLOAD_FOLDER'])

                photo = Photo(filename=fileobj.filename,
                              saved_filename=saved_filename)
                photo.noexif_filename = noexif_filename
                photo.thumb_filename = thumb_filename
                photo.size = filesize
                photo.start_date = get_image_date(
                    saved_filename=saved_filename,
                    filepath=current_app.config['UPLOAD_FOLDER'])
                photo.exif = get_image_exif(
                    saved_filename=saved_filename,
                    filepath=current_app.config['UPLOAD_FOLDER'])
                photo.user_id = session['user_id']
                db_session.add(photo)
                db_session.commit()

                result.append({
                    "name":
                    fileobj.filename,
                    "size":
                    filesize,
                    "url":
                    url_for("photos.show_photo", filename=fileobj.filename),
                    "thumbnail_url":
                    url_for("photos.show_thumb", filename=fileobj.filename),
                    "delete_url":
                    "",
                    "delete_type":
                    "POST"
                })
            else:
                current_app.logger.info("Invalid file extension: %s" %
                                        (fileobj.filename))
                result.append({
                    "name": fileobj.filename,
                    "error": "Invalid file extension",
                    "size": "",
                    "url": "",
                    "thumbnail_url": "",
                    "delete_url": "",
                    "delete_type": "POST"
                })

        # https://github.com/blueimp/jQuery-File-Upload/wiki/Setup
        # response formats
        final_result = {}
        final_result['files'] = result
        response_data = json.dumps(final_result)
        current_app.logger.info("response = %s" % (response_data))
        return response_data
def index():
    current_app.logger.info("upload_file, method = %s, folder=%s", request.method, current_app.config['UPLOAD_FOLDER'])
    if request.method == "GET":
        return render_template('admin/uploads.html')
    elif request.method == "POST":
        #generating json response array
        result = []
        for inputname, fileobj in request.files.items():
            # filename = fileobj.filename
            # current_app.logger.info("filename = %s" % (filename))
            # dump(fileobj)
            # fileobj = request.files[filename]
            # current_app.logger.info("filename = %s, file size = %d" % (filename, get_file_size(fileobj.file)))
            if fileobj and allowed_file(fileobj.filename):
                if Photo.query.filter(Photo.filename == fileobj.filename).count() > 0:
                    result.append({"name": fileobj.filename,
                                   "size": 0,
                                   "error": "Duplicated filename",
                                   "url": "",
                                   "thumbnail_url": "",
                                   "delete_url": "",
                                   "delete_type": "POST"})
                    continue

                # save file
                saved_filename = get_saved_filename(fileobj.filename)
                filesize = get_file_size(fileobj.stream)
                current_app.logger.info("filename original = %s, escaped = %s, size = %d" % (fileobj.filename, saved_filename, filesize))
                fileobj.save(os.path.join(current_app.config['UPLOAD_FOLDER'], saved_filename))

                # process file to get noexif image, thumb image
                noexif_filename = get_noexif_filename(fileobj.filename)
                thumb_filename = get_thumb_filename(fileobj.filename)
                save_transposed_imagefiles(saved_filename, noexif_filename, thumb_filename, current_app.config['UPLOAD_FOLDER'])

                photo = Photo(filename=fileobj.filename, saved_filename=saved_filename)
                photo.noexif_filename = noexif_filename
                photo.thumb_filename = thumb_filename
                photo.size = filesize
                photo.start_date = get_image_date(saved_filename=saved_filename, filepath=current_app.config['UPLOAD_FOLDER'])
                photo.exif = get_image_exif(saved_filename=saved_filename, filepath=current_app.config['UPLOAD_FOLDER'])
                photo.user_id = session['user_id']
                db_session.add(photo)
                db_session.commit()

                result.append({"name": fileobj.filename,
                               "size": filesize,
                               "url": url_for("photos.show_photo", filename=fileobj.filename),
                               "thumbnail_url": url_for("photos.show_thumb", filename=fileobj.filename),
                               "delete_url": "",
                               "delete_type": "POST"})
            else:
                current_app.logger.info("Invalid file extension: %s" % (fileobj.filename))
                result.append({"name": fileobj.filename,
                               "error": "Invalid file extension",
                               "size": "",
                               "url": "",
                               "thumbnail_url": "",
                               "delete_url": "",
                               "delete_type": "POST"})

        # https://github.com/blueimp/jQuery-File-Upload/wiki/Setup
        # response formats
        final_result = {}
        final_result['files'] = result
        response_data = json.dumps(final_result)
        current_app.logger.info("response = %s" % (response_data))
        return response_data
def show_photo_exif(filename):
    saved_filename = get_saved_filename(filename)
    exif_info = get_image_exif(saved_filename=saved_filename, filepath=current_app.config['UPLOAD_FOLDER'])
    return exif_info
def show_photo_exif(filename):
    saved_filename = get_saved_filename(filename)
    exif_info = get_image_exif(saved_filename=saved_filename,
                               filepath=current_app.config['UPLOAD_FOLDER'])
    return exif_info