def _migration(self, name, instance, **kwargs):
        """Migrates data from the original storage

        """
        value = getattr(aq_base(instance), name, _marker)
        if value is _marker:
                raise AttributeError(name)
        delattr(instance, name)

        # we use set method from base class to prevent migration to aws
        AnnotationStorage.set(self, name, instance, value, **kwargs)
        return value
Ejemplo n.º 2
0
    def set(self, name, instance, value, **kwargs):
        """Store video on Kaltura, 
           create media entry if required
        """
        initializing = kwargs.get('_initializing_', False)

        if initializing:
            AnnotationStorage.set(self, name, instance, value, **kwargs)
            return

        self.value = aq_base(value)
        if self.value.filename is None:
            return  #only interested in running set when instance is ready to save.

        #get a filehandle for the video content we are uploading to Kaltura Server
        fh_blob = openBlob(self.value.blob, mode='r')

        #find the temp dir that ZODB is using.
        tempdir = os.path.dirname(fh_blob.name)

        #connect to Kaltura Server
        (client, ks) = kconnect()
        #upload video content.

        token = KalturaUploadToken()
        token = client.uploadToken.add(token)
        token = client.uploadToken.upload(token.getId(), fh_blob)

        fh_blob.close()
        #instance needs to know the upload token to finalize the media entry
        # typically, a call to Kaltura's addFromUploadedFile or updateContent services does this.
        instance.uploadToken = token
        instance.fileChanged = True

        #if "no local storage" is set, we clobber the blob file.
        registry = getUtility(IRegistry)
        settings = registry.forInterface(IRfaKalturaSettings)
        if settings.storageMethod == u"No Local Storage":
            filename_aside = self.makeDummyData(dir=tempdir)
            value.blob.consumeFile(filename_aside)

        AnnotationStorage.set(self, name, instance, value, **kwargs)
    def get(self, name, instance, **kwargs):
        aws_utility = getUtility(IAWSFileClientUtility)
        if not aws_utility.active():
            return AnnotationStorage.get(self, name, instance, **kwargs)

        file_ = AnnotationStorage.get(self, name, instance, **kwargs)
        request = instance.REQUEST
        if not isinstance(request, type('')) and \
                request.get('%s_migrate' % name, '') or \
                ('migrate' in kwargs and kwargs['migrate']):
            # check if object is already migrated
            if isinstance(file_, AWSFile):
                return file_
            try:
                new_file_ = self._do_migrate(file_, instance)
            except (FileClientRemoveError, FileClientStoreError):
                return file_

            AnnotationStorage.set(self, name, instance, new_file_, **kwargs)
            return new_file_
        else:
            return file_
    def set(self, name, instance, value, **kwargs):
        """Set a value under the key 'name' for retrevial by/for
        instance."""

        # collect value info
        filename = getattr(value, 'filename', '')
        content_type = getattr(value, 'content_type', '')
        width = getattr(value, 'width', '')
        height = getattr(value, 'height', '')

        aws_utility = getUtility(IAWSFileClientUtility)
        if not aws_utility.active():
            if isinstance(value, AWSFile):
                # use default OFS.Image or OFS.File
                if width and height:
                    # we have image
                    value = Image(value.id(), '', str(value.data),
                                  content_type=content_type)
                else:
                    value = File(value.id(), '', str(value.data),
                                 content_type=content_type)
                setattr(value, 'filename', filename)
            AnnotationStorage.set(self, name, instance, value, **kwargs)
            return

        try:
            file_ = self.get(name, instance, **kwargs)
        except AttributeError:
            file_ = None

        if file_:
            if isinstance(file_, AWSFile):
                try:
                    self.update_source(file_, value.data, instance,
                                       filename, content_type, width, height)
                except (FileClientRemoveError, FileClientStoreError), e:
                    request = instance.REQUEST
                    IStatusMessage(request).addStatusMessage(
                        u"Couldn't update %s file to storage. %s" %
                        (safe_unicode(filename),
                         safe_unicode(e.message)), type='error')
                    if isinstance(value, AWSFile):
                        # use default OFS.Image or OFS.File
                        if width and height:
                            # we have image
                            value = Image(value.id(), '', str(value.data),
                                          content_type=content_type)
                        else:
                            value = File(value.id(), '', str(value.data),
                                         content_type=content_type)
                        setattr(value, 'filename', filename)
                    AnnotationStorage.set(self, name, instance,
                                          value, **kwargs)
                else:
                    # clean up data after update
                    setattr(file_, 'data', '')
            else:
                try:
                    file_ = self._do_migrate(file_, instance,
                                             data=value.data,
                                             filename=filename,
                                             content_type=content_type,
                                             width=width,
                                             height=height)
                except (FileClientRemoveError, FileClientStoreError):
                    request = instance.REQUEST
                    IStatusMessage(request).addStatusMessage(
                        u"Couldn't update %s file to storage. %s" %
                        (safe_unicode(filename),
                         safe_unicode(e.message)), type='error')
                    AnnotationStorage.set(self, name, instance,
                                          value, **kwargs)
                else:
                    AnnotationStorage.set(self, name, instance,
                                          file_, **kwargs)
                    AnnotationStorage.set(self, name, instance,
                                          file_, **kwargs)
        else:
            if value.size:
                file_ = AWSFile(name)
                try:
                    self.update_source(file_, value.data, instance,
                                       filename, content_type, width, height)
                except (FileClientRemoveError, FileClientStoreError), e:
                    request = instance.REQUEST
                    IStatusMessage(request).addStatusMessage(
                        u"Couldn't update %s file to storage. %s" %
                        (safe_unicode(filename),
                         safe_unicode(e.message)), type='error')

                    AnnotationStorage.set(self, name, instance,
                                          value, **kwargs)
                else:
                    AnnotationStorage.set(self, name, instance,
                                          file_, **kwargs)
            else:
                AnnotationStorage.set(self, name, instance, file_, **kwargs)

    security.declarePrivate('unset')
    def unset(self, name, instance, **kwargs):
        aws_utility = getUtility(IAWSFileClientUtility)
        if not aws_utility.active():
            return AnnotationStorage.unset(self, name, instance, **kwargs)

        file_ = self.get(name, instance, **kwargs)
        if isinstance(file_, AWSFile):
            file_.remove_source()