I'm trying to upload an image from a WordPress backend to a GitLab project, such that I can use that image in an issue. The image comes from a ContactForm7 form and I store it in WordPress first.
GitLab has a specific API for uploading files use in issues, but no matter what I sent, it returns {"error":"file is invalid"}. Another Stackoverlow issue to a similar question exists, but the solution is incomplete and/or does not work. I don't want to commit an image, I'm fine with it being in this weird upload architecture of GitLab.
I tried using just the filename in the body, the internal path, the public URL to the image and a base64 encoded version. All tries lead to the same invalid file error. I can't find any documentation on how GitLab expects my image and what I need to do for GitLab to accept it. Any ideas?
This is my code:
function uploadToGitLab($contact_form) {
$contact_form_id = $contact_form->id();
$submission = WPCF7_Submission::get_instance();
$data = $submission->get_posted_data();
$files = $submission->uploaded_files();
$markdownImage = "";
if (!empty($files) && $contact_form_id == "721") {
// move image from temp folder to upload folder
$image_file = file_get_contents($files['anhang'][0]);
$image_name = basename($files['anhang'][0]);
$imageUpload = wp_upload_bits(basename($files['anhang'][0]), null, $image_file);
$image_filename = $imageUpload['url'];
// Convert Image to Base64
$image_type = pathinfo($imageUpload['file']);
$image_stringdata = file_get_contents($imageUpload['file']);
$image_base64 = base64_encode($image_stringdata);
$uploadImageHeader = array(
'Content-type' => 'multipart/form-data',
// Set encoding to base64 if trying to send base64 image:
'encoding' => 'base64'
);
$uploadImageBody = array(
// Using the raw file uploaded in the form:
//'file' => '@'. $files["anhang"][0]
// Using just the filename:
//'file' => '@'. basename($files["anhang"][0])
// Using the image public URL:
//'file' => '@'. $image_filename
// Using base64 encoding of the image:
'file' => '@' . $image_base64
);
$uploadArgs = array(
'header' => $uploadImageHeader,
'body' => $uploadImageBody
);
$imageResponse = wp_remote_post("https://example.com/api/v4/projects/MYPROJECTID/uploads?private_token=TOKEN", $uploadArgs);
$markdownImage = $imageResponse["markdown"];
}
As described above, no matter what I put into the 'file' => '@' . line, it seems to be invalid.
I tried:
- @image.png
- @/wordpress/uploads/path/to/image.png
- @https://example.com/image.png
- @base64encodedimage
But all return invalid. What is GitLab expecting?