Exemple #1
0
    def test_oembed_response_json_for_link(self):
        url = 'https://vimeo.com/20379529'
        sourcefile = Sourcefile(width=100,
                                height=100,
                                data="{'junk':'here'}",
                                type='link',
                                file_key="asdfasdfasdfasdfasdf")
        sourcefile.save()

        sharedfile = Sharedfile(source_id=sourcefile.id,
                                user_id=self.user.id,
                                name=url,
                                title="Some Title",
                                source_url=url,
                                content_type='text/html')
        sharedfile.save()
        sharedfile.share_key = base36encode(sharedfile.id)
        sharedfile.save()
        sharedfile = Sharedfile.get('id = %s', 1)
        file_time_stamp = int(time.mktime(sharedfile.created_at.timetuple()))
        self.http_client.fetch(
            self.get_url("/services/oembed?url=http%3A//mltshp.com/p/1"),
            self.stop)
        response = self.wait()
        j_response = json_decode(response.body)
        self.assertEqual(j_response['type'], "link")
        self.assertEqual(j_response['url'], url)
Exemple #2
0
    def save_to_shake(self, for_user, to_shake=None):
        """
        Saves this file to a user's shake, or to the to_shake
        if it is provided.
        """
        new_sharedfile = Sharedfile()
        new_sharedfile.user_id = for_user.id
        new_sharedfile.name = self.name
        new_sharedfile.title = self.title
        new_sharedfile.content_type = self.content_type
        new_sharedfile.source_url = self.source_url
        new_sharedfile.source_id = self.source_id
        new_sharedfile.parent_id = self.id
        new_sharedfile.description = self.description

        if self.original_id == 0:
            new_sharedfile.original_id = self.id
        else:
            new_sharedfile.original_id = self.original_id
        new_sharedfile.save(ignore_tags=True)
        new_sharedfile.share_key = base36encode(new_sharedfile.id)
        new_sharedfile.save(ignore_tags=True)

        if to_shake:
            shake_to_save = to_shake
        else:
            shake_to_save = for_user.shake()
        new_sharedfile.add_to_shake(shake_to_save)

        #create a notification to the sharedfile owner
        notification.Notification.new_save(for_user, self)

        calculate_saves.delay_or_run(self.id)
        return new_sharedfile
Exemple #3
0
    def test_query_friend_shake_before_after(self):
        """
        Admin (user_a), who user2 (user_b) follows, uploads 10 files, which appear in
        Admin's stream.

        /friends/before/{share_key} should return all files that came before passed in share_key
        while /friends/after/{share_key} should return those that came after.

        We assume there is already one file uploaded by Admin (share_key -> 1)
        """
        files = []
        for x in range(10):
            source_file = Sourcefile(width=10, height=10, file_key='mumbles', thumb_key='bumbles')
            source_file.save()
            sf = Sharedfile(source_id = source_file.id, user_id = self.user_a.id, name="shgaredfile.png",
                            title='shared file', content_type='image/png', deleted=0)
            sf.save()
            sf.share_key = base36encode(sf.id)
            sf.save()
            sf.add_to_shake(self.user_a.shake())
            files.append(sf)

        request = signed_request(self.access_token, self.get_url('/api/friends/before/%s' % files[3].share_key))
        self.http_client.fetch(request, self.stop)
        response = self.wait()
        j_response = json_decode(response.body)
        self.assertEqual(4, len(j_response['friend_shake']))

        request = signed_request(self.access_token, self.get_url('/api/friends/after/%s' % files[3].share_key))
        self.http_client.fetch(request, self.stop)
        response = self.wait()
        j_response = json_decode(response.body)
        self.assertEqual(6, len(j_response['friend_shake']))
