TYPO3 11: convert t3 file uri into file identifier - typo3

How can I convert the TYPO3 file uri t3://file?uid=54 to be usable for other TYPO3 methods which need a file identifier?
The file uri is returned by a flexform which selects a XML file. This XML file should be read.
However I cannot find a useful API function in the TYPO3 Core.
$paramTestFile = 't3://file?uid=54';
$xmlString = GeneralUtility::getURL($paramTestFile);
The above code fails for TYPO3 URIs.
The file reference with uid=54 is at "fileadmin/example.xml".
The Filelist backend module shows the details of this file.
However I need this file path also in the PHP code in order to read in the file.
use TYPO3\CMS\Core\LinkHandling\FileLinkHandler;
$fileHandler = GeneralUtility::makeInstance(FileLinkHandler::class);
$fileInfo = $fileHandler->asString($paramTestFile);
It is not possible to use $paramTestFile in the above example.
The class FileLinkHandler and its method asString does exactly the opposite of what I need.
$content = #file_get_contents($url);
However the absolute file path is needed for the read method . How can I convert the FAL file URI into the file identifier?
use TYPO3\CMS\Core\Resource\StorageRepository;
$storageRepository = GeneralUtility::makeInstance(StorageRepository::class);
$defaultStorage = $storageRepository->getDefaultStorage();
$fileInfo = $defaultStorage->getFileByIdentifier($paramTestFileIdentifier);

To resolve a t3://file... URN use TYPO3\CMS\Core\LinkHandling\LinkService::resolve(). This method will return an array with a key file containing a TYPO3\CMS\Core\Resource\FileInterface. This also works for other URNs but will return another array structure.
Then use the getContents() method of the FileInterface to finally get the content of the file.
The documentation is not very detailed for this part of the core so I have linked you directly to the related sources.

Related

How to load a template text file in SAPUI5?

I have a template fragment file, which is an xml file in fact.
I want to load it in my controller, do some modification on that, and then use it to render some part of the view.
I only need to read this xml file as text file and put its content in a string.
I cannot find any object for doing this in SAPUI5 api.
Please note the file is placed in my view folder in server side.
I need some kind of promise that read the file and after reading the file run a successor function.
Thanks in advance
There can be multiple ways to to this.
1.Either you can load your XML in an XML Model "sap.ui.model.xml.XMLModel()"
var oModelX = new sap.ui.model.xml.XMLModel();
oModelX.attachRequestCompleted(function(){
var xmlStr = oModelX.getXML();
console.log(xmlStr); // Do what ever you want with your xml string
});
oModelX.loadData("../view/st.fragment.xml");
2.You can also read the content of that XML file using AJAX and do your parsing in AJAX response.

Upload same name files to google cloud storage then download them with original names

So in google-cloud-storage if you upload more than one file with the same name to it the last will overwrite what was uploaded before it.
If I want to upload more than one file with the same name I should append some unique thing to the file name e.g. timestamp, random UUID.
But by doing so I'll lose the original file name while downloading, because I want to serve the file directly from google.
If we used the unique identifier as a folder instead of appending it to the file name, e.g. UUID +"/"+ fileName then we can download the file with its original name.
You could turn on Object Versioning which will keep the old versions of the object around.
Alternatively, you can set the Content Disposition header when uploading the object, which should preserve whatever filename you want on download.
instead of using object versioning, you can attach the UUID (or any other unique identifier) and then update the metadata of the object (specifically the content disposition), the following is a part of a python script i've used to remove the forward slashes - added by google cloud buckets when to represent directories - from multiple objects, it's based on this blog post, please keep in mind the double quotes around the content position "file name"
def update_blob_download_name(bucket_name):
""" update the download name of blobs and remove
the path.
:returns: None
:rtype: None
"""
# Storage client, not added to the code for brevity
client = initialize_google_storage_client()
bucket = client.bucket(bucket_name)
for blob in bucket.list_blobs():
if "/" in blob.name:
remove_path = blob.name[blob.name.rfind("/") + 1:] # rfind gives that last occurence of the char
ext = pathlib.Path(remove_path).suffix
remove_id = remove_path[:remove_path.rfind("_id_")]
new_name = remove_id + ext
blob.content_disposition = f'attachment; filename="{new_name}"'
blob.patch()

Typo3 7.2 add file reference to extension model

