예제 #1
0
def process_and_create_file(project_id, name, length, mime_type):
    """Base function that handles the hashing of a file name and the creation of
    a record in the files collection. This function is use in the nodes module
    on assets_create.

    :type project_id: string
    :param bucket_name: The project id, uset to fetch the gcs bucket.

    :type name: string
    :param subdir: The the filename (currently used to build the path).

    :type length: int
    :param subdir: Filesize in bit (in case we start the upload from the js
        interface, we get the size for free, otherwise at the moment we
        hardcode it to 0)

    :type mime_type: string
    :param subdir: MIME type used do display/preview the file accordingly

    """

    root, ext = os.path.splitext(name)
    # Hash name based on file name, user id and current timestamp
    hash_name = name + str(current_user.objectid) + str(round(time.time()))
    link = hashlib.sha1(hash_name).hexdigest()
    link = os.path.join(link[:2], link + ext)

    src_dir_path = os.path.join(app.config['UPLOAD_DIR'],
                                str(current_user.objectid))

    # Move the file in designated location
    destination_dir = os.path.join(app.config['SHARED_DIR'], link[:2])
    if not os.path.isdir(destination_dir):
        os.makedirs(destination_dir)
    # (TODO) Check if filename already exsits
    src_file_path = os.path.join(src_dir_path, name)
    dst_file_path = os.path.join(destination_dir, link[3:])
    # (TODO) Thread this operation

    shutil.move(src_file_path, dst_file_path)

    api = system_util.pillar_api()
    file_item = File({
        'name': link[3:],
        'filename': name,
        'user': current_user.objectid,
        'backend': 'gcs',
        'md5': '',
        'content_type': mime_type,
        'length': length,
        'project': project_id
    })
    file_item.create(api=api)
    return file_item
예제 #2
0
def create():
    name = request.form['name']
    size = request.form['size']
    content_type = request.form['type']
    root, ext = os.path.splitext(name)
    # Hash name based on file name, user id and current timestamp
    hash_name = name + str(current_user.objectid) + str(round(time.time()))
    link = hashlib.sha1(hash_name).hexdigest()
    link = os.path.join(link[:2], link + ext)

    src_dir_path = os.path.join(app.config['UPLOAD_DIR'],
                                str(current_user.objectid))

    # Move the file in designated location
    destination_dir = os.path.join(app.config['SHARED_DIR'], link[:2])
    if not os.path.isdir(destination_dir):
        os.makedirs(destination_dir)
    # (TODO) Check if filename already exsits
    src_file_path = os.path.join(src_dir_path, name)
    dst_file_path = os.path.join(destination_dir, link[3:])
    # (TODO) Thread this operation

    shutil.copy(src_file_path, dst_file_path)

    api = SystemUtility.attract_api()
    node_file = File({
        'name': link,
        'filename': name,
        'user': current_user.objectid,
        'backend': 'cdnsun',
        'md5': '',
        'content_type': content_type,
        'length': size
    })
    node_file.create(api=api)
    thumbnail = node_file.thumbnail_file('s', api=api)

    return jsonify(status='success',
                   data=dict(id=node_file._id, link=thumbnail.link))
예제 #3
0
    def test_pickling_node(self):
        from pillarsdk import Node, File

        base_link = 'https://storage.googleapis.com/57534c07c37a7195f/_%2F326760eb7d7b244afe52'
        picture = File({
            '_id': '57534ccdc379cf1b24a7196d',
            '_created': '2016-06-04T23:49:01.000+0200',
            '_updated': '2017-09-07T15:55:37.000+0200',
            '_etag': '4bb3f525ea637c612fca882e5fcf334c056544fd',
            'status': 'complete',
            'name': '326763a705364fe0b0eb7d7b244afe52.png',
            'backend': 'gcs',
            'format': 'png',
            'variations': [
                {'width': 160,
                 'length': 4705,
                 'content_type': 'image/jpeg',
                 'height': 160,
                 'file_path': '326763a705364fe0b0eb7d7b244afe52-b.jpg',
                 'size': 'b',
                 'link': base_link + '-b.jpg'},
                {'width': 269,
                 'length': 8508,
                 'content_type': 'image/jpeg',
                 'height': 269,
                 'file_path': '326763a705364fe0b0eb7d7b244afe52-h.jpg',
                 'size': 'h',
                 'link': base_link + '-h.jpg'},
                {'width': 269,
                 'length': 8508,
                 'content_type': 'image/jpeg',
                 'height': 269,
                 'file_path': '326763a705364fe0b0eb7d7b244afe52-m.jpg',
                 'size': 'm',
                 'link': base_link + '-m.jpg'},
            ],
            'filename': '01d.png',
            'project': '57534c07c379cf1b24a7195f',
            'width': 269,
            'length': 9681,
            'user': '******',
            'content_type': 'image/png',
            'height': 269,
            'file_path': '326763a705364fe0b0eb7d7b244afe52.png',
            'md5': '',
            'length_aggregate_in_bytes': 47050,
            'link': base_link + '.png',
            'link_expires': '2117-09-08T14:54:35.250+0200',
        }
        )
        parent_node = Node({
            '_id': '54134',
            'name': 'Dadday',
        })
        original = Node({
            '_id': '123456',
            'name': 'über cooole node',
            'parent': parent_node,
            'picture': picture,
        })

        pickled = pickle.dumps(original)
        restored = pickle.loads(pickled)

        self.assertEqual(restored._id, '123456')
        self.assertEqual(restored.name, 'über cooole node')

        self.assertIsInstance(restored.parent, Node)
        self.assertEqual(restored.parent.name, 'Dadday')

        url = 'https://storage.googleapis.com/57534c07c37a7195f/_%2F326760eb7d7b244afe52-m.jpg'
        self.assertEqual(url, original.picture.thumbnail('m'))
        self.assertEqual(url, restored.picture.thumbnail('m'))
