Posted by JacobSingh on January 15, 2010 at 3:55pm
| Project: | Drupal core |
| Version: | 8.x-dev |
| Component: | file system |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | needs review |
| Issue tags: | Media Initiative |
Issue Summary
This is a trivial addition which would help reduce the bloated code to convert /myfile.jpg to
$file->uri = public://myfile.jpg
$file->size = 12345
$file->filemime = 'image/jpeg'
So that it can be run through file_save. This will help massively for any file import operation, and should also replace similar code in a ton of functions like file_save_upload, file_copy, file_scan_directory, etc...
For now, just adding this function is a win for profiles and contrib if nothing else.
Comments
#1
#2
Fixed the timestamp (didn't realize we don't use built in timestamp type in MySQL).
#3
Looks good but I think we really need an additional API function that will take a URI and return the existing file object, or save as a new one otherwise.
#4
yes, otherwise, sending it an existing youtube url (for instance) will fail. as will uploading a file that already exists (is there an md5 check somewhere in the system?)
#5
ah, scratch that re the md5; it's the filename doing that, i think...
#6
pretty sure that this will choke on identical filenames as well.
#7
I see a couple ways out here:
1). I'm not sure the uri should be unique in file. If it is, why do we even have an fid? I guess because during in import op we might move all the files. Anyway, this is a pet peeve from media, but a different topic.
2). If it stays unique, what is good error handling?
IMO, file_save should throw an exception it shouldn't let it go all the way to the PDO level and get some random DB error back. If we can bubble up from an key violation and catch it and then make sense of it fine, but that's a better way. I know Exceptions are in Exception D8, but I see no reason to even use PHP's built in Exception class, define a couple CONSTANTS for FILE_ERROR_DUPLICATE_URI, etc and let it rip.
this function simply preps a uri to be inserted. So I don't think the duplicate uri thing is its problem, that should be thrown at the time of insert, but maybe I'm missing the point.
-J
#8
No more new api's in D7, so bumping to D8.
#9
this addresses some of the concerns in #3 and 4:
this will load a file object if the uri exists in the db. otherwise, it creates a new object, and sets a ->is_new flag on the new object. doesn't actually save it, as that rightfully already exists in file_save().
#10
for reference, the media module is also using this function (and struggling with the issues here); see #1023254: Load a media object from a given URI.
#11
Subscribe.
#12
Major missing functionality. For those needing this in D7, the media module includes this function.
#13
Subscribe.
#14
the file_styles module has now also implemented it's own version of this : file_styles_uri_to_object
#15
+++ includes/file.inc 13 Jan 2011 16:24:47 -0000@@ -2057,6 +2057,41 @@ function file_get_mimetype($uri, $mappin
+ $query = db_select('file_managed', 'f')
+ ->fields('f', array('fid'))
+ ->condition('uri', $uri)
+ ->execute()
+ ->fetchCol();
+ if (!empty($query)) {
+ $file = file_load(array_shift($query));
+ }
Could be condensed just using $files = entity_load('file', FALSE, array('uri' => $uri)); $file = !empty($files) ? reset($files) : FALSE;
Should also run $uri = file_stream_wrapper_uri_normalize($uri) prior to searching for an existing file.
+++ includes/file.inc 13 Jan 2011 16:24:47 -0000@@ -2057,6 +2057,41 @@ function file_get_mimetype($uri, $mappin
+ $wrapper = file_stream_wrapper_get_instance_by_uri($uri);
$wrapper variable is not used. Remove this line.
+++ includes/file.inc 13 Jan 2011 16:24:47 -0000@@ -2057,6 +2057,41 @@ function file_get_mimetype($uri, $mappin
+ $file->is_new = TRUE;
This is unnecessary as checking if empty($file->fid) is a new file.
19 days to next Drupal core point release.
#16
Subscribe.
#17
Rerolled #9 with fixes suggested by Dave Reid in #15.
#18
The patch uses
$file = new StdClass;I don't know if we have a documented standard for this, but everywhere else in core we use
$file = new stdClass();#19
Good eye. Rerolled for consistency.
#20
function file_uri_to_object($uri)
code below:
codigo abaixo:
===
pt-BR
PS: na linha 51 add } eu adicioneu essa tag aqui = }
rodei a atualização o erro saiu, isso esta correto?
--//--
pt-EN
PS: at line 51 add} tag I added this here =}
I ran the update the error came out, this is correct?
===
will know much about programming, so I'm taking the entirely the code that is running on my work project completion of course (CBT).
analysts so that you can take a look I just made a change and it worked, at least out of the error tela.espero not be wrong. ORIGINAL en, sorry my English.
ORIGINAL aqui-
não entendo muito de programação, então estou pegando o codigo enteiro que está rodando no meu projeto de trabalho de conclusão de curso (TCC) .
para que vocês analistas possam dar uma olhada eu fiz apenas uma modificação e funcionou, pelo menos o erro saiu da tela.espero não estar errado. ORIGINAL pt-BR , sorry my english :-( .
( - is equals / , ok )
code below:Atatach
#21
Marked #367121: Wrapper function to turn static file into a file object as a duplicate of this issue even though it was older, this had more work on it.