MediaCenter : Media center
Media Center is a media resource warehouse provided by EdgerOS. App can use this warehouse to save and read shared media resources, such as mobile phone album synchronization. This module is available in EdgerOS 1.6.0 and later.
This module is the asynchronous mode of the MediaCenter
module. User can use the following code to import the MediaCenter
module.
var MediaCenter = require('async/mediacenter');
Support
The following shows MediaCenter
module APIs available for each permissions.
User Mode | Privilege Mode | |
---|---|---|
MediaCenter | ● | ● |
MediaCenter.columns | ● | ● |
MediaCenter.refresh | ● | |
mediacenter.total | ● | ● |
mediacenter.space | ● | ● |
mediacenter.albums | ● | ● |
mediacenter.list | ● | ● |
mediacenter.info | ● | ● |
mediacenter.remove | ● | ● |
mediacenter.get | ● | ● |
mediacenter.save | ● | ● |
MediaCenter Class
new MediaCenter()
- Returns: {Object} mediacenter object.
Create a new MediaCenter
object.
MediaCenter.columns
- {Array} Media resource database search columns.
You can use the columns given by this array to perform a combined condition search.
Each member in the array contains the following members:
name
{String} Column name.type
{String} Column type.
A typical MediaCenter.columns
array is as follows:
[
{ name: 'album', type: 'text' },
{ name: 'ext', type: 'text' },
{ name: 'acoid', type: 'text' },
{ name: 'app', type: 'text' },
{ name: 'digest', type: 'text' },
{ name: 'time', type: 'integer' },
{ name: 'size', type: 'integer' }
];
album
Represents the name of the album, and the default is'default'
.ext
Media file extension, including'.'
, such as'.jpg'
.acoid
This media file is saved by which user id.app
Which application saved this media file.digest
Media file MD5 digest value, hex format string.time
The time specified when the media file was saved, for example:new Date().getTime()
.size
The size of this media file, in bytes.
When querying conditional parameters, you can use the combination logic of these columns, which is equivalent to the WHERE
clause of the SQL statement, or you can use ORDER BY
for sorting.
async MediaCenter.refresh()
- Returns: {Boolean} Whether the operation was successful.
Repair database mismatches. Due to the possibility of power failure during the transmission process and other data errors, this function can be repaired, but it takes a long time. During the repair process, the Media Center system service will not respond any operation request.
MediaCenter Object
async mediacenter.total([condition])
condition
{String} Statistics search criteria. default: unconditional. Detail to see: Condition.- Returns: {Integer} The total number of media resources.
Count the number of media resources that meet the relevant conditions.
Example
var mediacenter = new MediaCenter();
async function defaultCount() {
return await mediacenter.total(`album='default'`);
}
async function totalCount() {
return await mediacenter.total();
}
async mediacenter.space()
- Returns: {Object} Media resource disk space statistics.
Get media resource disk space statistics. The return object contains the following members:
image
{Integer} Disk space occupied by image files, in Mbytes.media
{Integer} Disk space occupied by video or audio, in Mbytes.other
{Integer} Disk space occupied by other types, in Mbytes.
Example
mediacenter.space().then(space => console.log(space.image), console.error);
async mediacenter.albums([condition])
condition
{String} Statistics search criteria. default: unconditional. Detail to see: Condition.- Returns: {Array} Media albums array.
Get a list of albums in the media center under specified conditions.
Example
async function showAlbums() {
var list = await mediacenter.albums();
for (var album of list) {
console.log(album);
}
}
async mediacenter.list([condition[, limit[, offset]]])
condition
{String} Statistics search criteria. default: unconditional. Detail to see: Condition.limit
{Integer} Query entry limit. default: unlimit.offset
{Integer} Query start position. default: 0.- Returns: {Array} Media resource description array.
Get a list of media resource descriptions, each member in the returned object contains the following members:
id
{Integer} Media resource id.ext
{String} Media resource extension name.size
{Integer} Original resource size.digest
{String} Media resource MD5 digest value, hex format string.
Example
// Get first 50 media resource descriptions.
async function showResource() {
var list = await mediacenter.list(undefined, 50);
for (var item of list) {
console.log(item.id, item.size);
}
}
async mediacenter.info(id)
id
{Integer} Media resource id.- Returns: {Object} Media resource information.
Get the information of the specified media resource. The returned object contains the following members:
album
{String} The name of the album.ext
{String} Media resource extension name.acoid
{String} This media file is saved by which user id.app
{String} Which application saved this media file.digest
{String} Media resource MD5 digest value, hex format string.time
{Integer} The time specified when the media file was saved.size
{Integer} Original resource size.position
{Object} Geographic information.extra
{Object} Custom information when media information is saved.
If position
exists, the position
object contains the following information:
latitude
{Integer} Latitude.longitude
{Integer} Longitude.
async mediacenter.remove(id)
id
{Integer} Media resource id.
Remove the media resource specified by id
.
Example
// Delete the oldest resource
async function removeOldest() {
var list = await mediacenter.list(`ORDER BY time ASC`, 1);
await mediacenter.remove(list[0].id);
}
async mediacenter.get(id, thumbnail[, opt])
id
{Integer} Media resource id.thumbnail
{Boolean} Whether to get the thumbnail.opt
{Object} Get options.offset
{Integer} Video offset in bytes. default: 0.length
{Integer} Video length in bytes. default: hole video file size.
- Returns: {ReadStream} Media resource readable stream.
Get the specified media resource. When a media resource is saved, EdgerOS will automatically generate a thumbnail of this resource (the audio file has no thumbnail). You can specify whether to get only the thumbnail through the thumbnail
parameter. Thumbnails are in JPEG format.
Example
var dest = fs.createWriteStream('./dest.jpg');
// Get the jpg thumbnail
async function getThumbnail() {
var list = await mediacenter.list(`ext='.jpg'`, 1);
var r = await mediacenter.get(list[0].id, true);
r.pipe(dest);
//...
}
async mediacenter.get(id, thumbnail, dest[, opt])
id
{Integer} Media resource id.thumbnail
{Boolean} Whether to get the thumbnail.dest
{String} Target file.opt
{Object} Get options.offset
{Integer} Video offset in bytes. default: 0.length
{Integer} Video length in bytes. default: hole video file size.
- Returns: {Boolean} Whether the operation was successful.
Obtain the specified media resources and automatically store them in the specified file.
Example
// Get the jpg thumbnail
async function getThumbnail() {
var list = await mediacenter.list(`ext='.jpg'`, 1);
return await mediacenter.get(list[0].id, true, './dest.jpg');
}
async mediacenter.save(chunk[, opt], ext)
chunk
{Buffer} Media resource data.opt
{Object} Media resource options.ext
{String} Media resource extension name.- Returns: {Integer} Media resource id.
Save a chunk
into the media center. opt
can contain the following information:
album
{String} The name of the album. default: 'default'.acoid
{String} This media file is saved by which user id. default: ''.position
{Object} Geographic information. default: undefined.extra
{Object} Custom information when media information is saved. default: undefined.
Example
var chunk = fs.readFile('./aaa.jpg');
async function saveChunk() {
return await mediacenter.save(chunk, '.jpg');
}
async mediacenter.save(path[, opt])
path
{String} Media resource file path.opt
{Object} Media resource options.- Returns: {Integer} Media resource id.
Save a file into the media center.
Example
async function saveFile() {
return await mediacenter.save('./aaa.jpg');
}
async mediacenter.save(readable[, opt], ext, size)
readable
{ReadStream} Media resource readable stream.opt
{Object} Media resource options.ext
{String} Media resource extension name.size
{Integer} Media resource size, in bytes.
Save the readable stream as a media center, which is mostly used for large data video resources. The size
represents the maximum transfer size, when this value exceeds the EdgerOS limit, an exception will be thrown.
Example
// Readable stream can be web request object.
var readable = fs.createReadStream('./file.mov');
async function saveVideo() {
return await mediacenter.save(readable, '.mov', fs.size('./file.mov'));
}
Condition
The select condition expression support:
- Relational operations:
>
,>=
,<
,<=,
=
,==
,!=
. - Logic operation:
AND
,OR
. - Range operation:
BETWEEN...AND
. - Order operation:
ORDER BY... DESC|ASC
.
Example
- Relational.
var condition = 'album = "family"'; // `album` name is 'family'.
var condition = `album = 'family'`; // NOTICE: String support "" and ''.
var condition = 'size > 1024'; // File `size` more then 1KB.
- Logic.
var condition = 'album = "family" OR album = "life"'; // `aibum` name is 'family' or 'life'.
var condition = 'album = "family" AND ext = ".png"'; // `aibum` name is 'family' and extension name is '.png'.
- Range.
var condition = 'size BETWEEN 0 AND 1024'; // Select small file.
var condition = 'album = "family" AND size BETWEEN 0 AND 1024';
- Order.
var condition = 'ORDER BY time ASC'; // Sort ascending.
var condition = 'ORDER BY time'; // Default: sort ascending.
var condition = 'ORDER BY time DESC'; // Sort descending.
- Combination.
var condition = 'album = "family" ORDER BY time';
var condition = 'album = "family" AND ext = ".png" ORDER BY time DESC';