如何获取使用fileCreate上传的文件的URL?

huangapple go评论78阅读模式
英文:

How to get the url of a file uploaded using fileCreate?

问题

已按照文档中所示的示例进行了mutator的跟随。

这只返回文件上传时间(createdAt time)。如何获取图像的CDN URL?

英文:

Have followed the example mutator as shown in the documentation

    $query = <<<QUERY
  mutation fileCreate($files: [FileCreateInput!]!) {
    fileCreate(files: $files) {
      files {
        alt
        createdAt
      }
      userErrors {
        field
        message
      }
    }
  }
QUERY;

This is just returning the createdAt time that the file was uploaded. How do I get the cdn url of the image?

答案1

得分: 1

这需要一个两步的方法。首先是修改原始突变以检索从Shopify生成的文件ID:

$query = <<<QUERY
  mutation fileCreate($files: [FileCreateInput!]!) {
    fileCreate(files: $files) {
      files {
        alt
        createdAt
        ... on GenericFile {
          id
        }
        ... on MediaImage {
          id
        }
        ... on Video {
          id
        }
      }
      userErrors {
        field
        message
      }
    }
  }
QUERY;

这将返回一个类似于gid://shopify/MediaImage/123456789012345的ID。

文件上传后,我们需要使用我们的ID调用一个files查询:

$getFileQuery = <<<QUERY
query {
    files(first: 1, query: &quot;id:{{ id }}&quot;) {
      edges {
        node {
          fileStatus
          ... on MediaImage {
            image {
              url
            }
          }
        }
      }
    }
}
QUERY;

当您上传文件时,Shopify 必须对其进行处理,因此状态为 "PROCESSING",直到处理完成,您才能获得 URL。因此,我将查询函数包装在一个 do/while 循环中:

//Ping shopify for the image. Make sure it has finished being processed.
$process = false;
do {
    //Find my file by id
    $response = $api-&gt;query([&quot;query&quot; =&gt; str_replace(&#39;{{ id }}&#39;, str_replace(&#39;gid://shopify/MediaImage/&#39;, &#39;&#39;, $data[&#39;data&#39;][&#39;fileCreate&#39;][&#39;files&#39;][0][&#39;id&#39;]), $getFileQuery)]);
    $data = $response-&gt;getDecodedBody();

    //File still processing. Wait a moment and try again.
    if (
        !array_key_exists(0, $data[&#39;data&#39;][&#39;files&#39;][&#39;edges&#39;]) || 
        $data[&#39;data&#39;][&#39;files&#39;][&#39;edges&#39;][0][&#39;node&#39;][&#39;fileStatus&#39;] != &#39;READY&#39;
    ) {
        sleep(1);
        continue;
    }

    //Found our file. Extract the image url
    $newFileName = $data[&#39;data&#39;][&#39;files&#39;][&#39;edges&#39;][0][&#39;node&#39;][&#39;image&#39;][&#39;url&#39;];
    $process = true; //Break the loop.
} while (!$process);
//Use $newFileName to take over the world.
英文:

This requires a two-step approach. The first is to amend the original mutation to retrieve the generated file id from shopify:

    $query = &lt;&lt;&lt;QUERY
  mutation fileCreate($files: [FileCreateInput!]!) {
    fileCreate(files: $files) {
      files {
        alt
        createdAt
        ... on GenericFile {
          id
        }
        ... on MediaImage {
          id
        }
        ... on Video {
          id
        }
      }
      userErrors {
        field
        message
      }
    }
  }
QUERY;

This will return an id similar to gid://shopify/MediaImage/123456789012345

Once the file has been uploaded we need to call a files query with our ID:

    $getFileQuery = &lt;&lt;&lt;QUERY
query {
    files(first: 1, query: &quot;id:{{ id }}&quot;) {
      edges {
        node {
          fileStatus
          ... on MediaImage {
            image {
              url
            }
          }
        }
      }
    }
}
QUERY;

When you upload a file Shopify has to process it so the status is "PROCESSING" and you won't get the url out until it's finished. So I wrap the query function in a do/while loop:

//Ping shopify for the image. Make sure it has finished being processed.
$process = false;
do {
    //Find my file by id
    $response = $api-&gt;query([&quot;query&quot; =&gt; str_replace(&#39;{{ id }}&#39;, str_replace(&#39;gid://shopify/MediaImage/&#39;, &#39;&#39;, $data[&#39;data&#39;][&#39;fileCreate&#39;][&#39;files&#39;][0][&#39;id&#39;]), $getFileQuery)]);
    $data = $response-&gt;getDecodedBody();

    //File still processing. Wait a moment and try again.
    if (
        !array_key_exists(0, $data[&#39;data&#39;][&#39;files&#39;][&#39;edges&#39;]) || 
        $data[&#39;data&#39;][&#39;files&#39;][&#39;edges&#39;][0][&#39;node&#39;][&#39;fileStatus&#39;] != &#39;READY&#39;
    ) {
        sleep(1);
        continue;
    }

    //Found our file. Extract the image url
    $newFileName = $data[&#39;data&#39;][&#39;files&#39;][&#39;edges&#39;][0][&#39;node&#39;][&#39;image&#39;][&#39;url&#39;];
    $process = true; //Break the loop.
} while (!$process);
//Use $newFileName to take over the world.

Once Shopify has finished processing the file it sets the status as "READY" so you can grab the file url.

huangapple
  • 本文由 发表于 2023年6月15日 19:28:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481998.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定