def tearDownClass(self):

        shutil.rmtree(
            ServerFiles.GetExpectedUpdateDir(
                self._file_service.GetServiceKey()))
        shutil.rmtree(
            ServerFiles.GetExpectedUpdateDir(
                self._tag_service.GetServiceKey()))
Example #2
0
 def _threadDoGETJob( self, request ):
     
     self._checkBandwidth( request )
     
     # no permission check as any functional account can get thumbnails
     
     hash = request.hydrus_args[ 'hash' ]
     
     ( valid, mime ) = HG.server_controller.Read( 'service_has_file', self._service_key, hash )
     
     if not valid:
         
         raise HydrusExceptions.NotFoundException( 'Thumbnail not found on this service!' )
         
     
     if mime not in HC.MIMES_WITH_THUMBNAILS:
         
         raise HydrusExceptions.NotFoundException( 'That mime should not have a thumbnail!' )
         
     
     path = ServerFiles.GetThumbnailPath( hash )
     
     response_context = HydrusServerResources.ResponseContext( 200, mime = HC.APPLICATION_OCTET_STREAM, path = path )
     
     return response_context
Example #3
0
 def setUpClass( self ):
     
     services = []
     
     self._file_service = ClientData.GenerateService( HydrusData.GenerateKey(), HC.FILE_REPOSITORY, 'file repo', {} )
     self._tag_service = ClientData.GenerateService( HydrusData.GenerateKey(), HC.TAG_REPOSITORY, 'tag repo', {} )
     self._admin_service = ClientData.GenerateService( HydrusData.GenerateKey(), HC.SERVER_ADMIN, 'server admin', {} )
     
     services_manager = HydrusGlobals.test_controller.GetServicesManager()
     
     services_manager._keys_to_services[ self._file_service.GetServiceKey() ] = self._file_service
     services_manager._keys_to_services[ self._tag_service.GetServiceKey() ] = self._tag_service
     services_manager._keys_to_services[ self._admin_service.GetServiceKey() ] = self._admin_service
     
     HydrusPaths.MakeSureDirectoryExists( ServerFiles.GetExpectedUpdateDir( self._file_service.GetServiceKey() ) )
     HydrusPaths.MakeSureDirectoryExists( ServerFiles.GetExpectedUpdateDir( self._tag_service.GetServiceKey() ) )
     
     permissions = [ HC.GET_DATA, HC.POST_DATA, HC.POST_PETITIONS, HC.RESOLVE_PETITIONS, HC.MANAGE_USERS, HC.GENERAL_ADMIN, HC.EDIT_SERVICES ]
     
     account_key = HydrusData.GenerateKey()
     account_type = HydrusData.AccountType( 'account', permissions, ( None, None ) )
     created = HydrusData.GetNow() - 100000
     expires = None
     used_bytes = 0
     used_requests = 0
     
     self._account = HydrusData.Account( account_key, account_type, created, expires, used_bytes, used_requests )
     
     self._access_key = HydrusData.GenerateKey()
     self._file_hash = HydrusData.GenerateKey()
     
     def TWISTEDSetup():
         
         reactor.listenTCP( HC.DEFAULT_SERVER_ADMIN_PORT, ServerServer.HydrusServiceAdmin( self._admin_service.GetServiceKey(), HC.SERVER_ADMIN, 'hello' ) )
         reactor.listenTCP( HC.DEFAULT_LOCAL_FILE_PORT, ClientLocalServer.HydrusServiceLocal( CC.LOCAL_FILE_SERVICE_KEY, HC.LOCAL_FILE, 'hello' ) )
         reactor.listenTCP( HC.DEFAULT_LOCAL_BOORU_PORT, ClientLocalServer.HydrusServiceBooru( CC.LOCAL_BOORU_SERVICE_KEY, HC.LOCAL_BOORU, 'hello' ) )
         reactor.listenTCP( HC.DEFAULT_SERVICE_PORT, ServerServer.HydrusServiceRepositoryFile( self._file_service.GetServiceKey(), HC.FILE_REPOSITORY, 'hello' ) )
         reactor.listenTCP( HC.DEFAULT_SERVICE_PORT + 1, ServerServer.HydrusServiceRepositoryTag( self._tag_service.GetServiceKey(), HC.TAG_REPOSITORY, 'hello' ) )
         
     
     reactor.callFromThread( TWISTEDSetup )
     
     time.sleep( 1 )
