Я пытаюсь получить некоторый контент из корзины s3 AWS, и после запроса данных это занимает гораздо больше времени, чем я могу себе позволить, около 15-20 секунд. - PullRequest
0 голосов
/ 09 апреля 2020

вот мой api. php код файла в теле попытки, иначе дело занимает слишком много времени

<?php
    require_once(dirname(__FILE__) . "/constants.php");
    includeMonstaConfig();

    session_start();
    require_once(dirname(__FILE__) . '/lib/helpers.php');
    require_once(dirname(__FILE__) . '/lib/response_helpers.php');
    require_once(dirname(__FILE__) . '/request_processor/RequestMarshaller.php');

    if (file_exists(dirname(__FILE__) . '/../../mftp_extensions.php')) {
        include_once(dirname(__FILE__) . '/../../mftp_extensions.php');
    }

    dieIfNotPOST();

    require_once(dirname(__FILE__) . '/lib/access_check.php');

    $marshaller = new RequestMarshaller();

    try {
        $request = json_decode($_POST['request'], true);

        if ($request['actionName'] == 'fetchFile' || $request['actionName'] == 'downloadMultipleFiles') {
            switch ($request['actionName']) {
                case 'fetchFile':
                    $outputPath = $marshaller->prepareFileForFetch($request);
                    $outputFileName = monstaBasename($request['context']['remotePath']);
                    break;
                case 'downloadMultipleFiles':
                    $outputResponse = $marshaller->marshallRequest($request, false, true);
                    $outputPath = $outputResponse["data"];
                    $outputFileName = "mftp_zip_" . date("Y_m_d_H_i_s") . ".zip";
            }

            $fileKey = generateRandomString(16);

            $_SESSION[MFTP_SESSION_KEY_PREFIX . $fileKey] = array(
                "path" => $outputPath,
                "fileName" => $outputFileName
            );

            $response = array(
                "success" => true,
                "fileKey" => $fileKey
            );

            print json_encode($response);
        } else {
            $skipConfigurationActions = array('checkSavedAuthExists', 'writeSavedAuth', 'readSavedAuth',
                'readLicense', 'getSystemVars', 'resetPassword', 'forgotPassword', 'validateSavedAuthPassword',
                'downloadLatestVersionArchive', 'installLatestVersion');

            $skipConfiguration = in_array($request['actionName'], $skipConfigurationActions);

            $serializedResponse = $marshaller->marshallRequest($request, $skipConfiguration);

            print $serializedResponse;
        }
    } catch (Exception $e) {
        $marshaller->disconnect();
        handleExceptionInRequest($e);
    }

    $marshaller->disconnect();

вот запрос RequestMarshaller. php код, показывающий только код запроса

<?php

    require_once(dirname(__FILE__) . '/RequestDispatcher.php');
    require_once(dirname(__FILE__) . "/../lib/helpers.php");
    require_once(dirname(__FILE__) . "/../system/ApplicationSettings.php");

    class RequestMarshaller {
        /**
         * @var RequestDispatcher
         */
        private $requestDispatcher;


        private function initRequestDispatcher($request, $skipConfiguration = false) {
            if(!$skipConfiguration)
                $request['configuration'] = $this->applyConnectionRestrictions($request['connectionType'],
                    $request['configuration']);

            if (is_null($this->requestDispatcher))
                $this->requestDispatcher = new RequestDispatcher($request['connectionType'], $request['configuration'],
                    null, null, $skipConfiguration);
        }


        public function marshallRequest($request, $skipConfiguration = false, $skipEncode = false) {
            $this->initRequestDispatcher($request, $skipConfiguration);

            $response = array();

            if ($request['actionName'] == 'putFileContents')
                $response = $this->putFileContents($request);
            else if ($request['actionName'] == 'getFileContents')
                $response = $this->getFileContents($request);
            else {
                $context = array_key_exists('context', $request) ? $request['context'] : null;

                $responseData = $this->requestDispatcher->dispatchRequest($request['actionName'], $context);
                $response['success'] = true;

                if(is_object($responseData)) {
                    $response['data'] = method_exists($responseData, 'legacyJsonSerialize') ?
                        $responseData->legacyJsonSerialize() : $responseData;
                } else
                    $response['data'] = $responseData;
            }

            if ($skipEncode)
                return $response;

            return json_encode($response);
        }

        }
    }

и вот мой RequestDispatcher. php код

