0
0
Fork 0

Add API to upload media attachments

This commit is contained in:
Eugen Rochko 2016-09-05 17:46:36 +02:00
parent 05001d54d1
commit ae1fac0062
19 changed files with 139 additions and 3 deletions

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/

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

View file

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

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)

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

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?'

View file

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