From ae1fac006206ab28cd7cba63ab85c07a27f3d606 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Mon, 5 Sep 2016 17:46:36 +0200 Subject: Add API to upload media attachments --- app/assets/stylesheets/api/media.scss | 3 +++ app/controllers/api/media_controller.rb | 8 ++++++++ app/helpers/api/media_helper.rb | 2 ++ app/models/account.rb | 2 ++ app/models/media_attachment.rb | 13 +++++++++++++ app/models/status.rb | 1 + app/views/api/media/create.rabl | 3 +++ 7 files changed, 32 insertions(+) create mode 100644 app/assets/stylesheets/api/media.scss create mode 100644 app/controllers/api/media_controller.rb create mode 100644 app/helpers/api/media_helper.rb create mode 100644 app/models/media_attachment.rb create mode 100644 app/views/api/media/create.rabl (limited to 'app') diff --git a/app/assets/stylesheets/api/media.scss b/app/assets/stylesheets/api/media.scss new file mode 100644 index 000000000..e3c4fac3c --- /dev/null +++ b/app/assets/stylesheets/api/media.scss @@ -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/ diff --git a/app/controllers/api/media_controller.rb b/app/controllers/api/media_controller.rb new file mode 100644 index 000000000..d5a0a124a --- /dev/null +++ b/app/controllers/api/media_controller.rb @@ -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 diff --git a/app/helpers/api/media_helper.rb b/app/helpers/api/media_helper.rb new file mode 100644 index 000000000..ecaa91e7b --- /dev/null +++ b/app/helpers/api/media_helper.rb @@ -0,0 +1,2 @@ +module Api::MediaHelper +end diff --git a/app/models/account.rb b/app/models/account.rb index bcd6c1eba..8e0283ca1 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -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) diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb new file mode 100644 index 000000000..af1a4b9af --- /dev/null +++ b/app/models/media_attachment.rb @@ -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 diff --git a/app/models/status.rb b/app/models/status.rb index 14a698aae..05ae1a06e 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -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?' diff --git a/app/views/api/media/create.rabl b/app/views/api/media/create.rabl new file mode 100644 index 000000000..23909066d --- /dev/null +++ b/app/views/api/media/create.rabl @@ -0,0 +1,3 @@ +object @media +attribute :id +node(:url) { |media| full_asset_url(media.file.url) } -- cgit