Store a file imported with Content Hub
-
@cibersheep Yes I think it's ok, I have in apparmor file:
{ "policy_groups": [ "content_exchange" ], "policy_version": 16.04 }
-
@aloys Yep.
Then, try to move the file to directory in the writable cache dir (if you provide a directoy that doesn't exist --in the app cache directory-- it will be created) -
@cibersheep thanks!
I have some progress, the path given byStandardPaths.writableLocation(StandardPaths.CacheLocation)
does not work apparently because it starts withfile://
which in my case is:file:///home/phablet/.cache/taquin.aloysliska
.
I use instead a direct path:"/home/phablet/.cache/taquin.aloysliska/userImages"
, the move still fail but the fallback to copy works.Here are an extract from log:
qml: Transfer Charged Failed to move content to: "/home/phablet/.cache/taquin.aloysliska/userImages/userImage01.jpg" falling back to copy qml: file:///home/phablet/.cache/taquin.aloysliska/HubIncoming/60/image20210220_160613756.jpg qml: myImgUrl: /home/phablet/.cache/taquin.aloysliska/userImages/userImage01.jpg qml: Transfer Collected qml: Transfer Finalized qml: Transfer Finalized
So I have my file, I checked it is present with a shell. And it is still available within my app when I relaunched it.
But then when importing a new image with same process via content hub, I cannot overwrite the file, the move and fallback copy both failed if the file already exists.
So I am a bit lost, I will review my code, but if you have some ideas, this would be very helpful.To summarize what I'd like to do (in QML and js only, if possible), it is to allow user to import an image via content hub. This image shall be available for the app on the next launch. And the user shall be able to import a new image which will overwrite the previous one.
-
@aloys The move fails because the file is owned by the system, not by the app (something like that).
The files in HubIncoming are deleted when the phone reboots or if you tell the ContentHub to clean the files.On way of doing that is when Collected, make it finalize()
https://gitlab.com/cibersheep/tfamanager/-/blob/master/qml/Components/PageHubImport.qml#L69PS: For the
-
Thank you @cibersheep , your code helps me to do mine!
-
@aloys said in Store a file imported with Content Hub:
your code helps me to do mine
Open source basis beauty ^^
-
@aloys It's also from the code of others in the community.
I'm sure is improvable. Just MR any you have when you finish -
@aloys said in Store a file imported with Content Hub:
[...] does not work apparently because it starts with
file://
[...]Today I have discovered that qml StandardPaths returns url (that's why starts by
file://
) and QStandardPaths (cpp) returns paths (starts by root directory/
). -
@cibersheep , on my side I understand more clearly how works the Content Hub for import.
I share below my discoveries.When importing, the file is stored in cache directory, for example:
/home/phablet/.cache/taquin.aloysliska/HubIncoming/26/image20210220_160633634.jpg
This file:
- can be used within the app (that's the purpose)
- cannot be moved with method move from ContentItem, the move always fail.
- will be automatically deleted when closing the app, except with a dirty code that would not do finalize() method of ContentTransfer. That's dirty because imported file will accumulate in the cache directory and never deleted.
To have a permanent storage of the imported file, ContentStore should be defined and requested in ContentTransfer. There is example in the source code of ContentHub
here .To summarize, transfer is requested by:
peer.request(imgStore)
with imgStore defined by :ContentStore { id: imgStore scope: ContentScope.App // App: to store in application data space }
File will be stored in application data, for example:
/home/phablet/.local/share/taquin.aloysliska/Pictures/image20210220_160633634.jpg
This file:
- can be of course used within the app
- can be moved with method move from ContentItem, but you cannot overwrite an existing file with this method. Unfortunately that's what I wanted to do in my app!
- will remain when closing the app.
In pure QML and javascript you cannot erase or overwrite app data files! if you know please tell me, I really search a lot!
That's a missing feature I think.
So to erase or to overwrite an existing file, I need to use C++ . Fortunately, @fulvio mention a C++ plugin that should fit my needs: here . -
@aloys said in Store a file imported with Content Hub:
To summarize, transfer is requested by: peer.request(imgStore) with imgStore defined by :
ContentStore {
id: imgStore
scope: ContentScope.App // App: to store in application data space
}I can't believe it. I never ever understood how this worked. That's really cool... Now I need to do tests
There's also the possibility to choose an appId instead of the app chooser (which sometimes could be useful but not always). I'll need to investigate that.:
The ContentHub will not override a file like SingleDownload. You could convert the item into Base64[1] and use it as a source of an Image. And store it in a sqlite database... loooong workaround... or add a c++ backend and delete the file there (you can
clickable create
a new c++ project and it will set up a default plugin for youYou can do instead of
.move(dir)
,toDataURI()
and will return the Base64 string. -
@aloys said in Store a file imported with Content Hub:
ContentStore {
id: imgStore
scope: ContentScope.App // App: to store in application data space
}Oh, I was getting crazy with this... apparently it doesn't work for
ContentType.All
Ā¬Ā¬Edit: This seams to work only for Music, Pictures and Documents
-
Thanks @cibersheep for your answers, I succeed to do what I want with Content Hub and a C++ plugin to manage file deletion and renaming.
I wrote a a sample app available here: importwithcontenthub, with many (too much!) comments.
This sample app allow user to import an image, the image is stored (so that it is available on next app launch). And when user imports a new image, it is stored by overwriting the last one (only last imported image is stored).