Add API to upload media attachments
This commit is contained in:
parent
05001d54d1
commit
ae1fac0062
19 changed files with 139 additions and 3 deletions
3
app/assets/stylesheets/api/media.scss
Normal file
3
app/assets/stylesheets/api/media.scss
Normal 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
app/controllers/api/media_controller.rb
Normal file
8
app/controllers/api/media_controller.rb
Normal 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
app/helpers/api/media_helper.rb
Normal file
2
app/helpers/api/media_helper.rb
Normal file
|
@ -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)
|
||||
|
|
13
app/models/media_attachment.rb
Normal file
13
app/models/media_attachment.rb
Normal 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
|
|
@ -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
app/views/api/media/create.rabl
Normal file
3
app/views/api/media/create.rabl
Normal file
|
@ -0,0 +1,3 @@
|
|||
object @media
|
||||
attribute :id
|
||||
node(:url) { |media| full_asset_url(media.file.url) }
|
Loading…
Add table
Add a link
Reference in a new issue