Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rootstrap/active-storage-base64
Base64 support for ActiveStorage
https://github.com/rootstrap/active-storage-base64
activestorage api hacktoberfest rails rails-api ruby ruby-on-rails
Last synced: about 13 hours ago
JSON representation
Base64 support for ActiveStorage
- Host: GitHub
- URL: https://github.com/rootstrap/active-storage-base64
- Owner: rootstrap
- License: mit
- Created: 2018-11-07T19:31:03.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-01-17T13:39:04.000Z (12 months ago)
- Last Synced: 2024-12-25T05:06:35.164Z (8 days ago)
- Topics: activestorage, api, hacktoberfest, rails, rails-api, ruby, ruby-on-rails
- Language: Ruby
- Homepage: https://rootstrap.com
- Size: 135 KB
- Stars: 161
- Watchers: 13
- Forks: 16
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
[![CI](https://github.com/rootstrap/active-storage-base64/actions/workflows/ci.yml/badge.svg)](https://github.com/rootstrap/active-storage-base64/actions/workflows/ci.yml)
[![Maintainability](https://api.codeclimate.com/v1/badges/0da0a0901cedd72aeb10/maintainability)](https://codeclimate.com/github/rootstrap/active-storage-base64/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/0da0a0901cedd72aeb10/test_coverage)](https://codeclimate.com/github/rootstrap/active-storage-base64/test_coverage)# ActiveStorageBase64
Adds support for base64 attachments to ActiveStorage.
## Installation
In order to get the gem working on your project you just need to add the gem to your project like this:
```ruby
gem 'active_storage_base64'
```## Compatibility
Rails Version | ActiveStorageBase64 Version
--------------|-----------------------------
7.1.x | 3.0.x
7.0.x | 2.0.x
6.1.x | 1.2.x
6.0.x | 1.1.x
5.2.x | 0.1.x## Prerequisites
The only prerequisite for using this gem is having ActiveStorage properly set up in your project. For more information on how to do this, check [Active Storage Overview](https://edgeguides.rubyonrails.org/active_storage_overview.html).
## Usage
In order to use the gem's functionality, you need to include the `ActiveStorageSupport::SupportForBase64` module in your ActiveRecord models.
For example:
```ruby
class User < ActiveRecord::Base
include ActiveStorageSupport::SupportForBase64
end
```Note:
We highly recommend using an alternative class that inherits from `ActiveRecord::Base` and includes the module so instead of including the module for each of your classes, you make them inherit from this new class, check below:
```ruby
class ApplicationRecord < ActiveRecord::Base
include ActiveStorageSupport::SupportForBase64
endclass User < ApplicationRecord
has_one_base64_attached :avatar
end
```After you have the module included in your class you'll be able to use the following two helper methods to work with base64 files:
When you need a single image attached:
```ruby
has_one_base64_attached
```
and when you need multiple files attached:
```ruby
has_many_base64_attached
```
These helpers will work just like the `has_one_attached` and `has_many_attached` helper methods from ActiveStorage.A working example for this, assuming we have a model `User` with an `avatar` attached would be:
```ruby
class User < ActiveRecord::Base
include ActiveStorageSupport::SupportForBase64has_one_base64_attached :avatar
end
```on your controller you could do any of the following:
```ruby
class UsersController < ApplicationController
def create
user = User.create(user_params)
endprivate
def user_params
params.require(:user).permit(avatar: :data, :username, :email)
end
end
``````ruby
class UsersController < ApplicationController
def create
user = User.create(user_params)
user.avatar.attach(data: params[:avatar]) # params[:avatar] => 'data:image/png;base64,[base64 data]'
endprivate
def user_params
params.require(:user).permit(:username, :email)
end
end
``````ruby
class UsersController < ApplicationController
def create
user = User.create(user_params)
user.avatar.attach(avatar_params) # avatar_params => { data: 'data:image/png;base64,[base64 data]' }
endprivate
def user_params
params.require(:user).permit(:username, :email)
enddef avatar_params
params.require(:avatar).permit(:data)
end
end
``````ruby
class UsersController < ApplicationController
def create
user = User.create(user_params)
user.avatar = { data: params[:avatar] } # params[:avatar] => 'data:image/png;base64,[base64 data]'
user.save
endprivate
def user_params
params.require(:user).permit(:username, :email)
end
end
```### Specifying a filename or content type
If you are willing to add a specific filename to your attachment, or send in a specific content type for your file, you can use `data:` to attach the base64 data and specify your `filename:`, `content_type:` and/or `identify:` hash keys.
Check the following example:
```ruby
class UsersController < ApplicationController
def create
user = User.create(user_params)
user.avatar.attach(data: params[:avatar], filename: 'your_filename', content_type: 'content/type', identify: 'false') # params[:avatar] => 'data:image/png;base64,[base64 data]'
endprivate
def user_params
params.require(:user).permit(:username, :email)
end
end
```Or, in case you want to have the avatar attached as soon as the user is created you can do:
```ruby
class UsersController < ApplicationController
def create
user = User.create(user_params)
endprivate
def user_params
params.require(:user).permit(:username, :email, avatar: [:data,
:filename,
:content_type,
:identify])
end
end
```### Data Format
To attach base64 data it is required to come in the form of [Data URIs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs) .
For example:
```
data:image/png;base64,[base64 data]
```## Contributing
Please read our [CONTRIBUTING](https://github.com/rootstrap/active-storage-base64/blob/master/CONTRIBUTING.md) and our [CODE_OF_CONDUCT](https://github.com/rootstrap/active-storage-base64/blob/master/CODE_OF_CONDUCT.md) files for details on our code of conduct, and the process for submitting pull requests to us.
## Author
*Ricardo Cortio*
## License
This project is licensed under the MIT License - see the [LICENSE](https://github.com/rootstrap/active-storage-base64/blob/master/LICENSE.txt) file for details
## Acknowledgments
Special thanks to the people who helped with guidance and ensuring code quality in this project:
*Santiago Bartesaghi, Santiago Vidal and Matias Mansilla.*## Credits
Active Storage Base64 is maintained by [Rootstrap](http://www.rootstrap.com) with the help of our
[contributors](https://github.com/rootstrap/active-storage-base64/contributors).[](http://www.rootstrap.com)