Trying to create a stream wrapper class, at Media: YouTube. Making progress, except that when file_save() is called for the newly created file object, we receive a warning on $file->filesize = filesize($file->uri);
Warning: filesize(): stat failed for youtube://v/-jubiv7QUco in file_save() (line 482 of /var/www/d7-media/includes/file.inc).
It looks like filesize() doesn't work with streams. This is obviously inconsistent with other stream wrapper file functions, as it apparently can't be overridden, and doesn't simply return FALSE (as does file_exists() or is_readable()).
I believe we should move the $file->filesize assignment outside this function, perhaps even to be handled by the stream wrappers somehow. Otherwise, the File API will fail for handling remote streams.
| Comment | File | Size | Author |
|---|---|---|---|
| #22 | file.remote-file_save.628094.22.patch | 721 bytes | micnap |
| #21 | file.remote-file_save.628094.21.patch | 753 bytes | micnap |
| #13 | file.remote-file_save.628094.13.patch | 1.02 KB | aaron |
| #9 | file.remote-file_save.628094.9.patch | 906 bytes | aaron |
Comments
Comment #1
pwolanin commentedDo we implement http://www.php.net/manual/en/streamwrapper.stream-stat.php? It seems so - maybe make sure you are returning some size in the array returned by stream_stat() from your implementation
Comment #2
pwolanin commentedIf that doesn't work, perhaps we should call stat() instead of filesize()
Comment #3
aaron commentedNo, I've implemented stream_stat(), but it still fails. Let me try using stat() instead of filesize().
Comment #4
aaron commentedHmm.. That fails too. Perhaps I'm implementing stream_stat incorrectly?
(I just have a fake value for size currently, obviously.)
Comment #5
aaron commentedAnd my stat() implementation, for the record:
Comment #6
pwolanin commentedfrom php.net re: stream_stat()
NOT stat().
see: http://www.php.net/manual/en/function.fstat.php
Comment #7
aaron commented@pwolanin: Unfortunately, that page also reads
I tried it anyway, and now get:
What if we call the stream wrapper's ->stream_stat() instead?
Comment #8
aaron commentedoh wait, just saw that's expecting a handler. not sure if i can wrangle that, but i'll work on it this afternoon...
Comment #9
aaron commentedOK, this takes care of the issue. We grab the stream and call ->stream_stat directly. Not sure why PHP isn't doing that natively; probably a PHP bug. Oh well.
Comment #11
pwolanin commentedOdd indeed - how was this working even for local files like public://?
Quick look at the patch - looks reasonable.
Comment #12
aaron commentedlocal files worked because it was calling filesave() directly on the filename, rather than the stream.
this patch fails now for normal uploads:
. unfortunately, i guess we'll need to switch on the stream type (drupallocal vs others).
Comment #13
aaron commentedThis one works fine with local and remote streams now.
Comment #14
aaron commentedthis patch works by opening the stream before reading stream_stat, then closing it. i believe that filesave works the same way for local files, so this shouldn't add anything substantially different functionally. i'm still baffled by stat() not working on the remote stream; maybe dopry can clue us someday into why that's not working as i expected.
Comment #15
c960657 commentedI have been using filesize() together with stream wrappers without problems, e.g. the stream wrapper included in the Services_Amazon_S3 PEAR package. I even have tests for it. The stream_stat() implementation is here.
Your stream_stat() should return an array similar to that returned by stat(). On my system, that array has both numerical and string keys:
Perhaps this is what's causing you trouble?
Comment #16
aaron commentedhuh. well, i used that nearly verbatim, and it still didn't work. note that http://us3.php.net/manual/en/function.filesize.php writes:
in fact, i'm still unable to get stat() working with my stream... :(
Comment #17
aaron commentedOK, figured it out. I had to implement ->url_stat(). Really confusing. Not sure where ->stream_stat() is even used; i'd earlier interpreted its documentation to mean it was called for stat() functions.
Comment #18
c960657 commentedstream_stat() is used by fstat() (get info based on a filehandle) and url_stat() is used by stat() (get info based on a filename).
Comment #20
giupenni commentedWitct Drupal 7.22 problem persists:
https://drupal.org/node/1586260#comment-7898653
Comment #21
micnap commentedComment #22
micnap commentedLet's try this again without "docroot" in the path.
Comment #23
damienmckennaThese patches need to be moved to a new issue, or we need to have a maintainer reopen the issue.
Comment #24
szeidler commentedI don't think those patches are required. If the custom StreamWrapper is implementing the PHP stat() family properly, the retrieval of filesize will work out of the box and there is no need for the patch.