Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] store a bookmark's favicon in MongoDB #39

Open
masukomi opened this issue Jun 24, 2024 · 0 comments
Open

[feature] store a bookmark's favicon in MongoDB #39

masukomi opened this issue Jun 24, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@masukomi
Copy link
Owner

masukomi commented Jun 24, 2024

Use cases:

  • As a user I'd like to be able to see my bookmark's favicons even when not online.
  • As a user I'd like to minimize the traffic sent to google because bandwidth is expensive in many parts of the world, and cell connections are slow in other parts of the world.
  • As a user I'd like the app to be as fast as possible with no unnecessary network calls slowing things down.

MongoDB 's mongoid ruby driver supports a BSON::Binary data type (elsewhere referred to as BinData). Which is good for files of less than 16Mb.

My thinking is:

  1. when a bookmark is added 2 background jobs are spawned.
  • one to archive it
  • one to retrieve the favicon from google, using the current method, but in the background
  1. the binary data is ingested from google, and stored in the db in a new Domain object.
  2. the Bookmark model has_one: :domain (belongs_to ???) and the domain has a field :favicon, type: BSON::Binary
  3. when a bookmark is loaded on the web page, the view queries the domain to see if it has a favicon in the db and uses it if it does.

Things I'm unsure about:

  • What's the incantation to ingest binary data into a db field?

Serving

(example from here

class PhotosController < ApplicationController
  def serve
    @photo = Photo.find(params[:id])
    send_data(
      @photo.data, # a binary column / field in your db. in our case BSON::Binary
      :type => @photo.mime_type, 
      :filename => "#{@photo.name}.png", # but with dynamic suffix based on mime type 
      :disposition => "inline"
     )
  end
end

Storing

We probably need an Image model because we'd need to store mime type too (favicons can come in multiple types) and it wouldn't make any sense to store that on a Domain model.

Routing

# rails3
resources :photos do
  get "serve", :on => :member
end

View

<%= image_tag serve_photo_path(@photo) %>

(serve_photo_path is because of the route defined above)

How to Download

  1. read this
  2. install the down gem
  3. download to temp file
  4. then...

"then the step after downloading the file to a Tempfile would be to set the field value to the value of the tempfile's content.

So if it's a variable named foo then you'd want to do something like: record.image_field = foo.rewind.read
via Kevin Lawler on mastodon post

@masukomi masukomi added the enhancement New feature or request label Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant