API Docs for: 0.8.0
Show:

MediaServiceV2 Class

Provides functions to interact with media objects. This also includes interacting with the media contents

Constructor

MediaServiceV2

(
  • context
)

Parameters:

  • context Object
    • site String
    • onlyThisSite Boolean
    • [provider] MediaProvider optional

Methods

_emit

(
  • event
  • data
  • cb
)
protected

Parameters:

  • event String
  • data Object
  • cb Function

_get

(
  • id
  • [options]
  • cb
)
protected

Parameters:

  • id String
  • [options] Object optional
  • cb Function

_retrieveOnUpdateAndMerge

(
  • dto
  • options
  • cb
)
private
When an ID is available in the DTO the function attempts to retrieve the existing object. If it is not available a new object is created. The merge event is then called. After the merge is complete the callback is executed with the merged object.

Parameters:

  • dto Object
  • options Object
    • [isCreate] Boolean optional
  • cb Function

add

(
  • dto
  • [options]
  • cb
)
Attempts to persist the DTO as an add. The function executes a series of events: 1) The format event is fired 2) When an ID is provided the object is retrieved from the database otherwise a new object is created. 3) The merge event is triggered 4) The validate event is triggered. If validation errors are detected the process halts and the function calls back with an error. 5) The beforeSave event is triggered 6) The object is persisted 7) The afterSave event is triggered

Parameters:

  • dto Object
  • [options] Object optional
  • cb Function
    A callback that takes two parameters. The first is an error, if occurred. The second is the object that matches the specified query

count

(
  • [options]
  • cb
)
Executes a count of resources against the persistence layer. The function will callback with an array of results.

Parameters:

  • [options] Object optional
    • [where] Object optional
  • cb Function
    A callback that takes two parameters. The first is an error, if occurred. The second is the number of results that match the specified query

deleteById

(
  • id
  • options
  • cb
)
Deletes an object by ID

Parameters:

  • id String
  • options Object
  • cb Function

deleteSingle

(
  • [options]
  • cb
)
Deletes a single item based on the specified query in the options

Parameters:

  • [options] Object optional
    See BaseObjectService#getSingle
    • [where] Object optional
  • cb Function

extractNextMediaFlag

(
  • content
)
Object static

Given a content string the function will search for and extract the first occurrence of a media flag. The parsed value that is returned will include:

  • startIndex - The index where the flag was found to start
  • endIndex - The position in the content string of the last character of the media flag
  • flag - The entire media flag including the start and end markers
  • id - The media descriptor id that is referenced by the media flag
  • style - A hash of the style properties declared for the flag
  • cleanFlag - The media flag stripped of the start and end markers

Parameters:

  • content String

    The content string that potentially contains 1 or more media flags

Returns:

Object:

An object that contains the information about the parsed media flag.

findProviderType

() MediaProvider static

Looks up the prototype for the media provider based on the configuration

Returns:

MediaProvider:

formatMedia

(
  • media
)
Array static

Sets the proper icon and link for an array of media items

Parameters:

  • media Array

    The array of media objects to format

Returns:

Array:

The same array of media that was passed in

generateFilename

(
  • originalFilename
)
String static

Generates a filename for a new media object

Parameters:

  • originalFilename String

Returns:

String:

generateMediaPath

(
  • originalFilename
)
String static

Generates the path to uploaded media

Parameters:

  • originalFilename String

Returns:

String:

get

(
  • id
  • [options]
  • cb
)
Retrieves a resource by ID. The function will callback with the object that was found or NULL if no object could be found. The function will trigger the "get" event.

Parameters:

  • id String
  • [options] Object optional
  • cb Function

    (Error, object|null) A callback that takes two parameters. The first is an error, if occurred. The second is the object with the specified ID

getAll

(
  • [options]
  • cb
)
Executes a query for resources against the persistence layer. The function will callback with an array of results. The function will trigger the "getAll" event. Also note that there is hard limit on the number of results the returned.

Parameters:

  • [options] Object optional
    • [select] Object optional
    • [where] Object optional
    • [order] Array optional
    • [limit] Integer optional
    • [offset] Integer optional
  • cb Function

    A callback that takes two parameters. The first is an error, if occurred. The second is an array representing the results of the query.

getAllWithCount

(
  • [options]
  • cb
)
Executes a query for resources against the persistence layer. The function will callback with an object that contains a total count and an array of results. The function will trigger the "getAll" event. Also note that there is hard limit on the number of results the returned.

Parameters:

  • [options] Object optional
    • [select] Object optional
    • [where] Object optional
    • [order] Array optional
    • [limit] Integer optional
    • [offset] Integer optional
  • cb Function
    A callback that takes two parameters. The first is an error, if occurred. The second is an object representing the results of the query.

getContentStreamById

(
  • id
  • cb
)

Parameters:

  • id String
  • cb Function

