module Safrano::EntityClassMedia
special handling for media entity
Attributes
media_handler[R]
slug_field[R]
Public Instance Methods
api_check_media_fields()
click to toggle source
# File lib/odata/collection_media.rb, line 235 def api_check_media_fields raise(Safrano::API::MediaModelError, self) unless db_schema.key?(:content_type) end
finalize_media()
click to toggle source
# File lib/odata/collection_media.rb, line 220 def finalize_media @media_handler.finalize end
new_media_entity(mimetype:)
click to toggle source
END API
methods
# File lib/odata/collection_media.rb, line 241 def new_media_entity(mimetype:) nh = { 'content_type' => mimetype } new_from_hson_h(nh) end
odata_create_entity_and_relation(req, assoc = nil, parent = nil)
click to toggle source
POST for media entity collection –> Create media-entity linked to uploaded file from payload
-
create new media entity
-
get the pk/id from the new media entity
-
Upload the file and use the pk/id to get an unique Directory/filename assignment to the media entity record
NOTE: we will implement this first in a MVP way. There will be plenty of
potential future enhancements (performance, scallability, flexibility... with added complexity)
-
create relation to parent if needed
# File lib/odata/collection_media.rb, line 255 def odata_create_entity_and_relation(req, assoc = nil, parent = nil) req.with_media_data do |data, mimetype, filename| ## future enhancement: validate allowed mimetypes ? # if (invalid = invalid_media_mimetype(mimetype)) # ::Safrano::Request::ON_CGST_ERROR.call(req) # return [422, {}, ['Invalid mime type: ', invalid.to_s]] # end if req.accept?(APPJSON) new_entity = new_media_entity(mimetype: mimetype) if slug_field new_entity.set_fields({ slug_field => filename }, data_fields, missing: :skip) end # call before_create_entity media hook new_entity.before_create_media_entity(data: data, mimetype: mimetype) if new_entity.respond_to? :before_create_media_entity media_handler.check_before_create(data: data, entity: new_entity, filename: filename).if_valid { |_ret| # to_one rels are create with FK data set on the parent entity if parent odata_create_save_entity_and_rel(req, new_entity, assoc, parent) else # in-changeset requests get their own transaction new_entity.save(transaction: !req.in_changeset) end req.register_content_id_ref(new_entity) new_entity.copy_request_infos(req) # call before_create_media hook new_entity.before_create_media if new_entity.respond_to? :before_create_media media_handler.save_file(data: data, entity: new_entity, filename: filename) # call after_create_media hook new_entity.after_create_media if new_entity.respond_to? :after_create_media # json is default content type so we dont need to specify it here again # Contract.valid([201, EMPTY_HASH, new_entity.to_odata_post_json(service: req.service)]) # TODO quirks array mode ! Contract.valid([201, EMPTY_HASH, new_entity.to_odata_create_json(request: req)]) }.tap_error { |e| return e.odata_get(req) }.result else # TODO: other formats 415 end end end
set_default_media_handler()
click to toggle source
API
method for defining the media handler eg. publish_media_model photos do
use Safrano::Media::Static, :root => '/media_root'
end
# File lib/odata/collection_media.rb, line 216 def set_default_media_handler @media_handler = Safrano::Media::Static.new(mediaklass: self) end
slug(inp)
click to toggle source
API
method for setting the model field mapped to SLUG on upload
# File lib/odata/collection_media.rb, line 231 def slug(inp) @slug_field = inp end
use(klass, args)
click to toggle source
# File lib/odata/collection_media.rb, line 224 def use(klass, args) args[:mediaklass] = self @media_handler = klass.new(**args) @media_handler.create_abs_class_dir end