Browse Source

Add API to upload media attachments

closed-social-glitch-2
Eugen Rochko 8 years ago
parent
commit
ae1fac0062
19 changed files with 139 additions and 3 deletions
  1. +3
    -0
      app/assets/stylesheets/api/media.scss
  2. +8
    -0
      app/controllers/api/media_controller.rb
  3. +2
    -0
      app/helpers/api/media_helper.rb
  4. +2
    -0
      app/models/account.rb
  5. +13
    -0
      app/models/media_attachment.rb
  6. +1
    -0
      app/models/status.rb
  7. +3
    -0
      app/views/api/media/create.rabl
  8. +2
    -0
      config/environments/test.rb
  9. +1
    -0
      config/routes.rb
  10. +14
    -0
      db/migrate/20160905150353_create_media_attachments.rb
  11. +14
    -1
      db/schema.rb
  12. +34
    -0
      spec/controllers/api/media_controller_spec.rb
  13. +2
    -2
      spec/controllers/api/statuses_controller_spec.rb
  14. +6
    -0
      spec/fabricators/media_attachment_fabricator.rb
  15. BIN
     
  16. +15
    -0
      spec/helpers/api/media_helper_spec.rb
  17. +5
    -0
      spec/models/media_attachment_spec.rb
  18. +2
    -0
      spec/rails_helper.rb
  19. +12
    -0
      spec/spec_helper.rb

+ 3
- 0
app/assets/stylesheets/api/media.scss View File

@ -0,0 +1,3 @@
// Place all the styles related to the Api::Media controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

+ 8
- 0
app/controllers/api/media_controller.rb View File

@ -0,0 +1,8 @@
class Api::MediaController < ApiController
before_action :doorkeeper_authorize!
respond_to :json
def create
@media = MediaAttachment.create!(account: current_user.account, file: params[:file])
end
end

+ 2
- 0
app/helpers/api/media_helper.rb View File

@ -0,0 +1,2 @@
module Api::MediaHelper
end

+ 2
- 0
app/models/account.rb View File

@ -31,6 +31,8 @@ class Account < ApplicationRecord
has_many :following, through: :active_relationships, source: :target_account
has_many :followers, through: :passive_relationships, source: :account
has_many :media_attachments, dependent: :destroy
MENTION_RE = /(?:^|\s|\.)@([a-z0-9_]+(?:@[a-z0-9\.\-]+)?)/i
def follow!(other_account)

+ 13
- 0
app/models/media_attachment.rb View File

@ -0,0 +1,13 @@
class MediaAttachment < ApplicationRecord
belongs_to :account, inverse_of: :media_attachments
belongs_to :status, inverse_of: :media_attachments
has_attached_file :file
validates_attachment_content_type :file, content_type: /\Aimage\/.*\z/
validates :account, presence: true
def local?
self.remote_url.blank?
end
end

+ 1
- 0
app/models/status.rb View File

@ -11,6 +11,7 @@ class Status < ApplicationRecord
has_many :reblogs, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblog, dependent: :destroy
has_many :replies, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :thread
has_many :mentions, dependent: :destroy
has_many :media_attachments, dependent: :destroy
validates :account, presence: true
validates :uri, uniqueness: true, unless: 'local?'

+ 3
- 0
app/views/api/media/create.rabl View File

@ -0,0 +1,3 @@
object @media
attribute :id
node(:url) { |media| full_asset_url(media.file.url) }

+ 2
- 0
config/environments/test.rb View File

@ -41,3 +41,5 @@ Rails.application.configure do
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
end
Paperclip::Attachment.default_options[:path] = "#{Rails.root}/spec/test_files/:class/:id_partition/:style.:extension"

+ 1
- 0
config/routes.rb View File

@ -54,6 +54,7 @@ Rails.application.routes.draw do
end
resources :follows, only: [:create]
resources :media, only: [:create]
resources :accounts, only: [:show] do
collection do

+ 14
- 0
db/migrate/20160905150353_create_media_attachments.rb View File

@ -0,0 +1,14 @@
class CreateMediaAttachments < ActiveRecord::Migration[5.0]
def change
create_table :media_attachments do |t|
t.integer :status_id, null: true, default: nil
t.attachment :file
t.string :remote_url, null: false, default: ''
t.integer :account_id
t.timestamps
end
add_index :media_attachments, :status_id
end
end

