Closed (won't fix)
Project:
Drupal core
Version:
8.9.x-dev
Component:
file system
Priority:
Normal
Category:
Feature request
Assigned:
Issue tags:
Reporter:
Created:
15 Jan 2010 at 15:55 UTC
Updated:
23 Aug 2021 at 08:31 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #1
JacobSingh commentedComment #2
JacobSingh commentedFixed the timestamp (didn't realize we don't use built in timestamp type in MySQL).
Comment #3
pwolanin commentedLooks 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.
Comment #4
aaron commentedyes, 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?)
Comment #5
aaron commentedah, scratch that re the md5; it's the filename doing that, i think...
Comment #6
aaron commentedpretty sure that this will choke on identical filenames as well.
Comment #7
JacobSingh commentedI 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
Comment #8
ff1 commentedNo more new api's in D7, so bumping to D8.
Comment #9
aaron commentedthis 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().
Comment #10
aaron commentedfor 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.
Comment #11
Niklas Fiekas commentedSubscribe.
Comment #12
yareckon commentedMajor missing functionality. For those needing this in D7, the media module includes this function.
Comment #13
geerlingguy commentedSubscribe.
Comment #14
yareckon commentedthe file_styles module has now also implemented it's own version of this : file_styles_uri_to_object
Comment #15
dave reidCould 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.
$wrapper variable is not used. Remove this line.
This is unnecessary as checking if empty($file->fid) is a new file.
19 days to next Drupal core point release.
Comment #16
lolbroek commentedSubscribe.
Comment #17
brianV commentedRerolled #9 with fixes suggested by Dave Reid in #15.
Comment #18
tr commentedThe 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();Comment #19
brianV commentedGood eye. Rerolled for consistency.
Comment #20
GuilhermePuentes commentedfunction 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
Comment #21
dave reidMarked #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.
Comment #22
JeremyFrench commented#19: 685818-file-uri-to-object-rev2.patch queued for re-testing.
There has been no movement on this for a while. Before doing looking at closer thought a re test was in order.
Comment #24
JeremyFrench commentedThis is just a reroll of the patch from #19
Comment #25
JeremyFrench commentedComment #26
JeremyFrench commentedI have tweaked the function a little and changed the core file functions to use it. With the exception of
file_scan_directory()which really didn't like using it.Although I think if #1361226: Make the file entity a classed object happens this will be moot.
Comment #28
dave reidComment #29
slashrsm commentedDo we still need this? Now core provides entity_create(), which is used by file_save_upload() and file_save_data().
Comment #30
balagan commentedAccording to entity_load the third parameter you are using in the path here is deprecated.
Comment #31
dave reidYes this is still needed. Because file_save_data() in core still duplicates the code that all the contrib modules will end up duplicating in D8 as well.
Comment #34
hgoto commentedAfter some investigation, I believe this issue can be closed because D8 already has satisfied this request.
In
file_save_data():In
file_save_upload():In
Drupal\file\Entity\File:So at least we shouldn't continue on D8.
On the other hand, as with D7, I think we can add new APIs currently. Please reopen this if you want this kind of function in D7.
Comment #35
dave reidThe major part of this is ability to re-use an existing file (either a permanent file or a temporary file owned by the current user), which is not addressed in #34. I do not consider this resolved.
Comment #36
dave reidAlso, if an issue is still valid for D7, let's please not just close it, change the version and update it. That said, this is still valid for D8. It would be useful to have it as a method on the File entity object itself.
Comment #37
dave reidThis is what I've needed to re-use on several projects.
Comment #38
hgoto commented@Dave Reid, thank you for your response. I got it.
Comment #39
jonhattangetOwnerId() may return null. It needs type check.
Comment #44
ajitsPatch in #37 is no longer applicable. Needs reroll.
Comment #45
ajitsPlain reroll.
Comment #48
larowlanClosing this as a duplicate of #3223209: deprecate file_save_data, file_copy and file_move and replace with a service
Transferring issue credit over there
Comment #49
berdirChanging to a won't fix as the other issue removed it again and with the loadByUri() that we *do* add, it just doesn't seem worth adding that on its own anymore. There has been little to no interest here in years.