mirror of https://github.com/Siphonay/mastodon
Add API to upload media attachments
This commit is contained in:
parent
05001d54d1
commit
ae1fac0062
|
@ -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/
|
|
@ -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
|
|
@ -0,0 +1,2 @@
|
|||
module Api::MediaHelper
|
||||
end
|
|
@ -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)
|
||||
|
|
|
@ -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
|
|
@ -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?'
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
object @media
|
||||
attribute :id
|
||||
node(:url) { |media| full_asset_url(media.file.url) }
|
|
@ -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"
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
15
db/schema.rb
15
db/schema.rb
|
@ -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"
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
Fabricator(:media_attachment) do
|
||||
status_id 1
|
||||
file ""
|
||||
remote_url "MyString"
|
||||
account_id 1
|
||||
end
|
Binary file not shown.
After Width: | Height: | Size: 60 KiB |
|
@ -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
|
|
@ -0,0 +1,5 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe MediaAttachment, type: :model do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
|
@ -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,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…
Reference in New Issue