コード例 #1
0
    def DoDeferredPhysicalDeletes(self):

        num_files_deleted = 0
        num_thumbnails_deleted = 0

        pauser = HydrusData.BigJobPauser()

        (file_hash, thumbnail_hash) = self.Read('deferred_physical_delete')

        while (file_hash is not None
               or thumbnail_hash is not None) and not HG.view_shutdown:

            if file_hash is not None:

                path = ServerFiles.GetExpectedFilePath(file_hash)

                if os.path.exists(path):

                    HydrusPaths.RecyclePath(path)

                    num_files_deleted += 1

            if thumbnail_hash is not None:

                path = ServerFiles.GetExpectedThumbnailPath(thumbnail_hash)

                if os.path.exists(path):

                    HydrusPaths.RecyclePath(path)

                    num_thumbnails_deleted += 1

            self.WriteSynchronous('clear_deferred_physical_delete',
                                  file_hash=file_hash,
                                  thumbnail_hash=thumbnail_hash)

            (file_hash, thumbnail_hash) = self.Read('deferred_physical_delete')

            pauser.Pause()

        if num_files_deleted > 0 or num_thumbnails_deleted > 0:

            HydrusData.Print(
                'Physically deleted {} files and {} thumbnails from file storage.'
                .format(HydrusData.ToHumanInt(num_files_deleted),
                        HydrusData.ToHumanInt(num_files_deleted)))
コード例 #2
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})

        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_bytes = f.read()

        HG.test_controller.ClearWrites('file')

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

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

        [(args, kwargs)] = written

        (written_service_key, written_account, written_file_dict) = args

        hash = b'\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['hash'], hash)
        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})

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

        # account from hash

        subject_content = HydrusNetwork.Content(
            content_type=HC.CONTENT_TYPE_FILES, content_data=hash)

        subject_account_identifier = HydrusNetwork.AccountIdentifier(
            content=subject_content)

        HG.test_controller.SetRead('account', self._account)

        response = service.Request(
            HC.GET, 'other_account',
            {'subject_identifier': subject_account_identifier})

        self.assertEqual(repr(response['account']), repr(self._account))

        # 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})

        self.assertEqual(response, EXAMPLE_THUMBNAIL)

        try:
            os.remove(path)
        except:
            pass