示例#1
0
def storymap_import(user):
    try:
        if 'archive' in request.files:
            temp_file, temp_path = tempfile.mkstemp()
            request.files['archive'].save(temp_path)

            @after_this_request
            def cleanup_temp_file(response):
                os.close(temp_file)
                os.remove(temp_path)
                return response

            with ZipFile(temp_path, mode='r') as zip_file:
                files = zip_file.namelist()
                if ('metadata.json' not in files) or ('draft.json' not in files) or ('draft.html' not in files):
                    return jsonify({'error': 'This doesn\'t look like a StoryMap exported package.'})

                id = _import_metadata(user, json.loads(zip_file.read('metadata.json')))
                key_prefix = storage.key_prefix(user['uid'], id)

                for file_name in files:
                    if file_name != 'metadata.json':
                        key_name = "%s%s" % (key_prefix, file_name)
                        storage.save_from_data(key_name, mimetypes.guess_type(file_name), zip_file.read(file_name))

                return jsonify({'id': id})

    except Exception, e:
        traceback.print_exc()
        return jsonify({'error': str(e)})
示例#2
0
def _write_embed(embed_key_name, json_key_name, meta):
    """Write embed page"""
    image_url = meta.get('image_url', settings.STATIC_URL+'img/logos/logo_storymap.png')

    # NOTE: facebook needs the protocol on embed_url and image_url for og tag
    content = render_template('_embed.html',
        embed_url=_fix_url_for_opengraph(settings.AWS_STORAGE_BUCKET_URL+embed_key_name),
        json_url=urllib.quote(settings.AWS_STORAGE_BUCKET_URL+json_key_name),
        title=meta.get('title', ''),
        description=meta.get('description', ''),
        image_url=_fix_url_for_opengraph(image_url)
    )
    storage.save_from_data(embed_key_name, 'text/html', content)
示例#3
0
def _write_embed(embed_key_name, json_key_name, meta):
    """Write embed page"""    
    # NOTE: facebook needs the protocol on embed_url for og tag
    image_url = meta.get('image_url', settings.STATIC_URL+'img/logos/logo_storymap.png')
    if image_url.startswith('//'):
        image_url = 'http:'+image_url
        
    content = render_template('_embed.html',
        embed_url='http:'+settings.AWS_STORAGE_BUCKET_URL+embed_key_name,
        json_url=settings.AWS_STORAGE_BUCKET_URL+json_key_name,
        title=meta.get('title', ''),
        description=meta.get('description', ''),
        image_url=image_url
    )            
    storage.save_from_data(embed_key_name, 'text/html', content)
示例#4
0
def _write_embed(embed_key_name, json_key_name, meta):
    """Write embed page"""    
    # NOTE: facebook needs the protocol on embed_url for og tag
    image_url = meta.get('image_url', settings.STATIC_URL+'img/logos/logo_storymap.png')
    if image_url.startswith('//'):
        image_url = 'http:'+image_url
        
    content = render_template('_embed.html',
        embed_url='http:'+settings.AWS_STORAGE_BUCKET_URL+embed_key_name,
        json_url=settings.AWS_STORAGE_BUCKET_URL+json_key_name,
        title=meta.get('title', ''),
        description=meta.get('description', ''),
        image_url=image_url
    )            
    storage.save_from_data(embed_key_name, 'text/html', content)
示例#5
0
文件: api.py 项目: dmoses/StoryMapJS
def _write_embed(embed_key_name, json_key_name, meta):
    """Write embed page"""    
    # NOTE: facebook needs the protocol on embed_url for og tag
    image_url = meta.get('image_url', settings.STATIC_URL+'img/logos/logo_storymap.png')
    parts = _parse_url(image_url)
    parts['path'] = urllib.quote(parts['path'])
    image_url = '%(scheme)s://%(netloc)s%(path)s/%(file)s' % parts
        
    content = render_template('_embed.html',
        embed_url='http:'+urllib.quote(settings.AWS_STORAGE_BUCKET_URL+embed_key_name),
        json_url=urllib.quote(settings.AWS_STORAGE_BUCKET_URL+json_key_name),
        title=meta.get('title', ''),
        description=meta.get('description', ''),
        image_url=image_url
    )            
    storage.save_from_data(embed_key_name, 'text/html', content)