I'm using Typo 7.2 and am looking for an answer to the following question:
How to add a generated File as FileReference programmatically to an extension model?
First some infos regarding my achievements/tries.
DONE A command controller runs over folders, looks for a specific image and creates a blurred file via GraphicFunctions. The generated file is added to the storage as a standalone simple file and appears in the sys_file table.
$fileObject = $posterStorage->addFile(
$convertResult[3],
$posterStorage->getFolder($blurFolderName),
$newFileName);
PARTIALLY DONE. Now I need to add the generated file as a file reference to my model. The problem is, that I'm able to do this, but only by hacking core - not acceptable - and unable to do it the right way. The model says:
public function addPosterWebBlur(
\TYPO3\CMS\Extbase\Domain\Model\FileReference $posterWebBlur
) {
$this->posterWebBlur->attach($posterWebBlur);
}
So I succeeded by extending the FileReference class:
class FileReference extends \TYPO3\CMS\Extbase\Domain\Model\FileReference {
public function setFile(\TYPO3\CMS\Core\Resource\File $falFile) {
$this->$uidLocal = (int)$falFile->getUid();
}
}
The reference does not get established and I just get the following error in the backend:
Table 'db_name.tx_ext_name_domain_model_filereference' doesn't exist.
UPDATE
After integrating the data from Frans in ext_typoscript_setup.txt, the model can be saved, creates an sys_file_reference entry and acts nicely in the backend. But there are a few points open to fulfill all needs:
The sys_file_reference table does not contain a value for table_local, whereas all the entries generated by a backend user hold sys_file as value.
The same applies to l10n_diffsource which holds some binary large object. This entry gets inserted in the sys_file_reference table after saving the record manually via backend.
The pid of the file_reference has to be set via setPid($model->getPid()), is that okay?
The cruser_id is always set to zero. Is this the correct way?
When trying to delete a file (which was added to a model with the backend possibilities) via the file manager, I get a warning, that references to this file exist. This does not apply to the fileReference added programmatically. Also the references listed under the file (when clicking on "Info" for a generated file in the backend file manager) don't get listed. They get listed, when I enter the "sys_file" value in the sys_file_reference table by hand.
As Helmut Hummels example holds additional data, I'm wondering, if I just miss some stuff.
The file reference is used inside an object storage, but as the addImage function only calls objectStorage->attach I think this should be okay and no additional objectStorage actions are neccessary. Correct?
You have to tell the extbase persistence layer to use the correct table. See for instance this example https://github.com/helhum/upload_example/blob/master/ext_typoscript_setup.txt
gr. Frans
Trying to answer 1)
See
https://github.com/helhum/upload_example/blob/master/Configuration/TCA/tx_uploadexample_domain_model_example.php#L128
You should probably check the TCA definition for your posterWebBlur field. Second param of getFileFieldTCAConfig()
TT

How to get file name from Sharepoint Asset Library

How can I get the file name of an image from a SharePoint 2013 Asset Library?
I am trying to write a JQuery/REST snippet to search a subset of images within the library, based on their other column values and display them. I would use FileLeafRef in case of a Document Library, but I couldn't find an equivalent field in Asset Library.
I so far tried the following, neither returns file name:
https:///crm/_api/Web/Lists/GetByTitle('Publication%20List')/items?select=File/Name&expand=File
https:///crm/_api/Web/Lists/GetByTitle('Publication%20List')/items?select=FileLeafRef
There is a typo in your example, in particular $ symbol is missing for $select and $expand query options (more details).
The following rest endpoints demonstrate how to retrieve file name from Assets library:
1) Using FileLeafRef property:
/_api/web/lists/getByTitle('<list title>')/items?$select=FileLeafRef
2) Using File/Name property:
/_api/web/lists/getbytitle('<list title>')/items?$select=File/Name&$expand=File
3) Using Folder/Files property:
/_api/web/getfolderbyserverrelativeurl('<list url>')/files?$select=Name
I believe that yt's still FileLeafRef for an Asset Library. Any chance you could put the relevant code in your question?
Here's the rest endpoint for FileLeafRef:
/_api/web/lists/getByTitle('<list title>')/items?$select=FileLeafRef
Alternatively you could always use the File Name property:
/_api/web/lists/getByTitle('<list title>')/items?$select=File/Name&$expand=File

Exporting an JAR file in Eclipse and referencing a file

I have a project with a image stored as a logo that I wish to use.
URL logoPath = new MainApplication().getClass().getClassLoader().getResource("img/logo.jpg");
Using that method I get the URL for the file and convert it to string. I then have to substring that by 5 to get rid of this output "file:/C:/Users/Stephen/git/ILLA/PoC/bin/img/logo.jpg"
However when I export this as a jar and run it I run into trouble. The URL now reads /ILLA.jar!/ and my image is just blank. I have a gut feeling that it's tripping me up so how do I fix this?
Cheers
You are almost there.
Images in a jar are treated as resources. You need to refer to them using the classpath
Just use getClass().getResource: something like:
getClass().getResource("/images/logo.jpg"));
where "images" is a package inside the jar file, with the path as above
see the leading / in the call - this will help accessing the path correctly (using absolute instead of relative). Just make sure the path is correct
Also see:
How to includes all images in jar file using eclipse
See here: Create a file object from a resource path to an image in a jar file
String imgName = "/resources/images/image.jpg";
InputStream in = getClass().getResourceAsStream(imgName);
ImageIcon img = new ImageIcon(ImageIO.read(in));
Note it looks like you need to use a stream for a resource inside an archive.