TYPO3 - Extbase - Detect missing files for a given FileReference - typo3

I've tried three different ways to detect if a FileReference's original file is still existing (i.e. file has been deleted outside TYPO3 using SFTP or similar):
if($fileReference instanceof \TYPO3\CMS\Extbase\Domain\Model\FileReference) {
$isMissing = $fileReference->getOriginalResource()->getStorage()->getFile($fileReference->getOriginalResource()->getIdentifier())->isMissing();
$isMissing = $fileReference->getOriginalResource()->getOriginalFile()->isMissing();
$isMissing = $fileReference->getOriginalResource()->isMissing();
}
Only the first one give me the right isMissing() value.

The property isMissing is an database value, which is set if the storage detect an missing file. On getFile the storage check if the file is missing and set "isMissing" for the file. If you dont persist this to the database, the setting is get loose with the next call.
You can also call $isMissing = $fileReference->getOriginalResource()->getStorage()->hasFile($fileReference->getOriginalResource()->getIdentifier());
You can run the file indexer scheduler (TYPO3\CMS\Scheduler\Task\FileStorageIndexingTask) if you want to check frequently for deleted files. This should be required if you let change files externaly (like ftp).

Related

How can I manually edit the list of recently opened files in VS Code?

I rely heavily on the File: Open Recent… command to open frequently used files, but yesterday my local Google Drive folder got moved to a new location and now I can no longer access any of the files in that folder through the Open Recent panel because the paths don't match.
The fix would be as simple as replacing "/Google Drive/" with "/Google Drive/My Drive/" but I have no idea what file contains the list of files that appears in the recently opened panel.
I'm assuming it's somewhere in ~/Library/Application Support/Code but not sure where.
I was wondering the same thing the other day and found this while searching for a solution, so I took some time to investigate it today.
It's been a a few weeks since you posted, so hopefully this will still be of help to you.
Also, I'm using Windows and I'm not familiar with macOS, but I think it should be easy enough adjust the solution.
Location of settings
Those setting are stored in the following file: %APPDATA%\Code\User\globalStorage\state.vscdb.
The file is an sqlite3 database, which is used as a key-value store.
It has a single table named ItemTable and the relevant key is history.recentlyOpenedPathsList.
The value has the following structure:
{
"entries": [
{
"folderUri": "/path/to/folder",
"label": "...",
"remoteAuthority": "..."
}
]
}
To view the current list, you can run the following command:
sqlite3.exe -readonly "%APPDATA%\Code\User\globalStorage\state.vscdb" "SELECT [value] FROM ItemTable WHERE [key] = 'history.recentlyOpenedPathsList'" | jq ".entries[].label"
Modifying the settings
Specifically, I was interested in changing the way it's displayed (the label), so I'll detail how I did that, but it should be just as easy to update the path.
Here's the Python code I used to make those edits:
import json, sqlite3
# open the db, get the value and parse it
db = sqlite3.connect('C:/Users/<username>/AppData/Roaming/Code/User/globalStorage/state.vscdb')
history_raw = db.execute("SELECT [value] FROM ItemTable WHERE [key] = 'history.recentlyOpenedPathsList'").fetchone()[0]
history = json.loads(history_raw)
# make the changes you'd like
# ...
# stringify and update
history_raw = json.dumps(history)
db.execute(f"UPDATE ItemTable SET [value] = '{history_raw}' WHERE key = 'history.recentlyOpenedPathsList'")
db.commit()
db.close()
Code references
For reference (mostly for my future self), here are the relevant source code areas.
The settings are read here.
The File->Open Recent uses those values as-is (see here).
However when using the Get Started page, the Recents area is populated here. In the Get Started, the label is presented in a slightly different way:
vscode snapshot
The folder name is the link, and the parent folder is the the text beside it.
This is done by the splitName method.
Notes
Before messing around with the settings file, it would be wise to back it up.
I'm not sure how vscode handles and caches the settings, so I think it's best to close all vscode instances before making any changes.
I haven't played around with it too much, so not sure how characters that need to be json-encoded or html-encoded will play out.
Keep in mind that there might be some state saved by other extensions, so if anything weird happens, blame it on that.
For reference, I'm using vscode 1.74.2.
Links
SQLite command-line tools
jq - command-line JSON processor

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 can I rename file uploaded to s3 using javascript api?