Exemple #4
0
    def create_from_file(file_path,
                         file_name,
                         sha1_value,
                         content_type,
                         user_id,
                         title=None,
                         shake_id=None,
                         skip_s3=None):
        """
        TODO: Must only accept acceptable content-types after consulting a list.
        """
        if len(sha1_value) <> 40:
            return None

        if user_id == None:
            return None

        if content_type not in [
                'image/gif', 'image/jpeg', 'image/jpg', 'image/png'
        ]:
            return None

        # If we have no shake_id, drop in user's main shake. Otherwise, validate that the specififed
        # shake is a group shake that the user has permissions for.
        if not shake_id:
            destination_shake = shake.Shake.get(
                'user_id = %s and type=%s and deleted=0', user_id, 'user')
        else:
            destination_shake = shake.Shake.get('id=%s and deleted=0',
                                                shake_id)
            if not destination_shake:
                return None
            if not destination_shake.can_update(user_id):
                return None

        sf = sourcefile.Sourcefile.get_from_file(file_path,
                                                 sha1_value,
                                                 skip_s3=skip_s3)

        if sf:
            shared_file = Sharedfile(user_id=user_id,
                                     name=file_name,
                                     content_type=content_type,
                                     source_id=sf.id,
                                     title=title,
                                     size=path.getsize(file_path))
            shared_file.save()
            if shared_file.saved():
                shared_file.share_key = base36encode(shared_file.id)
                shared_file.save()
                shared_file.add_to_shake(destination_shake)

                if options.use_workers and content_type == "image/gif":
                    transcode_sharedfile.delay_or_run(shared_file.id)
                return shared_file
            else:
                return None
        else:
            return None
Exemple #5
0
    def on_thumbnail_response(self, response):
        if response.code != 200:
            self.render(
                "tools/save-video-error.html",
                message=
                "We could not load the thumbnail for this file and therefore could not save this video. Please contact support."
            )
            return

        # save the response
        url = self.get_argument('url')
        current_user = self.get_current_user_object()

        sha1_key = Sourcefile.get_sha1_file_key(file_path=None, file_data=url)
        thumbnail_path = "%s/%s" % (options.uploaded_files, sha1_key)
        fh = open(thumbnail_path, 'wb')
        fh.write(response.body)
        fh.close()
        source_file = Sourcefile.create_from_json_oembed(
            link=url,
            oembed_doc=self.oembed_doc,
            thumbnail_file_path=thumbnail_path)
        #cleanup
        if not options.debug:
            try:
                os.remove(thumbnail_path)
            except:
                pass

        title = ''
        if self.oembed_doc.has_key('title'):
            title = self.oembed_doc['title']

        shared_file = Sharedfile(user_id=current_user.id,
                                 name=url,
                                 content_type='text/html',
                                 source_id=source_file.id,
                                 title=title,
                                 source_url=url)
        shared_file.save()

        share_key = base36encode(shared_file.id)
        shared_file.share_key = share_key
        shared_file.save()

        user_shake = Shake.get('user_id = %s and type=%s', current_user.id,
                               'user')
        shared_file.add_to_shake(self.destination_shake)

        if self.oembed_doc.has_key('description'):
            shared_file.description = self.oembed_doc['description']

        self.write({'path': "/p/%s" % (share_key)})
        self.finish()
Exemple #6
0
    def generate(authorization_id):
        """
        Generate an access token based on an (unexpired) authorization id.
        """
        auth = authorizationcode.Authorizationcode.get('id=%s',
                                                       authorization_id)
        consumer_key = uuid.uuid3(
            uuid.NAMESPACE_DNS,
            base36encode(auth.id) + '-' + base36encode(auth.app_id))
        consumer_secret = sha224("%s%s" %
                                 (str(uuid.uuid1()), time.time())).hexdigest()

        if auth.expires_at > datetime.utcnow():
            access_token = Accesstoken(user_id=auth.user_id,
                                       app_id=auth.app_id,
                                       consumer_key=str(consumer_key),
                                       consumer_secret=str(consumer_secret))
            access_token.save()
            return access_token
        else:
            return None
Exemple #7
0
 def _post_to_shake(self, user, to_shake=None):
     """
     Utility method for creating a post to a shake.  If shake is
     not specified, the newly created sharedfile will be added to
     the user's shake.
     """
     source_file = Sourcefile(width=10, height=10, file_key='mumbles', thumb_key='bumbles')
     source_file.save()
     sf = Sharedfile(source_id=source_file.id, user_id=user.id, name="sharedfile.png",
                     title='shared file', content_type='image/png', deleted=0)
     sf.save()
     sf.share_key = base36encode(sf.id)
     sf.save()
     if to_shake:
         sf.add_to_shake(to_shake)
     else:
         sf.add_to_shake(user.shake())
     return sf
Exemple #8
0
 def key(self):
     return "%s-%s" % (base36encode(self.user_id), base36encode(self.id))