Example #4
0
    def _threadDoGETJob(self, request):

        begin = request.hydrus_args['begin']

        path = ServerFiles.GetServiceUpdatePackagePath(self._service_key,
                                                       begin)

        response_context = HydrusServerResources.ResponseContext(200,
                                                                 path=path,
                                                                 is_json=True)

        return response_context
Example #5
0
    def _threadDoGETJob(self, request):

        hash = request.hydrus_args['hash']

        # don't I need to check that we aren't stealing the file from another service?

        path = ServerFiles.GetPath('thumbnail', hash)

        response_context = HydrusServerResources.ResponseContext(200,
                                                                 path=path)

        return response_context
Example #6
0
 def _threadDoGETJob( self, request ):
     
     self._checkBandwidth( request )
     
     # no permissions check as any functional account can get updates
     
     update_hash = request.hydrus_args[ 'update_hash' ]
     
     if not self._service.HasUpdateHash( update_hash ):
         
         raise HydrusExceptions.NotFoundException( 'This update hash does not exist on this service!' )
         
     
     path = ServerFiles.GetFilePath( update_hash )
     
     response_context = HydrusServerResources.ResponseContext( 200, mime = HC.APPLICATION_OCTET_STREAM, path = path )
     
     return response_context
Example #7
0
 def _threadDoGETJob( self, request ):
     
     self._checkBandwidth( request )
     
     # no permission check as any functional account can get files
     
     hash = request.hydrus_args[ 'hash' ]
     
     ( valid, mime ) = HG.server_controller.Read( 'service_has_file', self._service_key, hash )
     
     if not valid:
         
         raise HydrusExceptions.NotFoundException( 'File not found on this service!' )
         
     
     path = ServerFiles.GetFilePath( hash )
     
     response_context = HydrusServerResources.ResponseContext( 200, mime = mime, path = path )
     
     return response_context