예제 #4
0
def index():
    """Custom files entry point
    """
    rfiles = []
    backend = app.config['STORAGE_BACKEND']
    api = SystemUtility.attract_api()
    user = current_user.objectid
    node_picture = File()

    for file_ in request.files:
        filestorage = request.files[file_]

        # Save file on AttractWeb Storage
        picture_path = os.path.join(app.config['UPLOAD_DIR'],
                                    filestorage.filename)
        filestorage.save(picture_path)

        picture_file_file = open(picture_path, 'rb')
        if backend == 'pillar':
            hash_ = hashfile(picture_file_file, hashlib.md5())
            name = "{0}{1}".format(hash_, os.path.splitext(picture_path)[1])
        picture_file_file.close()

        file_check = node_picture.all({"where": "path=='{0}'".format(name)},
                                      api=api)
        file_check = file_check['_items']

        if len(file_check) == 0:
            prop = {}
            prop['name'] = filestorage.filename
            prop['description'] = "File {0}".format(filestorage.filename)
            prop['user'] = user
            prop['content_type'] = filestorage.content_type
            # TODO Fix length value
            prop['length'] = filestorage.content_length
            prop['md5'] = hash_
            prop['filename'] = filestorage.filename
            prop['backend'] = backend
            if backend in ["pillar"]:
                prop['path'] = name
            node_picture.post(prop, api=api)
            prop['_id'] = node_picture['_id']
            if backend == 'pillar':
                node_picture.post_file(picture_path, name, api=api)
                node_picture.build_previews(name, api=api)

            url = "{0}/file_storage/file/{1}".format(
                app.config['PILLAR_SERVER_ENDPOINT'], prop['path'])
            rfiles.append({
                "id": prop['_id'],
                "name": prop['filename'],
                "size": prop['length'],
                "url": url,
                "thumbnailUrl": url,
                "deleteUrl": url,
                "deleteType": "DELETE"
            })
        else:
            url = "{0}/file_storage/file/{1}".format(
                app.config['PILLAR_SERVER_ENDPOINT'], file_check[0]['path'])
            rfiles.append({
                "id": file_check[0]['_id'],
                "name": file_check[0]['filename'],
                "size": file_check[0]['length'],
                "url": url,
                "thumbnailUrl": url,
                "deleteUrl": url,
                "deleteType": "DELETE"
            })

    return jsonify(dict(files=rfiles))
예제 #5
0
def assets_create():
    name = request.form['name']
    parent_id = request.form.get('parent_id')
    # Detect filetype by extension (improve by detectin real file type)
    root, ext = os.path.splitext(name)
    if ext in ['.jpg', '.jpeg', '.png', '.tif', '.tiff']:
        filetype = 'image'
    elif ext in ['.blend', '.txt', '.zip']:
        filetype = 'file'
    elif ext in ['.mov', '.avi', '.mp4', '.m4v']:
        filetype = 'video'
    else:
        filetype = 'file'
    # Hash name based on file name, user id and current timestamp
    hash_name = name + str(current_user.objectid) + str(round(time.time()))
    link = hashlib.sha1(hash_name).hexdigest()
    link = os.path.join(link[:2], link + ext)

    api = SystemUtility.attract_api()
    node_type = NodeType.find_first({
        'where': '{"name" : "asset"}',
    }, api=api)
    # We will create the Node object later on, after creating the file object
    node_asset_props = dict(
        name=name,
        #description=a.description,
        #picture=picture,
        user=current_user.objectid,
        node_type=node_type._id,
        #parent=node_parent,
        properties=dict(
            content_type=filetype,
            #file=a.link[4:],
            status='processing'))

    src_dir_path = os.path.join(app.config['UPLOAD_DIR'],
                                str(current_user.objectid))

    # Move the file in designated location
    destination_dir = os.path.join(app.config['SHARED_DIR'], link[:2])
    if not os.path.isdir(destination_dir):
        os.makedirs(destination_dir)
    # (TODO) Check if filename already exsits
    src_file_path = os.path.join(src_dir_path, name)
    dst_file_path = os.path.join(destination_dir, link[3:])
    # (TODO) Thread this operation

    shutil.copy(src_file_path, dst_file_path)

    if filetype == 'file':
        mime_type = 'application'
    else:
        mime_type = filetype
    content_type = "{0}/{1}".format(mime_type, ext.replace(".", ""))

    node_file = File({
        'name': link,
        'filename': name,
        'user': current_user.objectid,
        'backend': 'cdnsun',
        'md5': '',
        'content_type': content_type,
        'length': 0
    })

    node_file.create(api=api)

    node_asset_props['properties']['file'] = node_file._id
    if parent_id:
        node_asset_props['parent'] = parent_id
    node_asset = Node(node_asset_props)
    node_asset.create(api=api)

    return jsonify(link=link,
                   name=name,
                   filetype=filetype,
                   asset_id=node_asset._id)