Page MenuHomePhorge

PHP 8.2 "strlen(null)" exception when executing Conduit file.allocate
Closed, ResolvedPublic

Description

When you edit the phorge_url and conduit_api variables below and execute the code below, you get a Conduit error: strlen(): Passing null to parameter #1 ($string) of type string is deprecated

export phorge_url=http://phorge.local
export conduit_api=api-abcdef1g2hijk45lmnop6qrs7tu8
python3 file-upload.py $phorge_url $conduit_api
file-upload.py
import base64
import json
import os
import requests
import sys
from PIL import Image
import numpy as np
from PIL import Image

phorge_url = sys.argv[1]
api_token = sys.argv[2]
file_path = "/tmp/some-image.png"
image_width = 3500
image_height = 3500

def call_conduit(method, params):
    params['__conduit__'] = {'token': api_token}
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    data = 'params={}&output=json'.format(json.dumps(params))
    response = requests.post(f'{phorge_url}/api/{method}', data=data, headers=headers)
    print(f'### {method}: {response.text}')
    return json.loads(response.text)

# Generate a random colored image
image_data = np.random.randint(0, 256, (image_height, image_width, 3), dtype=np.uint8)
image = Image.fromarray(image_data)
image.save(file_path, format="PNG", compress_level=0)

# Read the file data
with open(file_path, "rb") as f:
    file_data = f.read()

# Allocate a new file using file.allocate
allocate_params = {
    "name": "some-image.png",
    "contentLength": len(file_data),
    "contentHash": None,
    "viewPolicy": "public"
}
allocate_result = call_conduit('file.allocate', allocate_params)
file_phid = allocate_result['result']['filePHID']

if file_phid is not None:
    # Query the chunks of the file using file.querychunks
    query_chunks_params = {
        "filePHID": file_phid
    }
    query_chunks_result = call_conduit('file.querychunks', query_chunks_params)

    # Upload the file data using file.uploadchunk
    for chunk in query_chunks_result['result']:
        byte_start = int(chunk['byteStart'])
        byte_end = int(chunk['byteEnd'])
        chunk_data = file_data[byte_start:byte_end]
        chunk_data_base64 = base64.b64encode(chunk_data).decode("utf-8")
        upload_chunk_params = {
            "filePHID": file_phid,
            "byteStart": byte_start,
            "dataEncoding": "base64",
            "data": chunk_data_base64
        }
        upload_chunk_result = call_conduit('file.uploadchunk', upload_chunk_params)

    search_params = {
            "constraints": { 
                "phids": [
                    file_phid
                ] 
            }
    }
    search_result = call_conduit('file.search', search_params);

    print(f"File uploaded successfully: {phorge_url}/F{search_result['result']['data'][0]['id']}")
else:
    print("This is not a large file.")

# Delete the generated image file
os.remove(file_path)

Stacktrace:

[Sun Jun 25 07:53:08.251193 2023] [php:notice] [::1:49864] [2023-06-25 05:53:08] EXCEPTION: (RuntimeException) strlen(): Passing null to parameter #1 ($string) of type string is deprecated at [<arcanist>/src/error/PhutilErrorHandler.php:261]
[Sun Jun 25 07:53:08.251552 2023] [php:notice] [::1:49864] arcanist(head=master, ref.master=8130241a11ac), diagram(), example(), phorge(head=master, ref.master=a25af8ccef69, custom=1)
[Sun Jun 25 07:53:08.251560 2023] [php:notice] [::1:49864]   #0 <#2> PhutilErrorHandler::handleError(integer, string, string, integer) called at [<phorge>/src/applications/files/storage/PhabricatorFile.php:291]
[Sun Jun 25 07:53:08.251563 2023] [php:notice] [::1:49864]   #1 <#2> PhabricatorFile::newChunkedFile(PhabricatorChunkedFileStorageEngine, integer, array) called at [<phorge>/src/applications/files/engine/PhabricatorChunkedFileStorageEngine.php:114]
[Sun Jun 25 07:53:08.251566 2023] [php:notice] [::1:49864]   #2 <#2> PhabricatorChunkedFileStorageEngine::allocateChunks(integer, array) called at [<phorge>/src/applications/files/conduit/FileAllocateConduitAPIMethod.php:116]
[Sun Jun 25 07:53:08.251568 2023] [php:notice] [::1:49864]   #3 <#2> FileAllocateConduitAPIMethod::execute(ConduitAPIRequest) called at [<phorge>/src/applications/conduit/method/ConduitAPIMethod.php:156]
[Sun Jun 25 07:53:08.251571 2023] [php:notice] [::1:49864]   #4 <#2> ConduitAPIMethod::executeMethod(ConduitAPIRequest) called at [<phorge>/src/applications/conduit/call/ConduitCall.php:131]
[Sun Jun 25 07:53:08.251573 2023] [php:notice] [::1:49864]   #5 <#2> ConduitCall::executeMethod() called at [<phorge>/src/applications/conduit/call/ConduitCall.php:81]
[Sun Jun 25 07:53:08.251575 2023] [php:notice] [::1:49864]   #6 <#2> ConduitCall::execute() called at [<phorge>/src/applications/conduit/controller/PhabricatorConduitAPIController.php:83]
[Sun Jun 25 07:53:08.251578 2023] [php:notice] [::1:49864]   #7 phlog(RuntimeException) called at [<phorge>/src/applications/conduit/controller/PhabricatorConduitAPIController.php:111]
[Sun Jun 25 07:53:08.251580 2023] [php:notice] [::1:49864]   #8 PhabricatorConduitAPIController::handleRequest(AphrontRequest) called at [<phorge>/src/aphront/configuration/AphrontApplicationConfiguration.php:284]
[Sun Jun 25 07:53:08.251582 2023] [php:notice] [::1:49864]   #9 AphrontApplicationConfiguration::processRequest(AphrontRequest, PhutilDeferredLog, AphrontPHPHTTPSink, MultimeterControl) called at [<phorge>/src/aphront/configuration/AphrontApplicationConfiguration.php:203]
[Sun Jun 25 07:53:08.251585 2023] [php:notice] [::1:49864]   #10 AphrontApplicationConfiguration::runHTTPRequest(AphrontPHPHTTPSink) called at [<phorge>/webroot/index.php:35]

Related Objects

View Standalone Graph
This task is connected to more than 200 other tasks. Only direct parents and subtasks are shown here. Use View Standalone Graph to show more of the graph.