'pickAndStore' method allows me to specify full path to the file, but I don't know it's extension at this point (file path has to be defined before file is uploaded, so it's not possible to provide a path with correct extension).
if I use 'pick' and then 'store' I have 2 files (because both methods uploads file to the s3). I can delete 'old' file, but it's not optimal and can be pain (take ages) with really big files.
Is there any better solution? Ideally to rename existing file.
Currently, there is no workaround for renaming file.
However, in our Javascript API v2 we are planing to add new callback function. onStart callback will be fired after user pick file but before file uploading. There could be option like renaming file based on original filename.
We will keep you updated.

How to check generated file has been modified in Eclipse plugin development?

Currently the plugin will generate a series of files in an IProject, I need to check whether the generated file has been modified by user before. If the generated artifact has been modified by user, I will need to handle the regeneration differently.
What I can think of is by checking Creation Date == Modified Date . The fact that I will delete the old file and create it again when user has not touched the file before to make sure the Creation Date always equals Modified Date. However I did not see how to retrieve these 2 properties from IFile. Anyone can help me regarding this?
I am quite new to Eclipse plugin development, can anyone suggest another way around this ?
*** Generated files cannot be locked as those are source codes
The modification stamp of an IFile or more generally an IResource can be obtained with getModificationStamp(). The return value is not strictly a time stamp but should serve your needs, see the JavaDoc for details.
If, however, you would like to track whether the content of a file was changed I would rather compute a hash of the content, for example with a MessageDigest. You can then compare the two hashes to decide whether the file was changed.
This latter approach would regard a file as unchanged if it was changed - saved - changes reverted - saved again. The modification stamp on the other hand would declare the file as changed even though its content is the same again.
Whichever approach you choose, you can store the modification stamp (or content hash) at generation time by using IResource#setPersistentProperty() and later compare it with the current modification stamp. Persistent properties are stored on disk with the platform metadata and maintained across platform shutdown and restart.
I found the answer:
private boolean isModified(IFile existingFile) throws CoreException {
IFileState[] history = existingFile.getHistory(NullProgessMonitor);
return history.length > 0;
}
This feature is maintained by eclipse IDE so it will survive the restarting of eclipse. If the file has been created without modification , the history state is zero.
You can clear local history by doing:
existingFile.clearHistory(NullProgessMonitor);

How to specify and read properties in an Eclipse plugin

I have an Eclipse product which uses my own plugins. I want to read some properties based on user inputs. I want to persist these properties on some user action, and read those properties back when required. Can this be achieved using some Eclipse API?
A more elaborate description of the above problem:
Say I have a property abc=xyz in a config file myconfig.ini. This property is read by the perspective during the bootstrapping process. During use of the perspective, some action sets this property to a new value xyz=def. Now, I should be able to save the new value in myconfig.ini. So next time the bootstrapping happens, the value of xyz is read as def instead of abc. However, I can also choose to manually set it to abc by editing the myconfig.ini file.
How would I manage myconfig.ini? Where should it exist within my eclipse product project?
What is the best API to manage reading, writing and updating properties in myconfig.ini?
You can use resource markers mechanism:
IMarker marker = file.createMarker(IMarker.MARKER);
marker.setAttribute(IMarker.MESSAGE, "blabla");
marker.setAttribute("attr", 5);
You can search for markers by using the findMarkers methods on IResource.
See FAQ also
You should consider using the apache configurations API http://commons.apache.org/proper/commons-configuration/
It can read and write INI files and if you want to change the configuration file type or add more configuration options you can simply configure it.
I would add a hidden directory to the workspace root e.g. ${WORKSPACE}/.productName/product.ini
and add an ISaveParticipant that ensures the ini file gets updated on shutdown.
You can get the Workspace using the ResourcesPlugin
IWorkspace workspace = ResourcesPlugin.getWorkspace();
and resolve it to an absolute path
IWorkspaceRoot wsRoot = workspace.getRoot();
IPath wsPath = wsRoot.getRawLocation();
IPath absoluteWsPath = wsPath.makeAbsolute();