Exemple #9
0
 def previous_sharedfile_key(self):
     return base36encode(self.previous_sharedfile_id)
Exemple #10
0
 def sharedfile_key(self):
     return base36encode(self.sharedfile_id)
Exemple #11
0
    def on_thumbnail_response(self, response):
        if response.code != 200:
            self.render(
                "tools/save-video-error.html",
                message=
                "We could not load the thumbnail for this file and therefore could not save this video. Please contact support."
            )
            return

        #if the thumbnail url needs to be extracted (Flickr) let's see if
        # we got back HTML that points to the thumbnail
        if self.oembed_doc['provider_name'] == "Flickr" and response.headers[
                'Content-Type'] == 'text/html; charset=utf-8':
            #if we're here, that means we need to extract the thumbnail and make a call to the actual jpg
            s = re.search(
                '<link rel="image_src" href="http://farm(\d).static.flickr.com/(\d+)/(\d+)_([a-zA-Z0-9]+)_m.jpg">',
                response.body)
            try:
                if s and s.group(0) and s.group(1) and s.group(2) and s.group(
                        3) and s.group(4):
                    self.oembed_doc[
                        'thumbnail_url'] = "http://farm%s.static.flickr.com/%s/%s_%s_b.jpg" % (
                            s.group(1), s.group(2), s.group(3), s.group(4))
                    request = HTTPRequest(self.oembed_doc['thumbnail_url'],
                                          'GET')
                    http = tornado.httpclient.AsyncHTTPClient()
                    http.fetch(request, self.on_thumbnail_response)
            except:
                self.render(
                    "tools/save-video-error.html",
                    message=
                    "We could not load the thumbnail for this file and therefore could not save this video. Please contact support."
                )
                return
        elif self.oembed_doc['provider_name'] == "Vine" and response.headers[
                'Content-Type'] == 'text/html; charset=utf-8':
            # if we're here, that means we need to extract the thumbnail and make a call to the actual jpg
            # use BeautfilSoup to parse for the title and meta tag. We'll do this bit of danger in a
            # try block and shrug if something bad happens
            try:
                soup = BeautifulSoup(
                    response.body, convertEntities=BeautifulSoup.HTML_ENTITIES)
                self.oembed_doc['title'] = soup.title.text
                thumbnail = soup.find('meta', {"property": "og:image"})
                if thumbnail:
                    self.oembed_doc['thumbnail_url'] = thumbnail.attrMap[
                        'content']

                    request = HTTPRequest(self.oembed_doc['thumbnail_url'],
                                          'GET')
                    http = tornado.httpclient.AsyncHTTPClient()
                    http.fetch(request, self.on_thumbnail_response)
                    return
            except:
                pass
            # either we failed to find a thumbnail url, or an exception was raised
            # while attempting to fetch.
            self.render(
                "tools/save-video-error.html",
                message=
                "We could not load the thumbnail for this file and therefore could not save this video. Please contact support."
            )
        else:
            # save the response
            url = self.get_argument('url')
            current_user = self.get_current_user_object()

            sha1_key = Sourcefile.get_sha1_file_key(file_path=None,
                                                    file_data=url)
            thumbnail_path = "%s/%s" % (options.uploaded_files, sha1_key)
            fh = open(thumbnail_path, 'wb')
            fh.write(response.body)
            fh.close()
            source_file = Sourcefile.create_from_json_oembed(
                link=url,
                oembed_doc=self.oembed_doc,
                thumbnail_file_path=thumbnail_path)
            #cleanup
            if not options.debug:
                try:
                    os.remove(thumbnail_path)
                except:
                    pass

            title = ''
            if self.oembed_doc.has_key('title'):
                title = self.oembed_doc['title']

            shared_file = Sharedfile(user_id=current_user.id,
                                     name=url,
                                     content_type='text/html',
                                     source_id=source_file.id,
                                     title=title,
                                     source_url=url)
            shared_file.save()

            share_key = base36encode(shared_file.id)
            shared_file.share_key = share_key
            shared_file.save()

            user_shake = Shake.get('user_id = %s and type=%s', current_user.id,
                                   'user')
            shared_file.add_to_shake(self.destination_shake)

            if self.oembed_doc.has_key('description'):
                shared_file.description = self.oembed_doc['description']

            self.write({'path': "/p/%s" % (share_key)})
            self.finish()