<?php

    class RequestDispatcher {
        /**
         * @var ConnectionBase
         */
        private $connection;

        /**
         * @var string
         */
        private $connectionType;

        /**
         * @var array
         */
        private $rawConfiguration;
        public $username;
        public function getusername(){
            $configuration = $this->connection->getConfiguration();
            $this->username=$configuration->getRemoteUsername();
            return $this->username;
        }
        public function __construct($connectionType, $rawConfiguration, $configurationFactory = null,
                                    $connectionFactory = null, $skipConfiguration = false) {
            $this->connectionType = $connectionType;
            /* allow factory objects to be passed in for testing with mocks */
            if ($skipConfiguration) {
                $this->connection = null;
            } else {
                $this->rawConfiguration = $rawConfiguration;
                $configurationFactory = is_null($configurationFactory) ? new ConfigurationFactory() : $configurationFactory;
                $connectionFactory = is_null($connectionFactory) ? new ConnectionFactory() : $connectionFactory;
                $configuration = $configurationFactory->getConfiguration($connectionType, $rawConfiguration);
                $this->connection = $connectionFactory->getConnection($connectionType, $configuration);
            }
        }

        public function dispatchRequest($actionName, $context = null) {
            if (in_array($actionName, array(
                'listDirectory',
                'downloadFile',
                'uploadFile',
                'deleteFile',
                'makeDirectory',
                'deleteDirectory',
                'rename',
                'changePermissions',
                'copy',
                'testConnectAndAuthenticate',
                'checkSavedAuthExists',
                'writeSavedAuth',
                'readSavedAuth',
                'readLicense',
                'getSystemVars',
                'fetchRemoteFile',
                'uploadFileToNewDirectory',
                'downloadMultipleFiles',
                'createZip',
                'setApplicationSettings',
                'deleteMultiple',
                'extractArchive',
                'updateLicense',
                'reserveUploadContext',
                'transferUploadToRemote',
                'getRemoteFileSize',
                'getDefaultPath',
                'downloadForExtract',
                'cleanUpExtract',
                'resetPassword',
                'forgotPassword',
                'validateSavedAuthPassword',
                'downloadLatestVersionArchive',
                'installLatestVersion'
            ))) {
                if (!is_null($context))
                    return $this->$actionName($context);
                else
                    return $this->$actionName();
            }

            throw new InvalidArgumentException("Unknown action $actionName");
        }

        public function getConnection() {
            return $this->connection;
        }

        private function connectAndAuthenticate($isTest = false) {
            $sessionNeedsStarting = false;

            if (function_exists("session_status")) {
                if (session_status() == PHP_SESSION_NONE) {
                    $sessionNeedsStarting = true;
                }
            } else {
                $sessionNeedsStarting = session_id() == "";
            }
            $configuration = $this->connection->getConfiguration();
            $this->username=$configuration->getRemoteUsername();
            if ($sessionNeedsStarting && !defined("MONSTA_UNIT_TEST_MODE")) {  // TODO: pass in this as parameter to avoid global state
                session_start();
                $_SESSION["RemoteUsername"]=$configuration->getRemoteUsername();


            }



            $maxFailures = defined("MFTP_MAX_LOGIN_FAILURES") ? MFTP_MAX_LOGIN_FAILURES : 0;
            $loginFailureResetTimeSeconds = defined("MFTP_LOGIN_FAILURES_RESET_TIME_MINUTES")
                ? MFTP_LOGIN_FAILURES_RESET_TIME_MINUTES * 60 : 0;

            if (!isset($_SESSION["MFTP_LOGIN_FAILURES"]))
                $_SESSION["MFTP_LOGIN_FAILURES"] = array();

            $banManager = new UserBanManager($maxFailures, $loginFailureResetTimeSeconds,
                $_SESSION["MFTP_LOGIN_FAILURES"]);

            if ($banManager->hostAndUserBanned($configuration->getHost(), $configuration->getRemoteUsername())) {
                mftpActionLog("Log in", $this->connection, "", "", "Login and user has exceed maximum failures.");
                throw new FileSourceAuthenticationException("Login and user has exceed maximum failures.",
                    LocalizableExceptionDefinition::$LOGIN_FAILURE_EXCEEDED_ERROR, array(
                        "banTimeMinutes" => MFTP_LOGIN_FAILURES_RESET_TIME_MINUTES
                    ));
            }

            try {
                $this->connection->connect();
            } catch (Exception $e) {
                mftpActionLog("Log in", $this->connection, "", "", $e->getMessage());
                throw $e;
            }

            try {
                $this->connection->authenticate();
            } catch (Exception $e) {
                mftpActionLog("Log in", $this->connection, "", "", $e->getMessage());

                $banManager->recordHostAndUserLoginFailure($configuration->getHost(),
                    $configuration->getRemoteUsername());

                $_SESSION["MFTP_LOGIN_FAILURES"] = $banManager->getStore();

                throw $e;
            }

            $banManager->resetHostUserLoginFailure($configuration->getHost(), $configuration->getRemoteUsername());

            $_SESSION["MFTP_LOGIN_FAILURES"] = $banManager->getStore();

            if ($isTest) {
                // only log success if it is the first connect from the user
                mftpActionLog("Log in", $this->connection, "", "", "");
            }

            if ($configuration->getInitialDirectory() === "" || is_null($configuration->getInitialDirectory())) {
                return $this->connection->getCurrentDirectory();
            }

            return null;
        }

        public function disconnect() {
            if ($this->connection != null && $this->connection->isConnected())
                $this->connection->disconnect();
        }

        public function listDirectory($context) {
            $this->connectAndAuthenticate();
            $directoryList = $this->connection->listDirectory($context['path'], $context['showHidden']);
            $this->disconnect();
            return $directoryList;
        }

        public function downloadFile($context, $skipLog = false) {
            $this->connectAndAuthenticate();
            $transferOp = TransferOperationFactory::getTransferOperation($this->connectionType, $context);
            $this->connection->downloadFile($transferOp);
            if (!$skipLog) {
                // e.g. if editing a file don't log that it was also downloaded
                mftpActionLog("Download file", $this->connection, dirname($transferOp->getRemotePath()), monstaBasename($transferOp->getRemotePath()), "");
            }
            $this->disconnect();
        }

        public function downloadMultipleFiles($context) {
            $this->connectAndAuthenticate();
            $fileFinder = new RecursiveFileFinder($this->connection, $context['baseDirectory']);
            $foundFiles = $fileFinder->findFilesInPaths($context['items']);

            foreach ($foundFiles as $foundFile) {
                $fullPath = PathOperations::join($context['baseDirectory'], $foundFile);
                mftpActionLog("Download file", $this->connection, dirname($fullPath), monstaBasename($fullPath), "");
            }

            $zipBuilder = new ZipBuilder($this->connection, $context['baseDirectory']);
            $zipPath = $zipBuilder->buildZip($foundFiles);

            $this->disconnect();
            return $zipPath;
        }

        public function createZip($context) {
            $this->connectAndAuthenticate();
            $fileFinder = new RecursiveFileFinder($this->connection, $context['baseDirectory']);
            $foundFiles = $fileFinder->findFilesInPaths($context['items']);

            foreach ($foundFiles as $foundFile) {
                $fullPath = PathOperations::join($context['baseDirectory'], $foundFile);
                mftpActionLog("Download file", $this->connection, dirname($fullPath), monstaBasename($fullPath), "");
            }

            $zipBuilder = new ZipBuilder($this->connection, $context['baseDirectory']);
            $destPath = PathOperations::join($context['baseDirectory'], $context['dest']);
            $zipPath = $zipBuilder->buildLocalZip($foundFiles, $destPath);

            $this->connection->uploadFile(new FTPTransferOperation($zipPath, $destPath, FTP_BINARY));

            $this->disconnect();
            return $zipPath;
        }

        public function uploadFile($context, $preserveRemotePermissions = false, $skipLog = false) {
            $this->connectAndAuthenticate();
            $transferOp = TransferOperationFactory::getTransferOperation($this->connectionType, $context);
            $this->connection->uploadFile($transferOp, $preserveRemotePermissions);
            if (!$skipLog) {
                // e.g. if editing a file don't log that it was also uploaded
                mftpActionLog("Upload file", $this->connection, dirname($transferOp->getRemotePath()), monstaBasename($transferOp->getRemotePath()), "");
            }
            $this->disconnect();
        }

        public function uploadFileToNewDirectory($context) {
            // This will first create the target directory if it doesn't exist and then upload to that directory
            $this->connectAndAuthenticate();
            $transferOp = TransferOperationFactory::getTransferOperation($this->connectionType, $context);
            $this->connection->uploadFileToNewDirectory($transferOp);
            mftpActionLog("Upload file", $this->connection, dirname($transferOp->getRemotePath()), monstaBasename($transferOp->getRemotePath()), "");
            $this->disconnect();
        }

        public function deleteFile($context) {
            $this->connectAndAuthenticate();
            $this->connection->deleteFile($context['remotePath']);
            mftpActionLog("Delete file", $this->connection, dirname($context['remotePath']), monstaBasename($context['remotePath']), "");
            $this->disconnect();
        }

        public function makeDirectory($context) {
            $this->connectAndAuthenticate();
            $this->connection->makeDirectory($context['remotePath']);
            $this->disconnect();
        }

        public function deleteDirectory($context) {
            $this->connectAndAuthenticate();
            $this->connection->deleteDirectory($context['remotePath']);
            $this->disconnect();
        }

        public function rename($context) {
            $this->connectAndAuthenticate();

            if(array_key_exists('action', $context) && $context['action'] == 'move') {
                $action = 'Move';
            } else {
                $action = 'Rename';
            }

            $itemType = $this->connection->isDirectory($context['source']) ? 'folder' : 'file';

            $this->connection->rename($context['source'], $context['destination']);

            if ($action == 'Move') {
                mftpActionLog($action . " " . $itemType, $this->connection, dirname($context['source']),
                monstaBasename($context['source']) . " to " . $context['destination'],
                "");
            }
            if ($action == 'Rename') {
                mftpActionLog($action . " " . $itemType, $this->connection, dirname($context['source']),
                monstaBasename($context['source']) . " to " . monstaBasename($context['destination']),
                "");
            }

            $this->disconnect();
        }

        public function changePermissions($context) {
            $this->connectAndAuthenticate();

            $itemType = $this->connection->isDirectory($context['remotePath']) ? 'folder' : 'file';

            $this->connection->changePermissions($context['mode'], $context['remotePath']);

            mftpActionLog("CHMOD " . $itemType, $this->connection, dirname($context['remotePath']),
                monstaBasename($context['remotePath']) . " to " . decoct($context['mode']), "");

            $this->disconnect();
        }

        public function copy($context) {
            $this->connectAndAuthenticate();
            $this->connection->copy($context['source'], $context['destination']);
            $this->disconnect();
        }

        public function testConnectAndAuthenticate($context, $isInitalLogin = true) {
            $initialDirectory = $this->connectAndAuthenticate($isInitalLogin);
            $serverCapabilities = array("initialDirectory" => $initialDirectory);

            if (isset($context['getServerCapabilities']) && $context['getServerCapabilities']) {
                $serverCapabilities["changePermissions"] = $this->connection->supportsPermissionChange();
            }

            clearOldTransfers();

            return array("serverCapabilities" => $serverCapabilities);
        }

        public function checkSavedAuthExists() {
            if ($this->readLicense() == null)
                return false;

            return AuthenticationStorage::configurationExists(AUTHENTICATION_FILE_PATH);
        }

        public function writeSavedAuth($context) {
            if ($this->readLicense() == null)
                return;

            AuthenticationStorage::saveConfiguration(AUTHENTICATION_FILE_PATH, $context['password'],
                $context['authData']);
        }

        public function readSavedAuth($context) {
            if ($this->readLicense() == null)
                return array();

            return AuthenticationStorage::loadConfiguration(AUTHENTICATION_FILE_PATH, $context['password']);
        }

        public function readLicense() {
            $keyPairSuite = new KeyPairSuite(PUBKEY_PATH);
            $licenseReader = new LicenseReader($keyPairSuite);
            $license = $licenseReader->readLicense(MONSTA_LICENSE_PATH);

            if (is_null($license))
                return $license;

            $publicLicenseKeys = array("expiryDate", "version", "isTrial", "licenseVersion", "productEdition");
            $publicLicense = array();
            foreach ($publicLicenseKeys as $publicLicenseKey) {
                if (isset($license[$publicLicenseKey]))
                    $publicLicense[$publicLicenseKey] = $license[$publicLicenseKey];
            }

            return $publicLicense;
        }

        private function recordAffiliateSource($licenseEmail) {
            $affiliateChecker = new AffiliateChecker();
            $installUrl = getMonstaInstallUrl();
            $affiliateId = defined("MFTP_AFFILIATE_ID") ? MFTP_AFFILIATE_ID : "";
            return $affiliateChecker->recordAffiliateSource($affiliateId, $licenseEmail, $installUrl);
        }

        public function getSystemVars() {
            $systemVars = SystemVars::getSystemVarsArray();

            $applicationSettings = new ApplicationSettings(APPLICATION_SETTINGS_PATH);

            $systemVars['applicationSettings'] = $applicationSettings->getSettingsArray();
            return $systemVars;
        }

        public function setApplicationSettings($context) {
            $applicationSettings = new ApplicationSettings(APPLICATION_SETTINGS_PATH);
            $applicationSettings->setFromArray($context['applicationSettings']);
            $applicationSettings->save();
        }





        public function validateSavedAuthPassword($context) {
            return AuthenticationStorage::validateAuthenticationPassword(AUTHENTICATION_FILE_PATH, $context["password"]);
        }


    }

Я не настолько опытен в этой области, я новичок и получил эту задачу, чтобы выяснить причину задержки и решить Это . Пожалуйста, помогите мне в этом отношении, я перепробовал много вещей, так как думал, что причиной может быть json_encoding (response), но в ответ возвращается только 10 элементов

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...