Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

fix(TaskProcessing): detect mimetype from buffer instead of wrapped stream - #66870

Open
MrMegaNova wants to merge 2 commits into
nextcloud:masterfrom
MrMegaNova:fix/taskprocessing-mime-stream-cast
Open

MrMegaNova wants to merge 2 commits into
nextcloud:masterfrom
MrMegaNova:fix/taskprocessing-mime-stream-cast

Conversation

@MrMegaNova

@MrMegaNova MrMegaNova commented Nov 20, 2026 •

Copy link
Copy Markdown

Summary

getFileContentsInternal() calls mime_content_type($node->fopen('rb')). When file locking is enabled (the default), View::basicOperation() wraps the stream in Icewind\Streams\CallbackWrapper to release the shared lock on close. mime_content_type() needs to cast the stream, the user-space wrapper does not implement stream_cast, so every task file download logs:

mime_content_type(): Icewind\Streams\CallbackWrapper::stream_cast is not implemented! at core/Controller/TaskProcessingApiController.php#575

The handle opened for the detection was also never closed, so the shared lock was only released at the end of the request.

This reads the first 8 KiB and sniffs them with IMimeTypeDetector::detectString() (finfo on a buffer), then closes the handle. The rest is unchanged: getSecureMimeType(), fallback to $node->getMimeType() when the result is application/octet-stream. Unlike #54673, the detection is still done on the content, so files without an extension keep their detected type.

Testing

On nextcloud:apache 35.0.3 (the function is identical on master), SQLite, testing app enabled for the fake providers. Tasks scheduled through POST /ocs/v2.php/taskprocessing/schedule, files fetched through GET /ocs/v2.php/taskprocessing/tasks/{id}/file/{fileId}:

File Task type Content-Type before Content-Type after Body
sample.wav core:audio2text audio/wav audio/wav identical
sample_noext (WAV, no extension) core:audio2text application/octet-stream application/octet-stream identical
empty.wav (0 bytes) core:audio2text audio/wav audio/wav identical
img_noext (PNG, no extension) core:image2text:ocr image/png image/png identical

stream_cast warnings in nextcloud.log: 4 per run before, 0 after (over 4 runs).

php-cs-fixer with nextcloud/coding-standard: no changes.

Unit tests added in tests/Core/Controller/TaskProcessingApiControllerTest.php: the file is served through a CallbackWrapper, as with file locking enabled. They cover detection from the first bytes, the fallback to the node mimetype (undetected type or empty file) and the closing of the detection handle. They pass with the fix and fail on the previous code.

TODO

  • Unit tests

Checklist

AI (if applicable)

  • The content of this PR was partly or fully generated using AI

Claude Code (claude-opus-5-5) helped investigate the issue, write the fix and the unit tests, and draft this description. I reviewed the changes and tested them on a live instance as described above. Each commit carries an Assisted-by trailer.

@MrMegaNova
MrMegaNova requested a review from a team as a code owner November 20, 2026 17:28
@MrMegaNova
MrMegaNova requested review from icewind1991 and leftybournes and removed request for a team November 20, 2026 17:28
@MrMegaNova

MrMegaNova commented Nov 30, 2026 •

Copy link
Copy Markdown
Author

Hi! Gentle ping on this one 🙂 The CI workflows are waiting for maintainer approval, so nothing has run yet.

@julien-nc since you looked into #54669 and suggested detecting from the content in #54673, would you mind having a look? This sniffs the first 8 KiB with detectString() instead of writing a temp file, and closes the handle so the shared lock is released right away.


Assisted by ClaudeCode:claude-opus-5-5

…tream

The stream returned by File::fopen() is wrapped in a user-space
CallbackWrapper when file locking is enabled. mime_content_type() cannot
cast such a stream and logs "CallbackWrapper::stream_cast is not
implemented!" on every task file download. The handle opened for the
detection was also never closed.

Read the first bytes instead and sniff them with
IMimeTypeDetector::detectString(), then close the handle.

Fixes nextcloud#54669

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Leo <leo.crotet@shellby.io>
getFileContentsInternal() had no test coverage. The new tests serve the
file through a CallbackWrapper, as View does when file locking is
enabled, so they fail with the previous mime_content_type() call.

They check that the type is sniffed from the first 8 KiB, that the node
mimetype is kept when detection gives application/octet-stream or the
file is empty, and that the handle opened for detection is closed.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Leo <leo.crotet@shellby.io>

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

[Bug]: mime_content_type(): Icewind\\Streams\\CallbackWrapper::stream_cast is not implemented

1 participant