5 Replies Latest reply on Dec 5, 2011 11:29 PM by thushanga

    File Download from Modeshape

    thushanga

      Hi All,

       

      I am using a repository first time and it the Modeshape. Please help me to overcome form the following simple problem.

       

      I use Modeshape 2.6 with DiskConnector and embeded mode. My application uploads file into Modeshape.

       

      I have a Presentaion logic in my application to display the list of files available.

       

      and when click on the file name file should be dowloaded.

       

      So how can i give Downlodable url for a file stored in the modeshape repository .

       

      Thanks

      Emil thushanga

        • 1. Re: File Download from Modeshape
          rhauch

          Perhaps this is obvious, but the JCR 2.0 specification does not define a way to obtain a URL for a binary object.

           

          But, the procedure to do this is pretty straightforward, though it is highly dependent upon your configuration and your application's use of the disk connector. It also circumvents all JCR-based security mechanisms. As such, proceed with caution.

           

          The disk connector stores all binary values based upon the SHA-1 hash of the contents. The name of the files are the Base-64 encoded value of the SHA-1 hash with ".dat" appended, and these files are stored in the directory given to the connector by the "largeValuePath" property in your configuration. If your configuration does not explicitly set it, the default is the "largeValues" directory inside the "repositoryRootPath" directory that your configuration is required to set. If you can get the SHA-1 of the binary file, you can easily construct a file-based URL for display and use in your application.

           

          Unfortunately, there's no easy way to get this out of the Binary value. (I've logged an enhancement for 2.7 to add to our API a way to get the SHA-1 hash. 2.7, which is due out in a week or so.)

           

          The best way is to for your application to compute it from the content and store it as a property on the parent of the Binary property. For example, if you're using 'nt:file' with an 'nt:resource' node pattern to store your files, you could add the 'mode:hashed' mixin to the 'nt:resource' node and set the 'mode:sha1' property to be the base-64 encoded SHA-1 (see our SecureHash and Base64 classes for how to do this) of the 'jcr:data' Binary property.

           

          The easiest, ugliest and most fragile way to do this is to get the SHA1 of the Binary value from its toString() method, which happens to output the hexadecimal form of the SHA-1 hash. You'd have to parse the toString() result, identify the hex SHA-1, convert that back to a byte[], and then convert the byte[] to a base-64-encoded string. Personally, I'd avoid this approach, as it really isn't that much easier and it is much more fragile than computing it directly from the content as outlined in the previous paragraph.

           

          Hope this helps.

          • 2. Re: File Download from Modeshape
            thushanga

            Thank you So much. This helps me lot.

            • 3. Re: File Download from Modeshape
              thushanga

              Can you Please Tell me , If i used database connector with Mysql , Then how will it be.

               

              Thank you so much.

              • 4. Re: File Download from Modeshape
                rhauch

                If i used database connector with Mysql , Then how will it be.

                The files will be stored within the database (not on the file system), so there's no way to provide a standard URL to your uploaded files. If you need a URL within your application, you might try a custom protocol handler that exposes the content from JCR through the URL mechanism.

                1 of 1 people found this helpful
                • 5. Re: File Download from Modeshape
                  thushanga

                  Thank you so much helping me.

                  Emil