示例#6
0
def _write_embed(embed_key_name, json_key_name, meta):
    """Write embed page"""    
    image_url = meta.get('image_url', settings.STATIC_URL+'img/logos/logo_storymap.png')
    parts = _parse_url(image_url)
    parts['path'] = urllib.quote(parts['path'])
    image_url = '%(scheme)s://%(netloc)s%(path)s/%(file)s' % parts
        
    # NOTE: facebook needs the protocol on embed_url for og tag
    content = render_template('_embed.html',
        embed_url=urllib.quote(settings.AWS_STORAGE_BUCKET_URL+embed_key_name),
        json_url=urllib.quote(settings.AWS_STORAGE_BUCKET_URL+json_key_name),
        title=meta.get('title', ''),
        description=meta.get('description', ''),
        image_url=image_url
    )            
    storage.save_from_data(embed_key_name, 'text/html', content)
    def test_save_from_data(self):
        file_name = 'test1.json'
        key_name = '%s/%s' % (settings.AWS_STORAGE_BUCKET_KEY, file_name)
        content_type = 'application/json'
        content = json.dumps({ 'test_key': 'test_value' })
        save_from_data(key_name, content_type, content)
        endpoint = settings.AWS_ENDPOINT_URL
        if endpoint:
            s3 = boto3.resource('s3', endpoint_url=endpoint)
        else:
            s3 = boto3.resource('s3')
        obj = s3.Object(settings.AWS_STORAGE_BUCKET_NAME, key_name)
        try:
            self.assertEqual('test_value',
                json.loads(obj.get()['Body'].read())['test_key'])
        except botocore.exceptions.ConnectionError:
            self.fail("""
boto3 connection error in test. Check your environment variables

Be sure AWS_ENDPOINT_URL points to a valid localized endpoint. If connecting to S3, be sure AWS_ENDPOINT_URL is blank or not set and that AWS_SECRET_ACCESS_KEY and AWS_ACCESS_KEY_ID are set
""")
        except Exception as e:
            if 'NoSuchBucket' in e.message:
                self.fail("""
Could not connect. No such bucket: %s
AWS endpoint: %s

NOTE: StoryMap and these tests do not create the storage bucket. For testing, your endpoint should have a bucket named according to your AWS_TEST_BUCKET environment variable. With localstack, this bucket can be created with the following command:

aws --endpoint-url=http://localhost:4572 s3 mb s3://%s
""" % (obj.bucket_name, endpoint, settings.AWS_TEST_BUCKET))
            elif 'NoSuchKey' in e.message:
                self.fail("""
No such key error

The `save_from_data` function currently only saves to remote S3. To get this test passing, we will need to migrate to boto3 usage that allows for local storage (via localstack) or remote (to s3)
""")
            else:
                raise
    def test_save_from_data(self):
        file_name = 'test1.json'
        key_name = '%s/%s' % (settings.AWS_STORAGE_BUCKET_KEY, file_name)
        content_type = 'application/json'
        content = json.dumps({'test_key': 'test_value'})
        save_from_data(key_name, content_type, content)
        endpoint = settings.AWS_ENDPOINT_URL
        if endpoint:
            s3 = boto3.resource('s3', endpoint_url=endpoint)
        else:
            s3 = boto3.resource('s3')
        obj = s3.Object(settings.AWS_STORAGE_BUCKET_NAME, key_name)
        try:
            self.assertEqual('test_value',
                             json.loads(obj.get()['Body'].read())['test_key'])
        except botocore.exceptions.ConnectionError:
            self.fail("""
boto3 connection error in test. Check your environment variables

Be sure AWS_ENDPOINT_URL points to a valid localized endpoint. If connecting to S3, be sure AWS_ENDPOINT_URL is blank or not set and that AWS_SECRET_ACCESS_KEY and AWS_ACCESS_KEY_ID are set
""")
        except Exception as e:
            if 'NoSuchBucket' in e.message:
                self.fail("""
Could not connect. No such bucket: %s
AWS endpoint: %s

NOTE: StoryMap and these tests do not create the storage bucket. For testing, your endpoint should have a bucket named according to your AWS_TEST_BUCKET environment variable. With localstack, this bucket can be created with the following command:

aws --endpoint-url=http://localhost:4572 s3 mb s3://%s
""" % (obj.bucket_name, endpoint, settings.AWS_TEST_BUCKET))
            elif 'NoSuchKey' in e.message:
                self.fail("""
No such key error

The `save_from_data` function currently only saves to remote S3. To get this test passing, we will need to migrate to boto3 usage that allows for local storage (via localstack) or remote (to s3)
""")
            else:
                raise
示例#9
0
def storymap_image_save(user, id):
    """
    Save storymap image
    @id = storymap id
    @name = file name
    @content = data:URL representing the file's data as base64 encoded string
    """
    try:
        name, content = _request_get_required('name', 'content')

        m = re.match('data:(.+);base64,(.+)', content)
        if m:
            content_type = m.group(1)
            content = m.group(2).decode('base64')
        else:
            raise Exception('Expected content as data-url')

        key_name = storage.key_name(user['uid'], id, '_images', name)
        storage.save_from_data(key_name, content_type, content)

        return jsonify({'url': settings.AWS_STORAGE_BUCKET_URL+key_name})
    except storage.StorageException, e:
        traceback.print_exc()
        return jsonify({'error': str(e), 'error_detail': e.detail})
示例#10
0
文件: api.py 项目: jorol/StoryMapJS
def storymap_image_save(user, id):
    """
    Save storymap image
    @id = storymap id
    @name = file name
    @content = data:URL representing the file's data as base64 encoded string
    """
    try:
        name, content = _request_get_required("name", "content")

        m = re.match("data:(.+);base64,(.+)", content)
        if m:
            content_type = m.group(1)
            content = m.group(2).decode("base64")
        else:
            raise Exception("Expected content as data-url")

        key_name = storage.key_name(user["uid"], id, "_images", name)
        storage.save_from_data(key_name, content_type, content)

        return jsonify({"url": settings.AWS_STORAGE_BUCKET_URL + key_name})
    except storage.StorageException, e:
        traceback.print_exc()
        return jsonify({"error": str(e), "error_detail": e.detail})