UBports Robot Logo UBports Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Store a file imported with Content Hub

    Scheduled Pinned Locked Moved Solved App Development
    15 Posts 3 Posters 999 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
      Reply
      • Reply as topic
      Log in to reply
      This topic has been deleted. Only users with topic management privileges can see it.
      • A Offline
        Aloys
        last edited by

        Re: Using image from Content Hub

        Hi,
        I find interesting information in above topic. I succeed to import an image with content hub.
        Now I'd like to save this image in order to have it still there when launching again the app.
        I try to use move function of content item my code here:

        imgImportPage.activeTransfer.items[0].move(StandardPaths.writableLocation(StandardPaths.CacheLocation))
        

        but I get always this error:

        Failed to move content to: "file:///home/phablet/.cache/taquin.aloysliska/image20210219_153501743.jpg" falling back to copy
        Failed to copy content to: "file:///home/phablet/.cache/taquin.aloysliska/image20210219_153501743.jpg"
        qml: file:///home/phablet/.cache/taquin.aloysliska/HubIncoming/49/image20210219_153501743.jpg
        

        I try to change the path name, to change the location (in .local/share/..), but none works.
        Has someone any idea to solve this issue ?

        I wonder also how we can manage directories and files (make a directory, move, delete, overwrite file), can it be done in QML or in javascript (no doc found about this) or do I need to code this in C++, python or other?

        KenedaK CiberSheepC 2 Replies Last reply Reply Quote 0
        • KenedaK Offline
          Keneda @Aloys
          last edited by Keneda

          @aloys
          Isn't .cache folder read only?

          Edit : answer to myself, no it is not.

          2015-2023 : Meizu MX4 ☠️⚰️✝️
          2023-2024 : Nexus 5 ☠️⚰️✝️
          2024-***** : FPOS Fairphone 5 waiting UT for freedom 😉
          🇲🇫🇬🇧

          1 Reply Last reply Reply Quote 0
          • CiberSheepC Offline
            CiberSheep @Aloys
            last edited by CiberSheep

            @aloys Have you added the permissions to the apparmor?
            https://docs.ubports.com/en/latest/appdev/platform/apparmor.html#content-exchange

            Another planet, another time, another universe!

            A 1 Reply Last reply Reply Quote 0
            • A Offline
              Aloys @CiberSheep
              last edited by

              @cibersheep Yes I think it's ok, I have in apparmor file:

              {
                  "policy_groups": [
                      "content_exchange"
                  ],
                  "policy_version": 16.04
              }
              
              CiberSheepC 1 Reply Last reply Reply Quote 0
              • CiberSheepC Offline
                CiberSheep @Aloys
                last edited by CiberSheep

                @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)

                Another planet, another time, another universe!

                A 1 Reply Last reply Reply Quote 0
                • A Offline
                  Aloys @CiberSheep
                  last edited by

                  @cibersheep thanks!
                  I have some progress, the path given by StandardPaths.writableLocation(StandardPaths.CacheLocation) does not work apparently because it starts with file:// 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.

                  CiberSheepC 2 Replies Last reply Reply Quote 0
                  • CiberSheepC Offline
                    CiberSheep @Aloys
                    last edited by CiberSheep

                    @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#L69

                    PS: For the

                    Another planet, another time, another universe!

                    A 1 Reply Last reply Reply Quote 1
                    • A Offline
                      Aloys @CiberSheep
                      last edited by

                      Thank you @cibersheep , your code helps me to do mine!

                      KenedaK CiberSheepC 2 Replies Last reply Reply Quote 1
                      • KenedaK Offline
                        Keneda @Aloys
                        last edited by

                        @aloys said in Store a file imported with Content Hub:

                        your code helps me to do mine

                        Open source basis beauty ^^

                        2015-2023 : Meizu MX4 ☠️⚰️✝️
                        2023-2024 : Nexus 5 ☠️⚰️✝️
                        2024-***** : FPOS Fairphone 5 waiting UT for freedom 😉
                        🇲🇫🇬🇧

                        1 Reply Last reply Reply Quote 1
                        • CiberSheepC Offline
                          CiberSheep @Aloys
                          last edited by

                          @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 🙂

                          Another planet, another time, another universe!

                          1 Reply Last reply Reply Quote 0
                          • CiberSheepC Offline
                            CiberSheep @Aloys
                            last edited by

                            @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 /).

                            Another planet, another time, another universe!

                            A 1 Reply Last reply Reply Quote 0
                            • A Offline
                              Aloys @CiberSheep
                              last edited by Aloys

                              @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 .

                              CiberSheepC 2 Replies Last reply Reply Quote 0
                              • CiberSheepC Offline
                                CiberSheep @Aloys
                                last edited by CiberSheep

                                @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 you

                                [1] http://api-docs.ubports.com/sdk/apps/qml/Ubuntu.Content/ContentItem.html#sdk-ubuntu-content-contentitem-todatauri

                                You can do instead of .move(dir), toDataURI() and will return the Base64 string.

                                Another planet, another time, another universe!

                                1 Reply Last reply Reply Quote 1
                                • CiberSheepC Offline
                                  CiberSheep @Aloys
                                  last edited by CiberSheep

                                  @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 🙂

                                  Another planet, another time, another universe!

                                  A 1 Reply Last reply Reply Quote 0
                                  • A Offline
                                    Aloys @CiberSheep
                                    last edited by

                                    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).

                                    1 Reply Last reply Reply Quote 2
                                    • First post
                                      Last post