getContentStreamByPath

(
  • mediaPath
  • cb
)

Parameters:

  • mediaPath String
  • cb Function

getContext

(
  • [data]
)
Object
Retrieves a context object to be passed to event listeners

Parameters:

  • [data] String | Object | Number | Boolean optional

Returns:

Object:

getIdWhere

(
  • dto
)
Object
Creates the where clause that creates a lookup by the key that indicates uniqueness for the collection

Parameters:

  • dto Object

Returns:

Object:

getMediaFlag

(
  • mid
  • [options]
)
String static

Generates a media placeholder for templating

Parameters:

  • mid String

    The media descriptor ID

  • [options] Object optional

    The list of attributes to be provided to the rendering element.

Returns:

String:

getMediaIcon

(
  • mediaType
)
String static

Retrieves the font awesome icon for the media type.

Parameters:

  • mediaType String

Returns:

String:

getRendererByType

(
  • mediaUrl
  • [isFile=false]
)
Object | Null static

Retrieves a media renderer for the specified URL

Parameters:

  • mediaUrl String

    The media URL

  • [isFile=false] Boolean optional

    TRUE if the URL represents an uploaded file, FALSE if not

Returns:

Object | Null:

A media renderer interface implementation or NULL if none support the given URL.

getRendererByType

(
  • type
)
Object | Null static

Retrieves a media renderer for the specified type

Parameters:

  • type String

    The media type

Returns:

Object | Null:

A media renderer interface implementation or NULL if none support the given type.

getSingle

(
  • [options]
  • cb
)
Retrieves a single resource by the specified query. The function will callback with the object that was found or NULL if no object could be found. The function will trigger the "getAll" event.

Parameters:

  • [options] Object optional
    • [select] Object optional
    • [where] Object optional
    • [order] Array optional
    • [offset] Integer optional
  • cb Function

    A callback that takes two parameters. The first is an error, if occurred. The second is the object that matches the specified query

getStyleForPosition

(
  • position
)
String static

The default editor implementations all for three position values to declared for embedded media (none, left, right, center). These values map to HTML alignments. This function retrieves the HTML style attribute for the provided position.

Parameters:

  • position String

    Can be one of 4 values: none, left, right, center

Returns:

String:

The HTML formatted style attribute(s)

getStyleForView

(
  • renderer
  • view
  • [overrides]
)
static

Retrieves the base style for the given renderer and view. Overrides will be applied on top of the base style.

Parameters:

  • renderer MediaRenderer

    An implementation of MediaRenderer

  • view String

    The view to retrieve the default styling for (view, editor, post)

  • [overrides] Object optional

    A hash of style properties that will be applied to the base style for the given view

getSupportedExtensions

() Array static

Provides a mechanism to retrieve all of the supported extension types that can be uploaded into the system.

Returns:

Array:

provides an array of strings

getType

() String
Retrieves the object type supported by the service

Returns:

String: The object type supported

isFile

(
  • mediaUrl
)
static

Determines if the media URI is a file. It is determined to be a file if and only if the URI does not begin with "http" or "//".

Parameters:

  • mediaUrl String

    A URI string that points to a media resource

isRegistered

(
  • interfaceImplementation
)
Boolean static

Indicates if a media renderer is already registered

Parameters:

  • interfaceImplementation Function | Object

    A prototype or object that implements the media renderer interface

Returns:

Boolean:

TRUE if registered, FALSE if not

loadMediaProvider

(
  • context
)
MediaProvider static

Parameters:

  • context Object
    • site String

Returns:

MediaProvider:

An instance of a media provider or NULL when no provider can be loaded.

onBeforeSave

(
  • context
  • cb
)
static

Parameters:

  • context Object
  • cb Function

    (Error)

onFormat

(
  • context
  • cb
)
static

Parameters:

  • context Object
    • data Object

      The incoming request body

    • service MediaServiceV2

      An instance of the service that triggered the event that called this handler

  • cb Function

    A callback that takes a single parameter: an error if occurred

onMerge

(
  • context
  • cb
)
static

Parameters:

  • context Object
    • data Object

      The incoming request body

    • object Object

      The object to be persisted

    • service MediaServiceV2

      An instance of the service that triggered the event that called this handler

  • cb Function

    A callback that takes a single parameter: an error if occurred

onValidate

(
  • context
  • cb
)
static

Parameters:

  • context Object
    • data Object

      The DTO that was provided for persistence

    • service MediaServiceV2

      An instance of the service that triggered the event that called this handler

  • cb Function

    A callback that takes a single parameter: an error if occurred

parseMediaFlag

(
  • flag
)
Object static

Parses a media flag and returns each part in an object. The parsed value that is returned will include:

  • id - The media descriptor id that is referenced by the media flag
  • style - A hash of the style properties declared for the flag
  • cleanFlag - The media flag stripped of the start and end markers

