Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > Java
You last visited: Today at 17:56

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Eine URL mit Hilfe von GridFS DownloadToStream ersetzen

Discussion on Eine URL mit Hilfe von GridFS DownloadToStream ersetzen within the Java forum part of the Coders Den category.

Reply
 
Old   #1
 
emmoplayer's Avatar
 
elite*gold: 0
Join Date: Jun 2008
Posts: 308
Received Thanks: 15
Eine URL mit Hilfe von GridFS DownloadToStream ersetzen

Hey alle zusammen.

Ich sitze seit Tagen an einem Problem was ich einfach nicht gelöst bekomme.

Ich nutze MongoDB als meine Datenbank und speichere Bilder über GridFS ab.
Leider kriege ich nicht über MongoDB eine Art URL zu den Bildern, die über GridFS abgespeichert worden sind.

Gibt es eine Möglichkeit, mit Hilfe des DownloadToStream/OpenDownloadStream eine URL zu ersetzen?
Leider konnte ich bei den Java Drivern nichts passendes finden.

Vielen Dank im Voraus.
emmoplayer is offline  
Old 04/18/2021, 23:45   #2
 
elite*gold: 0
Join Date: Apr 2011
Posts: 363
Received Thanks: 166
Quote:
Originally Posted by emmoplayer View Post
Hey alle zusammen.

Ich sitze seit Tagen an einem Problem was ich einfach nicht gelöst bekomme.

Ich nutze MongoDB als meine Datenbank und speichere Bilder über GridFS ab.
Leider kriege ich nicht über MongoDB eine Art URL zu den Bildern, die über GridFS abgespeichert worden sind.

Gibt es eine Möglichkeit, mit Hilfe des DownloadToStream/OpenDownloadStream eine URL zu ersetzen?
Leider konnte ich bei den Java Drivern nichts passendes finden.

Vielen Dank im Voraus.
I don't know what you mean by URL, im assuming you want your asset data url.

Code:
  // Obtain asset DATA url
    public String obtainFileUrl(GridFSBucket bucket, String fileNameOrId) {
        // open download stream
        GridFSDownloadStream downloadStream = bucket.openDownloadStream(fileNameOrId);
        // get file info, we need content type and file size
        GridFSFile file = downloadStream.getGridFSFile();
        // get content type
        org.bson.Document metadata = file.getMetadata();
        // if content type of asset is not stored as this key, change it
        String contentType = metadata.getString("contentType");
        // create byte buffer to read our stream
       // this can be bad if files are too big or memory is limited so be careful 
        byte[] raw = new byte[(int)file.getLength()];
        downloadStream.read(raw);
        downloadStream.close();
        // encode asset raw bytes to base64 string
        String base64EncodedAsset = Base64.getEncoder().encodeToString(raw);
        // create data url
        String url = String.format("data:%s;base64,%s", contentType, base64EncodedAsset);
        return url;
    }
This can be overhead if your api or whatever is consuming assets a lot.

Another solution: -> DownloadToStream to a file in a temp folder (with a hash or sth) and send back that file, every time a file needs to be consumed you check in your temp folder if a the file exits otherwise it creates the new one. (This strategy needs to flush unused files from time to time to prevent eating your disk space)
elmarcia is offline  
Thanks
1 User
Old 04/19/2021, 14:08   #3
 
emmoplayer's Avatar
 
elite*gold: 0
Join Date: Jun 2008
Posts: 308
Received Thanks: 15
Hey @.

Thank you for your efforts to explain both ways.
I took the second solution because the files could be really big.
emmoplayer is offline  
Old 04/20/2021, 02:50   #4
 
elite*gold: 0
Join Date: Apr 2011
Posts: 363
Received Thanks: 166
Quote:
Originally Posted by emmoplayer View Post
Hey @.

Thank you for your efforts to explain both ways.
I took the second solution because the files could be really big.
Which framework are you using in your backend?,

Take into account that:
Code:
GridFSDownloadStream downloadStream = bucket.openDownloadStream(fileNameOrId);
Inherits InputStream so you can write your response in a way like this:

Example with Spring Boot
Code:
{
...
 try {

    InputStream is = downloadStream;
     response.setContentType(contentType);      
     response.setHeader("Content-Disposition", "attachment;filename=\"+file.getFilename​()+\""); 
     // copy it to response's OutputStream
      org.apache.commons.io.IOUtils.copy(is, response.getOutputStream());
      response.flushBuffer();
    } catch (IOException ex) {
      throw new RuntimeException("IOError writing file to output stream");
    }
As a third option, this will forward the stream without needing to store in disk.
elmarcia is offline  
Thanks
1 User
Old 04/20/2021, 20:12   #5
 
emmoplayer's Avatar
 
elite*gold: 0
Join Date: Jun 2008
Posts: 308
Received Thanks: 15
@
I'm working without a special framework.

Quote:
As a third option, this will forward the stream without needing to store in disk.
Thanks!
emmoplayer is offline  
Reply


Similar Threads Similar Threads
[Hilfe] Wenn, URL 1 nicht geht dann URL 2
01/20/2015 - .NET Languages - 12 Replies
Hey ElitePvPers Community, Ich habe für den Privaten gebraucht mit ein kleines YouTube Video/Thumbnail Download Tool in Visual Basic erstellt, und habe eine frage bezüglich des Thumbnail Tools. Ich habe es so gemacht, das man oben die YouTube url eingibt, dann die ID vom Video genommen wird und dann in den Link zum Thumbnail eingefügt wird, um dieses dann durch einen klick auf einen Button zu speicher. https://i.ytimg.com/vi/ VIDEO ID HIER HIN /maxresdefault.jpg Hierbei gibt es aber...



All times are GMT +2. The time now is 17:56.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.