1

I'm struggling to upload images to the Drupal 8 site via REST. Finally, I managed to deliver the file to the server, but then the error 500 rased due destination folder not set. I can't figure where to define this destination directory: in request JSON data or header, or somewhere else in the settings.php or somewhere in REST configuration?

Here my code in Python:

def create_drupal_image(session, oauth, media_name, image_path):
    file_size = os.stat(image_path).st_size
    with open(image_path, "rb") as image_file:
        encoded_string = base64.b64encode(image_file.read())

    payload = {
        "filename": [
            {
                "value": media_name + ".jpg"
            }
        ],
        "filemime": [
            {
                "value": "image/jpeg"
            }
        ],
        "filesize": [
            {
                "value": str(file_size)
            }
        ],
        "type": [
            {
                "target_id": "image"
            }
        ],
        "data": [
            {
                "value": "data:image/jpeg;base64," + str(encoded_string)
            }
        ],
        "_links": {
            "type": {
                "href": oauth.base_url + REST_TYPE_FILE_IMAGE
            },
        }
    }
    return create_drupal_entity_impl(session, oauth, ENTITY_FILE_URL, payload)


def create_drupal_entity_impl(session, oauth, url, payload):
    payload_str = json.dumps(payload)
    # print(payload_str)
    endpoint = oauth.base_url + url
    # Set all required headers
    headers = {
        'Content-Type': 'application/hal+json',
        'Accept': 'application/hal+json',
        'Authorization': 'Bearer ' + oauth.access_token
    }
    # Post the new node (a Contact) to the endpoint.
    response = session.post(endpoint, data=payload_str, headers=headers, verify=oauth.verify)
    return handle_create_response(response)

Edited this source core/lib/Drupal/Core/File/FileSystem.php line 488 in order to print the $destination, and it appears to be empty. See '' in the server log below:

AH01071: Got error 'PHP message: Uncaught PHP Exception Drupal\\Core\\File\\Exception
\\DirectoryNotReadyException: "The specified file 'temporary://filemDIAwD' could not be copied 
because the destination '' directory is not properly configured. This may be caused by a problem 
with file or directory permissions." at /var/www/vhosts/web.site/core/lib/Drupal/Core/File
/FileSystem.php line 488'

Tried to define 'file_private_path' folder in settings.php, added 'tmp' folder under 'sites/default/files' but with no result. What am I missing here?

1 Answer 1

0

The missing property was destination "uri"

"uri": [
   {
      "value": "public://" + media_name + ".jpg"
   }
],

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.