<commit_before><?php

/**
 * This is the implementation of the server side part of
 * Flow.js client script, which sends/uploads files
 * to a server in several chunks.
 *
 * The script receives the files in a standard way as if
 * the files were uploaded using standard HTML form (multipart).
 *
 * This PHP script stores all the chunks of a file in a temporary
 * directory (`temp`) with the extension `_part<#ChunkN>`. Once all
 * the parts have been uploaded, a final destination file is
 * being created from all the stored parts (appending one by one).
 *
 * @author Buster "Silver Eagle" Neece
 * @email <EMAIL>
 *
 * @author <NAME> (http://online-php.com)
 * @email <EMAIL>
 *
 * @editor <NAME> (http://www.bivekjoshi.com.np)
 * @email <EMAIL>
 */

namespace App\Service;

use App\Exception;
use App\Http\Response;
use App\Http\ServerRequest;
use App\Service\Flow\UploadedFile;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UploadedFileInterface;
use RuntimeException;

use const SCANDIR_SORT_NONE;

class Flow
{
    /**
     * Process the request and return a response if necessary, or the completed file details if successful.
     */
    public static function process(
        ServerRequest $request,
        Response $response,
        string $tempDir = null
    ): UploadedFile|ResponseInterface {
        if (null === $tempDir) {
            $tempDir = sys_get_temp_dir() . '/uploads';
        }

        $params = $request->getParams();

        $flowIdentifier = $params['flowIdentifier'] ?? '';
        $flowChunkNumber = (int)($params['flowChunkNumber'] ?? 1);

        $targetSize = (int)($params['flowTotalSize'] ?? 0);
        $targetChunks = (int)($params['flowTotalChunks'] ?? 1);

        // Handle a regular file upload that isn't using flow.
        if (1 === $targetChunks) {
            if ('GET' === $request->getMethod()) {
                return $response->withStatus(204, 'No Content');
            }

            return self::handleStandardUpload($request, $tempDir);
        }<commit_msg>Implement longer-term solution to flow uploads for tiny files.<commit_after><?php

/**
 * This is the implementation of the server side part of
 * Flow.js client script, which sends/uploads files
 * to a server in several chunks.
 *
 * The script receives the files in a standard way as if
 * the files were uploaded using standard HTML form (multipart).
 *
 * This PHP script stores all the chunks of a file in a temporary
 * directory (`temp`) with the extension `_part<#ChunkN>`. Once all
 * the parts have been uploaded, a final destination file is
 * being created from all the stored parts (appending one by one).
 *
 * @author Buster "<NAME>" Neece
 * @email <EMAIL>
 *
 * @author <NAME> (http://online-php.com)
 * @email <EMAIL>
 *
 * @editor <NAME> (http://www.bivekjoshi.com.np)
 * @email <EMAIL>
 */

namespace App\Service;

use App\Exception;
use App\Http\Response;
use App\Http\ServerRequest;
use App\Service\Flow\UploadedFile;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UploadedFileInterface;
use RuntimeException;

use const SCANDIR_SORT_NONE;

class Flow
{
    /**
     * Process the request and return a response if necessary, or the completed file details if successful.
     */
    public static function process(
        ServerRequest $request,
        Response $response,
        string $tempDir = null
    ): UploadedFile|ResponseInterface {
        if (null === $tempDir) {
            $tempDir = sys_get_temp_dir() . '/uploads';
        }

        $params = $request->getParams();

        // Handle a regular file upload that isn't using flow.
        if (empty($params['flowTotalChunks']) || empty($params['flowIdentifier'])) {
            // Prompt an upload if this is indeed a mistaken Flow request.
            if ('GET' === $request->getMethod()) {
                return $response->withStatus(204, 'No Content');
            }

            return self::handleStandardUpload($request, $tempDir);
        }

        $flowIdentifier = $params['flowIdentifier'] ?? '';
        $flowChunkNumber = (int)($params['flowChunkNumber'] ?? 1);

        $targetSize = (int)($params['flowTotalSize'] ?? 0);
        $targetChunks = (int)($params['flowTotalChunks'] ?? 1);
{'fraction_non_alphanumeric': 0.12229029217719133, 'fraction_numerical': 0.003063147973609802, 'mean_word_length': 3.0467111534795044, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5070403', 'n_tokens_mistral': 1225, 'n_tokens_neox': 1188, 'n_words': 402}