Example #8
0
    def _test_repo(self, service):

        service_key = service.GetServiceKey()

        # num_petitions

        num_petitions = [
            (HC.CONTENT_TYPE_MAPPINGS, HC.CONTENT_STATUS_PETITIONED, 23),
            (HC.CONTENT_TYPE_TAG_PARENTS, HC.CONTENT_STATUS_PENDING, 0)
        ]

        HG.test_controller.SetRead('num_petitions', num_petitions)

        response = service.Request(HC.GET, 'num_petitions')

        self.assertEqual(response['num_petitions'], num_petitions)

        # petition

        action = HC.CONTENT_UPDATE_PETITION
        petitioner_account = HydrusNetwork.Account.GenerateUnknownAccount()
        reason = 'it sucks'
        contents = [
            HydrusNetwork.Content(
                HC.CONTENT_TYPE_FILES,
                [HydrusData.GenerateKey() for i in range(10)])
        ]

        petition = HydrusNetwork.Petition(action, petitioner_account, reason,
                                          contents)

        HG.test_controller.SetRead('petition', petition)

        response = service.Request(HC.GET, 'petition')

        self.assertEqual(response['petition'].GetSerialisableTuple(),
                         petition.GetSerialisableTuple())

        # definitions

        definitions_update = HydrusNetwork.DefinitionsUpdate()

        if i in range(100, 200):

            definitions_update.AddRow(
                (HC.DEFINITIONS_TYPE_TAGS, i, 'series:test ' + str(i)))
            definitions_update.AddRow((HC.DEFINITIONS_TYPE_HASHES, i + 500,
                                       HydrusData.GenerateKey()))

        definitions_update_network_string = definitions_update.DumpToNetworkString(
        )

        definitions_update_hash = hashlib.sha256(
            definitions_update_network_string).digest()

        path = ServerFiles.GetExpectedFilePath(definitions_update_hash)

        with open(path, 'wb') as f:
            f.write(definitions_update_network_string)

        response = service.Request(HC.GET, 'update',
                                   {'update_hash': definitions_update_hash})

        try:
            os.remove(path)
        except:
            pass

        self.assertEqual(response, definitions_update_network_string)

        # content

        rows = [(random.randint(100, 1000),
                 [random.randint(100, 1000) for i in range(50)])
                for j in range(20)]

        content_update = HydrusNetwork.ContentUpdate()

        for row in rows:

            content_update.AddRow(
                (HC.CONTENT_TYPE_MAPPINGS, HC.CONTENT_UPDATE_ADD, row))

        content_update_network_string = content_update.DumpToNetworkString()

        content_update_hash = hashlib.sha256(
            content_update_network_string).digest()

        path = ServerFiles.GetExpectedFilePath(content_update_hash)

        with open(path, 'wb') as f:
            f.write(content_update_network_string)

        response = service.Request(HC.GET, 'update',
                                   {'update_hash': content_update_hash})

        try:
            os.remove(path)
        except:
            pass

        self.assertEqual(response, content_update_network_string)

        # metadata

        metadata = HydrusNetwork.Metadata()

        metadata.AppendUpdate([definitions_update_hash, content_update_hash],
                              HydrusData.GetNow() - 101000,
                              HydrusData.GetNow() - 1000,
                              HydrusData.GetNow() + 100000)

        service._metadata = metadata

        response = service.Request(HC.GET, 'metadata_slice', {'since': 0})

        self.assertEqual(response['metadata_slice'].GetSerialisableTuple(),
                         metadata.GetSerialisableTuple())

        # post content

        raise NotImplementedError()
        '''
Example #9
0
    def _test_file_repo(self, service):

        # file

        path = ServerFiles.GetExpectedFilePath(self._file_hash)

        HydrusPaths.MakeSureDirectoryExists(os.path.dirname(path))

        with open(path, 'wb') as f:
            f.write(EXAMPLE_FILE)

        response = service.Request(HC.GET, 'file',
                                   {'hash': self._file_hash.encode('hex')})

        self.assertEqual(response, EXAMPLE_FILE)

        try:
            os.remove(path)
        except:
            pass

        path = os.path.join(HC.STATIC_DIR, 'hydrus.png')

        with open(path, 'rb') as f:
            file = f.read()

        service.Request(HC.POST, 'file', {'file': file})

        written = HG.test_controller.GetWrite('file')

        [(args, kwargs)] = written

        (written_service_key, written_account, written_file_dict) = args

        self.assertEqual(
            written_file_dict['hash'],
            '\xadm5\x99\xa6\xc4\x89\xa5u\xeb\x19\xc0&\xfa\xce\x97\xa9\xcdey\xe7G(\xb0\xce\x94\xa6\x01\xd22\xf3\xc3'
        )
        self.assertEqual(written_file_dict['ip'], '127.0.0.1')
        self.assertEqual(written_file_dict['height'], 200)
        self.assertEqual(written_file_dict['width'], 200)
        self.assertEqual(written_file_dict['mime'], 2)
        self.assertEqual(written_file_dict['size'], 5270)

        # ip

        (ip, timestamp) = ('94.45.87.123', HydrusData.GetNow() - 100000)

        HG.test_controller.SetRead('ip', (ip, timestamp))

        response = service.Request(HC.GET, 'ip',
                                   {'hash': self._file_hash.encode('hex')})

        self.assertEqual(response['ip'], ip)
        self.assertEqual(response['timestamp'], timestamp)

        # thumbnail

        path = ServerFiles.GetExpectedThumbnailPath(self._file_hash)

        HydrusPaths.MakeSureDirectoryExists(os.path.dirname(path))

        with open(path, 'wb') as f:
            f.write(EXAMPLE_THUMBNAIL)

        response = service.Request(HC.GET, 'thumbnail',
                                   {'hash': self._file_hash.encode('hex')})

        self.assertEqual(response, EXAMPLE_THUMBNAIL)

        try:
            os.remove(path)
        except:
            pass
Example #10
0
    def _test_repo(self, service, host, port):

        service_key = service.GetServiceKey()

        # news

        news = 'this is the news'

        service.Request(HC.POST, 'news', {'news': news})

        written = HydrusGlobals.test_controller.GetWrite('news')

        [(args, kwargs)] = written

        (written_service_key, written_news) = args

        self.assertEqual(news, written_news)

        # num_petitions

        num_petitions = 23

        HydrusGlobals.test_controller.SetRead('num_petitions', num_petitions)

        response = service.Request(HC.GET, 'num_petitions')

        self.assertEqual(response['num_petitions'], num_petitions)

        # petition

        action = HC.CONTENT_UPDATE_PETITION
        account_identifier = HydrusData.AccountIdentifier(
            account_key=HydrusData.GenerateKey())
        reason = 'it sucks'
        contents = [
            HydrusData.Content(HC.CONTENT_TYPE_FILES,
                               [HydrusData.GenerateKey() for i in range(10)])
        ]

        petition = HydrusData.ServerToClientPetition(
            action=action,
            petitioner_account_identifier=account_identifier,
            reason=reason,
            contents=contents)

        HydrusGlobals.test_controller.SetRead('petition', petition)

        response = service.Request(HC.GET, 'petition')

        self.assertEqual(type(response), HydrusData.ServerToClientPetition)

        # update

        begin = 100
        subindex_count = 5

        update = HydrusData.ServerToClientServiceUpdatePackage()
        update.SetBeginEnd(begin, begin + HC.UPDATE_DURATION - 1)
        update.SetSubindexCount(subindex_count)

        path = ServerFiles.GetExpectedServiceUpdatePackagePath(
            service_key, begin)

        with open(path, 'wb') as f:
            f.write(update.DumpToNetworkString())

        response = service.Request(HC.GET, 'service_update_package',
                                   {'begin': begin})

        self.assertEqual(response.GetBegin(), update.GetBegin())

        try:
            os.remove(path)
        except:
            pass

        subindex = 2
        num_hashes = 12
        tag = 'series:blah'
        hash_ids_to_hashes = {i: HydrusData.GenerateKey() for i in range(12)}
        rows = [(tag, [i for i in range(num_hashes)])]

        update = HydrusData.ServerToClientContentUpdatePackage()
        update.AddContentData(HC.CONTENT_TYPE_MAPPINGS, HC.CONTENT_UPDATE_ADD,
                              rows, hash_ids_to_hashes)

        path = ServerFiles.GetExpectedContentUpdatePackagePath(
            service_key, begin, subindex)

        with open(path, 'wb') as f:
            f.write(update.DumpToNetworkString())

        response = service.Request(HC.GET, 'content_update_package', {
            'begin': begin,
            'subindex': subindex
        })

        self.assertEqual(response.GetNumContentUpdates(),
                         update.GetNumContentUpdates())

        try:
            os.remove(path)
        except:
            pass

        update = HydrusData.ClientToServerContentUpdatePackage(
            {}, hash_ids_to_hashes)

        service.Request(HC.POST, 'content_update_package', {'update': update})

        written = HydrusGlobals.test_controller.GetWrite('update')

        [(args, kwargs)] = written

        (written_service_key, written_account, written_update) = args

        self.assertEqual(update.GetHashes(), written_update.GetHashes())
Example #11
0
    def _test_file_repo(self, service, host, port):

        info = service.GetInfo()

        info['access_key'] = self._access_key

        # file

        path = ServerFiles.GetExpectedPath('file', self._file_hash)

        with open(path, 'wb') as f:
            f.write('file')

        response = service.Request(HC.GET, 'file',
                                   {'hash': self._file_hash.encode('hex')})

        self.assertEqual(response, 'file')

        try:
            os.remove(path)
        except:
            pass

        path = os.path.join(HC.STATIC_DIR, 'hydrus.png')

        with open(path, 'rb') as f:
            file = f.read()

        service.Request(HC.POST, 'file', {'file': file})

        written = HydrusGlobals.test_controller.GetWrite('file')

        [(args, kwargs)] = written

        (written_service_key, written_account, written_file_dict) = args

        self.assertEqual(
            written_file_dict['hash'],
            '\xadm5\x99\xa6\xc4\x89\xa5u\xeb\x19\xc0&\xfa\xce\x97\xa9\xcdey\xe7G(\xb0\xce\x94\xa6\x01\xd22\xf3\xc3'
        )
        self.assertEqual(written_file_dict['ip'], '127.0.0.1')
        self.assertEqual(written_file_dict['height'], 200)
        self.assertEqual(written_file_dict['width'], 200)
        self.assertEqual(written_file_dict['mime'], 2)
        self.assertEqual(written_file_dict['size'], 5270)

        # ip

        (ip, timestamp) = ('94.45.87.123', HydrusData.GetNow() - 100000)

        HydrusGlobals.test_controller.SetRead('ip', (ip, timestamp))

        response = service.Request(HC.GET, 'ip',
                                   {'hash': self._file_hash.encode('hex')})

        self.assertEqual(response['ip'], ip)
        self.assertEqual(response['timestamp'], timestamp)

        # thumbnail

        path = ServerFiles.GetExpectedPath('thumbnail', self._file_hash)

        with open(path, 'wb') as f:
            f.write('thumb')

        response = service.Request(HC.GET, 'thumbnail',
                                   {'hash': self._file_hash.encode('hex')})

        self.assertEqual(response, 'thumb')

        try:
            os.remove(path)
        except:
            pass