Parameters:

  • flag String

    The content string that potentially contains 1 or more media flags

Returns:

Object:

An object that contains the information about the parsed media flag.

persistContent

(
  • context
  • cb
)

Persists the uploaded media content to the provider

Parameters:

  • context Object
    • data Object
      • content File
        Formidable file descriptor
  • cb Function

    (Error)

registerRenderer

(
  • interfaceImplementation
)
Boolean static

Registers a media renderer

Parameters:

  • interfaceImplementation Function | Object

    A prototype or object that implements the media renderer interface.

Returns:

Boolean:

TRUE if the implementation was registered, FALSE if not

render

(
  • media
  • options
  • cb
)

Renders the media represented by the provided media descriptor.

Parameters:

  • media Object

    The media resource ID

    • media_type String
  • options Object
    • [attrs] Object optional

      The desired HTML attributes that will be added to the element that provides the rendering

    • [style] Object optional

      The desired style overrides for the media

    • [view] String optional

      The view type that the media will be rendered for (view, editor, post). Any style options provided will override those provided by the default style associated with the view.

  • cb Function

    A callback that provides two parameters. An Error if exists and the rendered HTML content for the media resource.

renderById

(
  • id
  • options
  • cb
)

Renders a media resource by ID where ID refers the to the media descriptor id.

Parameters:

  • id String

    The media resource ID

  • options Object
    • [attrs] Object optional

      The desired HTML attributes that will be added to the element that provides the rendering

    • [style] Object optional

      The desired style overrides for the media

    • [view] String optional

      The view type that the media will be rendered for (view, editor, post). Any style options provided will override those provided by the default style associated with the view.

  • cb Function

    A callback that provides two parameters. An Error if exists and the rendered HTML content for the media resource.

renderByLocation

(
  • options
  • cb
)

Renders a resource by type and location (mediaId).

Parameters:

  • options Object
    • location String

      The unique media identifier for the type

    • [type] String optional

      The type of provider that knows how to render the resource

    • [attrs] Object optional

      The desired HTML attributes that will be added to the element that provides the rendering

    • [style] Object optional

      The desired style overrides for the media

    • [view] String optional

      The view type that the media will be rendered for (view, editor, post). Any style options provided will override those provided by the default style associated with the view.

    • [isFile=false] Boolean optional
  • cb Function

    A callback that provides two parameters. An Error if exists and the rendered HTML content for the media resource.

save

(
  • dto
  • [options]
  • cb
)
Attempts to persist the DTO. The function executes a series of events: 1) The format event is fired 2) When an ID is provided the object is retrieved from the database otherwise a new object is created. 3) The merge event is triggered 4) The validate event is triggered. If validation errors are detected the process halts and the function calls back with an error. 5) The beforeSave event is triggered 6) The object is persisted 7) The afterSave event is triggered

Parameters:

  • dto Object
  • [options] Object optional
    • [isCreate] Boolean optional
  • cb Function
    A callback that takes two parameters. The first is an error, if occurred. The second is the object that matches the specified query

unregisterRenderer

(
  • interfaceToUnregister
)
Boolean static

Unregisters a media renderer

Parameters:

  • interfaceToUnregister Function | Object

    A prototype or object that implements the media renderer interface

Returns:

Boolean:

TRUE if unregistered, FALSE if not

update

(
  • dto
  • [options]
  • cb
)
Attempts to persist the DTO as an update. The function executes a series of events: 1) The format event is fired 2) When an ID is provided the object is retrieved from the database otherwise a new object is created. 3) The merge event is triggered 4) The validate event is triggered. If validation errors are detected the process halts and the function calls back with an error. 5) The beforeSave event is triggered 6) The object is persisted 7) The afterSave event is triggered

Parameters:

  • dto Object
  • [options] Object optional
  • cb Function
    A callback that takes two parameters. The first is an error, if occurred. The second is the object that matches the specified query

validate

(
  • context
  • cb
)

Parameters:

  • context Object
    • data Object

      The DTO that was provided for persistence

    • service MediaServiceV2

      An instance of the service that triggered the event that called this handler

  • cb Function

    A callback that takes a single parameter: an error if occurred

validateName

(
  • context
  • cb
)

Parameters:

  • context Object
    • data Object
      • name String
    • validationErrors Array
  • cb Function

    (Error)

validateTopicReferences

(
  • context
  • cb
)

Validates any references to topic objects in the data object passed through the context.

Parameters:

  • context Object
    • data Object
      • media_topics Array
    • validationErrors Array
  • cb Function

    (Error)

Properties

dao

DAO
An instance of DAO to be used to interact with the persistence layer

MEDIA_PROVIDERS

Object private static

REGISTERED_MEDIA_RENDERERS

Array private static

Contains the list of media renderers

TYPE

String private static

type

String
Represents the name of the collection to interact with