+ 14
- 1
db/schema.rb View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160826155805) do
ActiveRecord::Schema.define(version: 20160905150353) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -59,6 +59,19 @@ ActiveRecord::Schema.define(version: 20160826155805) do
t.index ["account_id", "target_account_id"], name: "index_follows_on_account_id_and_target_account_id", unique: true, using: :btree
end
create_table "media_attachments", force: :cascade do |t|
t.integer "status_id"
t.string "file_file_name"
t.string "file_content_type"
t.integer "file_file_size"
t.datetime "file_updated_at"
t.string "remote_url", default: "", null: false
t.integer "account_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["status_id"], name: "index_media_attachments_on_status_id", using: :btree
end
create_table "mentions", force: :cascade do |t|
t.integer "account_id"
t.integer "status_id"

+ 34
- 0
spec/controllers/api/media_controller_spec.rb View File

@ -0,0 +1,34 @@
require 'rails_helper'
RSpec.describe Api::MediaController, type: :controller do
render_views
let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
let(:token) { double acceptable?: true, resource_owner_id: user.id }
before do
allow(controller).to receive(:doorkeeper_token) { token }
end
describe 'POST #create' do
before do
post :create, params: { file: fixture_file_upload('files/attachment.jpg', 'image/jpeg') }
end
it 'returns http success' do
expect(response).to have_http_status(:success)
end
it 'creates a media attachment' do
expect(MediaAttachment.first).to_not be_nil
end
it 'uploads a file' do
expect(MediaAttachment.first).to have_attached_file(:file)
end
it 'returns media ID in JSON' do
expect(body_as_json[:id]).to eq MediaAttachment.first.id
end
end
end

+ 2
- 2
spec/controllers/api/statuses_controller_spec.rb View File

@ -64,7 +64,7 @@ RSpec.describe Api::StatusesController, type: :controller do
end
it 'return json with updated attributes' do
hash_body = JSON.parse(response.body).with_indifferent_access
hash_body = body_as_json
expect(hash_body[:reblog][:id]).to eq status.id
expect(hash_body[:reblog][:reblogs_count]).to eq 1
@ -92,7 +92,7 @@ RSpec.describe Api::StatusesController, type: :controller do
end
it 'return json with updated attributes' do
hash_body = JSON.parse(response.body).with_indifferent_access
hash_body = body_as_json
expect(hash_body[:id]).to eq status.id
expect(hash_body[:favourites_count]).to eq 1

+ 6
- 0
spec/fabricators/media_attachment_fabricator.rb View File

@ -0,0 +1,6 @@
Fabricator(:media_attachment) do
status_id 1
file ""
remote_url "MyString"
account_id 1
end

BIN
View File


+ 15
- 0
spec/helpers/api/media_helper_spec.rb View File

@ -0,0 +1,15 @@
require 'rails_helper'
# Specs in this file have access to a helper object that includes
# the Api::MediaHelper. For example:
#
# describe Api::MediaHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe Api::MediaHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end

+ 5
- 0
spec/models/media_attachment_spec.rb View File

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe MediaAttachment, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

+ 2
- 0
spec/rails_helper.rb View File

@ -6,6 +6,7 @@ abort("The Rails environment is running in production mode!") if Rails.env.produ
require 'spec_helper'
require 'rspec/rails'
require 'webmock/rspec'
require 'paperclip/matchers'
ActiveRecord::Migration.maintain_test_schema!
WebMock.disable_net_connect!
@ -19,6 +20,7 @@ RSpec.configure do |config|
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::TestHelpers, type: :view
config.include Paperclip::Shoulda::Matchers
end
RSpec::Sidekiq.configure do |config|

+ 12
- 0
spec/spec_helper.rb View File

@ -12,4 +12,16 @@ RSpec.configure do |config|
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
config.after(:suite) do
FileUtils.rm_rf(Dir["#{Rails.root}/spec/test_files/"])
end
end
def body_as_json
json_str_to_hash(response.body)
end
def json_str_to_hash(str)
JSON.parse(str).with_indifferent_access
end

Loading…
Cancel
Save