CloudStorageTools :: createUploadUrl在上传时重定向到不正确的URL

时间:2015-03-17 03:28:45

标签: php wordpress google-app-engine file-upload

我在GAE上运行了wordpress多站点安装,一切都很好用。

我希望用户通过前端将文件上传到{customdomain.com} / app / client / {client_id} / upload_profile_image /(我正在使用重写规则)的自定义文件处理程序

这适用于非app-engine设置(apache / php)但是当我使用GCS上传器时,我似乎被重定向到主站点域的这个url:{maindomain} /wp-signup.php? new =

这是基于wp google-app-engine插件代码的代码:

    $url = site_url('/app/client/' . $Client->ID . '/upload_profile_image/');
    require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
    $options = [
         'gs_bucket_name' => get_option('appengine_uploads_bucket', ''),
         'url_expiry_time_seconds' => 60 * 60 * 24, // One day is the maximum
    ];
    $wp_maxupsize = wp_max_upload_size();
    // set max_bytes_per_blob option only if max upload size is a positive int
    if (is_int($wp_maxupsize) && $wp_maxupsize > 0) {
       $options['max_bytes_per_blob'] = $wp_maxupsize;
    }
    // Setup internal authentication
    $sign_result = \google\appengine\api\app_identity\AppIdentityService::signForApp(AUTH_KEY . get_current_user_id());
    $url = add_query_arg('gae_auth_user', get_current_user_id(), $url);
    $url = add_query_arg('gae_auth_key', $sign_result['key_name'], $url);
    $url = add_query_arg('gae_auth_signature', urlencode(base64_encode($sign_result['signature'])), $url);

     $url = \google\appengine\api\cloud_storage\CloudStorageTools::createUploadUrl($url,$options);

当我提交文件时(通过拖放使用dropzone),我看到以下请求:

Accept:application/json
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8,es;q=0.6
Cache-Control:no-cache
Connection:keep-alive
Content-Length:49190
Content-Type:multipart/form-data; boundary=----WebKitFormBoundarytVOWWgs3diiHzPNF
Cookie: {cookies}
Host:{customdomain}
Origin:http://{customdomain}
Pragma:no-cache
Referer:http://{customdomain}/app/client/27/view/
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36
X-Requested-With:XMLHttpRequest

回复是这样的:

Alternate-Protocol:80:quic,p=0.5,80:quic,p=0.5
Cache-Control:no-cache, must-revalidate
Content-Length:0
Content-Type:text/html
Date:Tue, 17 Mar 2015 03:07:53 GMT
Expires:Fri, 01 Jan 1990 00:00:00 GMT
Location:http://{maindomain}/wp-signup.php?new=
Pragma:no-cache
Server:UploadServer ("Built on Mar 6 2015 11:14:41 (1425669281)")
X-AppEngine-Estimated-CPM-US-Dollars:$0.000009
X-AppEngine-Resource-Usage:ms=174 cpu_ms=22

哪个上传失败,我在控制台中看到了这个: XMLHttpRequest无法加载http:// {maindomain} /wp-signup.php?new=。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许来源'http:// {customdomain}'访问。响应的HTTP状态代码为404.

这是我的app.yaml:

application: 
version: 1
runtime: php55
api_version: 1

default_expiration: "1h"

inbound_services:
  - warmup

handlers:
- url: /robots.txt
  static_files: robots.txt
  upload: robots.txt

- url: /favicon.ico
  static_files: wordpress/favicon.ico
  upload: wordpress/favicon.ico

- url: /(.*\.(htm|html|css|js))$
  static_files: wordpress/\1
  upload: wordpress/.*\.(htm|html|css|js)$
  application_readable: true

- url: /wp-content/(.*\.(ico|jpg|png|gif|svg|ttf|eot|woff|swf))$
  static_files: wordpress/wp-content/\1
  upload: wordpress/wp-content/.*\.(ico|jpg|png|gif|svg|ttf|eot|woff|swf)$
  application_readable: true

- url: /(.*\.(ico|jpg|png|gif))$
  static_files: wordpress/\1
  upload: wordpress/.*\.(ico|jpg|png|gif)$

- url: /wp-admin/(.+)
  script: wordpress/wp-admin/\1

- url: /wp-admin/
  script: wordpress/wp-admin/index.php

- url: /wp-login.php
  script: wordpress/wp-login.php

- url: /wp-cron.php
  script: wordpress/wp-cron.php

- url: /wp-multi-cron.php
  script: wordpress/wp-multi-cron.php

- url: /xmlrpc.php
  script: wordpress/xmlrpc.php

- url: /wp-(.+).php
  script: wordpress/wp-\1.php

- url: /(.+)?/?
  script: wordpress/index.php

- url: /wp-admin/network/
  script: wordpress/wp-admin/network/index.php

- url: /wp-admin/network/(.+)
  script: wordpress/wp-admin/network/\1

- url: /([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*\.(htm.*$|html.*$|css.*$|js.*$|ico.*$|jpg.*$|png.*$|gif.*$|woff.*$|ttf.*$|eot.*$|svg.*$))
  static_files: wordpress/\2
  upload: wordpress/([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*\.(htm.*$|html.*$|css.*$|js.*$|ico.*$|jpg.*$|png.*$|gif.*$|woff.*$|ttf.*$|eot.*$|svg.*$))
  application_readable: true


pagespeed:
  domains_to_rewrite:
  url_blacklist:
  enabled_rewriters:
  - LeftTrimUrls
  disabled_rewriters:
  - CombineCss
  - CombineJs
  - MoveCssToHead

skip_files:
- wordpress/wp-content/uploads/.*
- ^(.*/)?.*/.svn/.*$

我对可能出现的问题感到茫然。通过WP后端定期上传工作正常,所以它必须是我做错了。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:2)

我找到了问题的解决方案,因为我怀疑这是我做错了。

为简洁起见,我省略了一些if语句,这些语句导致$ url为空,然后传递给' CreateUploadUrl'这就是上传后它被重定向到默认网站的原因。

当你在漫长的一天之后深夜编码时会发生这种情况。