Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catalog Image Upload Issue #24

Closed
jtwiest opened this issue Sep 2, 2020 · 6 comments
Closed

Catalog Image Upload Issue #24

jtwiest opened this issue Sep 2, 2020 · 6 comments

Comments

@jtwiest
Copy link

jtwiest commented Sep 2, 2020

I'm attempting to upload an image from an s3 bucket to a catalog image. Following the docs here's what I have:

$imageData = new CatalogImage();
$imageData->setCaption('Some image name');

$imageObject = new CatalogObject(CatalogObjectType::IMAGE, '#TESTIMAGE');
$imageObject->setImageData($imageData);

// Update catalog object in the api
$requestId = (string) Str::uuid();
$imageUploadBody = new CreateCatalogImageRequest($requestId, $imageObject);
$imageUploadBody->setObjectId($externalId); // $externalId is a valid CatalogItem Id
$imageUploadBody->setImage($imageObject);

// Save local copy of image (in prod this is coming from my s3 bucket)
$filePath = sys_get_temp_dir() . '/filename.jpeg';
$fileUrl = 'https://www.aworldofdifference.co.nz/pics/45,3,1,6,3,0/20151002142909/test-jpg.jpeg';
file_put_contents($filePath, file_get_contents($fileUrl));

// Configure object url
$imageFile = FileWrapper::createFromPath($filePath);
$apiResponse = $this->catalogApi->createCatalogImage($imageUploadBody, $imageFile);

However I'm getting the following error from square after I do the upload even though the file type of the provided file is a .jpeg (ie a supported type).

array:1 [
  0 => Square\Models\Error {#1613 
    -category: "INVALID_REQUEST_ERROR"
    -code: "INVALID_CONTENT_TYPE"
    -detail: "Only [image/jpeg image/pjpeg image/png image/x-png image/gif] content type allowed. but got application/octet-stream"
    -field: null
  }
]

In an ideal world I'd like to just supply the public image url from my bucket to the catalog uploaded but under the hood it looks like FileWrapper::createFromPath() is using CurlFile which my understanding is does not support remote file upload. https://github.com/square/square-php-sdk/blob/master/src/Utils/FileWrapper.php#L63

I've been hacking at this for quite some time now without a good solution to show for it so any advice would be much appreciated. Thanks!

@StephenJosey
Copy link

As far as I understand, FileWrapper requires a local image/file. It sounds like you could use your own (outside of Square) request to download the file locally, and then use FileWrapper to attach it, but that would be outside of Square.

@jtwiest
Copy link
Author

jtwiest commented Sep 2, 2020

Thanks for the response @StephenJosey. I tried that solution in the code snippit above however.

// Save local copy of image (in prod this is coming from my s3 bucket)
$filePath = sys_get_temp_dir() . '/filename.jpeg';
$fileUrl = 'https://www.aworldofdifference.co.nz/pics/45,3,1,6,3,0/20151002142909/test-jpg.jpeg';
file_put_contents($filePath, file_get_contents($fileUrl));

I'm downloading the image from the url and putting it into my /tmp directory saving it's final path as $filePath. The $filePath variable is then what I'm providing as the input to FileWrapper::createFromPath($filePath) but my problem still persists.

@StephenJosey
Copy link

Ah, sorry, I totally missed that ha. What's your app id? I can see if there's anything obvious going on in our logs.

@jtwiest
Copy link
Author

jtwiest commented Sep 2, 2020

Ah thanks - I believe my app id is sq0idp-sELSl3rZR8ZFMJM02n_Kfg I pulled that from https://squareupsandbox.com/dashboard/apps/my-applications

Everything above is being done on the sandbox server as well. Not sure if there is anything different about that environment either.

Thanks!

@StephenJosey
Copy link

Actually, I think maybe it's because you're not passing in a mimeType (which I can see is optional). According to PHP docs, the default is "application/octet-stream": https://www.php.net/manual/en/class.curlfile.php. Could you try:

$imageFile = FileWrapper::createFromPath($filePath, "image/jpeg");

instead? I have not tested this so apologies if this doesn't help.

@jtwiest
Copy link
Author

jtwiest commented Sep 2, 2020

@StephenJosey you're my hero, that worked! I assumed it would have picked the mimeType up automatically and it was my image that was the problem.

It might be a good idea to make that field required in a future version since the default value is not a supported type of the api.

Really appreciate the support.

@jtwiest jtwiest closed this as completed Sep 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants