Skip to content

Allow project to include images in the backend #31

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

Merged
merged 14 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
ALLOWED_ORIGINS=localhost:3002,localhost:3000

AWS_ACCESS_KEY_ID=changeme
AWS_S3_ACTIVE_STORAGE_BUCKET=changeme
AWS_S3_REGION=changeme
AWS_SECRET_ACCESS_KEY=changeme

POSTGRES_HOST=changeme
POSTGRES_USER=changeme
POSTGRES_PASSWORD=changeme
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '3.0.3'

gem 'aws-sdk-s3', require: false
gem 'bootsnap', require: false
gem 'cancancan', '~> 3.3'
gem 'faraday'
Expand Down
18 changes: 18 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ GEM
addressable (2.8.0)
public_suffix (>= 2.0.2, < 5.0)
ast (2.4.2)
aws-eventstream (1.2.0)
aws-partitions (1.567.0)
aws-sdk-core (3.130.0)
aws-eventstream (~> 1, >= 1.0.2)
aws-partitions (~> 1, >= 1.525.0)
aws-sigv4 (~> 1.1)
jmespath (~> 1.0)
aws-sdk-kms (1.55.0)
aws-sdk-core (~> 3, >= 3.127.0)
aws-sigv4 (~> 1.1)
aws-sdk-s3 (1.113.0)
aws-sdk-core (~> 3, >= 3.127.0)
aws-sdk-kms (~> 1)
aws-sigv4 (~> 1.4)
aws-sigv4 (1.4.0)
aws-eventstream (~> 1, >= 1.0.2)
bootsnap (1.9.3)
msgpack (~> 1.0)
builder (3.2.4)
Expand Down Expand Up @@ -109,6 +125,7 @@ GEM
io-wait (0.2.1)
jbuilder (2.11.4)
activesupport (>= 5.0.0)
jmespath (1.6.1)
loofah (2.14.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
Expand Down Expand Up @@ -273,6 +290,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
aws-sdk-s3
bootsnap
cancancan (~> 3.3)
dotenv-rails
Expand Down
15 changes: 15 additions & 0 deletions app/controllers/api/projects/images_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Api
module Projects
class ImagesController < ApiController
before_action :require_oauth_user

def create
@project = Project.find_by!(identifier: params[:project_id])
@project.images.attach(params[:images])
render '/api/projects/images', formats: [:json]
end
end
end
end
1 change: 1 addition & 0 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Project < ApplicationRecord
belongs_to :parent, class_name: 'Project', foreign_key: 'remixed_from_id', optional: true, inverse_of: :children
has_many :components, -> { order(:index) }, dependent: :destroy, inverse_of: :project
has_many :children, class_name: 'Project', foreign_key: 'remixed_from_id', dependent: :nullify, inverse_of: :parent
has_many_attached :images
accepts_nested_attributes_for :components

private
Expand Down
8 changes: 8 additions & 0 deletions app/views/api/projects/images.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

json.call(@project)

json.image_list @project.images do |image|
json.filename image.filename
json.url rails_blob_url(https://github.com/image)
end
5 changes: 5 additions & 0 deletions app/views/api/projects/show.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ json.call(@project, :identifier, :project_type, :name, :user_id)
json.parent(@project.parent, :name, :identifier) if @project.parent

json.components @project.components, :id, :name, :extension, :content, :index

json.image_list @project.images do |image|
json.filename image.filename
json.url rails_blob_url(https://github.com/image)
end
3 changes: 1 addition & 2 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for Apache
# config.action_dispatch.x_sendfile_header = "X-Accel-Redirect" # for NGINX

# Store uploaded files on the local file system (see config/storage.yml for options).
config.active_storage.service = :local
config.active_storage.service = :amazon

# Mount Action Cable outside main process or domain.
# config.action_cable.mount_path = nil
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

resources :projects, only: %i[show update] do
resource :remix, only: %i[create], controller: 'projects/remixes'
resource :images, only: %i[create], controller: 'projects/images'
end
end
end
13 changes: 6 additions & 7 deletions config/storage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ local:
service: Disk
root: <%= Rails.root.join("storage") %>

# Use bin/rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key)
# amazon:
# service: S3
# access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
# secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
# region: us-east-1
# bucket: your_own_bucket-<%= Rails.env %>
amazon:
service: S3
access_key_id: <%= ENV.fetch('AWS_ACCESS_KEY_ID', nil) %>
secret_access_key: <%= ENV.fetch('AWS_SECRET_ACCESS_KEY', nil) %>
region: <%= ENV.fetch('AWS_S3_REGION', 'eu-west-2') %>
bucket: <%= ENV.fetch('AWS_S3_ACTIVE_STORAGE_BUCKET', nil) %>

# Remember not to checkin your GCS keyfile to a repository
# google:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
def change
# Use Active Record's configured type for primary and foreign keys
primary_key_type, foreign_key_type = primary_and_foreign_key_types

create_table :active_storage_blobs, id: primary_key_type do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.string :service_name, null: false
t.bigint :byte_size, null: false
t.string :checksum

if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end

t.index [ :key ], unique: true
end

create_table :active_storage_attachments, id: primary_key_type do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type
t.references :blob, null: false, type: foreign_key_type

if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end

t.index [ :record_type, :record_id, :name, :blob_id ], name: :index_active_storage_attachments_uniqueness, unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end

create_table :active_storage_variant_records, id: primary_key_type do |t|
t.belongs_to :blob, null: false, index: false, type: foreign_key_type
t.string :variation_digest, null: false

t.index [ :blob_id, :variation_digest ], name: :index_active_storage_variant_records_uniqueness, unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
end

private
def primary_and_foreign_key_types
config = Rails.configuration.generators
setting = config.options[config.orm][:primary_key_type]
primary_key_type = setting || :primary_key
foreign_key_type = setting || :bigint
[primary_key_type, foreign_key_type]
end
end
32 changes: 31 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ version: "3.9"
services:
db:
image: postgres:14.1
volumes:
- pg-data:/var/lib/postgresql/data
command: postgres -c listen_addresses='*'
environment:
- POSTGRES_PASSWORD=password
- POSTGRES_USER
- POSTGRES_PASSWORD
- POSTGRES_DB
ports:
- "5434:5432"
web:
Expand Down
6 changes: 6 additions & 0 deletions spec/factories/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@
object.components << FactoryBot.create(:default_python_component, project: object)
end
end

trait :with_attached_images do
after(:create) do |object|
object.images.attach(fixture_file_upload(Rails.root.join('spec/fixtures/test_image_1.png'), 'image/png'))
end
end
end
end
Binary file added spec/fixtures/files/test_image_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added spec/fixtures/files/test_image_2.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added spec/fixtures/files/test_image_3.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added spec/fixtures/files/test_image_4.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions spec/models/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
it { is_expected.to have_many(:components) }
it { is_expected.to have_many(:children) }
it { is_expected.to belong_to(:parent).optional(true) }
it { is_expected.to have_many_attached(:images) }
end

describe 'identifier not nil' do
Expand Down
58 changes: 58 additions & 0 deletions spec/request/projects/images_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'Images requests', type: :request do
let!(:project) { create(:project) }
let(:user_id) { 'e0675b6c-dc48-4cd6-8c04-0f7ac05af51a' }
let(:image_filename) { 'test_image_1.png' }
let(:params) { { images: [fixture_file_upload(image_filename, 'image/png')] } }
let(:expected_json) do
{
image_list: [
{
filename: image_filename,
url: rails_blob_url(https://github.com/project.images[0])
}
]
}.to_json
end

describe 'create' do
context 'when auth is correct' do
before do
mock_oauth_user
end

it 'attaches file to project' do
expect { post "/api/projects/#{project.identifier}/images", params: params }.to change { project.images.count }.by(1)
end

it 'returns file list' do
post "/api/projects/#{project.identifier}/images", params: params

expect(response.body).to eq(expected_json)
end

it 'returns success response' do
post "/api/projects/#{project.identifier}/images", params: params

expect(response.status).to eq(200)
end

it 'returns 404 response if invalid project' do
post '/api/projects/no-such-project/images'

expect(response.status).to eq(404)
end
end

context 'when auth is invalid' do
it 'returns unauthorized' do
post "/api/projects/#{project.identifier}/images"

expect(response.status).to eq(401)
end
end
end
end
3 changes: 2 additions & 1 deletion spec/request/projects/show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
project_type: 'python',
name: project.name,
user_id: project.user_id,
components: []
components: [],
image_list: []
}.to_json
end

Expand Down