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.
User can use the following code to import the MediaCenter
module.
var MediaCenter = require('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.
MediaCenter.refresh([callback])
callback
{Function} Callback function.error
{Error} If an error occurs, indicate the cause of the error.
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
mediacenter.total(callback[, condition])
callback
{Function} Callback function.error
{Error} If an error occurs, indicate the cause of the error.total
{Integer} The total number of media resources.
condition
{String} Statistics search criteria. default: unconditional. Detail to see: Condition.
Count the number of media resources that meet the relevant conditions.
Example
var mediacenter = new MediaCenter();
mediacenter.total(function(error, count) {
if (error) {
console.error(error.message);
} else {
console.log('Total number:', count);
}
});
// Can have WHERE clause conditions
mediacenter.total(function(error, count) {
if (error) {
console.error(error.message);
} else {
console.log('Number of default album:', count);
}
}, `album='default'`);
mediacenter.space(callback)
callback
{Function} Callback function.error
{Error} If an error occurs, indicate the cause of the error.space
{Object} Media resource disk space statistics.
Get media resource disk space statistics. The space
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(function(error, space) {
if (error) {
console.error(error.message);
} else {
console.log('Image space(MB):', space.image);
}
});
mediacenter.albums(callback[, condition])
callback
{Function} Callback function.error
{Error} If an error occurs, indicate the cause of the error.list
{Array} Media albums array.
condition
{String} Statistics search criteria. default: unconditional. Detail to see: Condition.
Get a list of albums in the media center under specified conditions.
Example
mediacenter.albums(function(error, list) {
if (Array.isArray(list)) {
console.log('All albums:', list);
}
});
mediacenter.list(callback[, condition[, limit[, offset]]])
callback
{Function} Callback function.error
{Error} If an error occurs, indicate the cause of the error.list
{Array} Media resource description array.
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.
Get a list of media resource descriptions, each member in the list 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.
mediacenter.list(function(error, descs) {
if (descs) {
descs.forEach(function(item) {
console.log(item.id, item.ext, item.size);
});
}
}, undefined, 50);
mediacenter.info(id, callback)
id
{Integer} Media resource id.callback
{Function} Callback function.error
{Error} If an error occurs, indicate the cause of the error.info
{Object} Media resource information.
Get the information of the specified media resource. info
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.
mediacenter.remove(id[, callback])
id
{Integer} Media resource id.callback
{Function} Callback function. default: undefined.error
{Error} If an error occurs, indicate the cause of the error.
Remove the media resource specified by id
.
Example
// Delete the oldest resource
mediacenter.list(function(error, list) {
if (list) {
mediacenter.remove(list[0].id);
}
}, `ORDER BY time ASC`, 1);
mediacenter.get(id, thumbnail, callback[, opt])
id
{Integer} Media resource id.thumbnail
{Boolean} Whether to get the thumbnail.callback
{Function} Callback function.error
{Error} If an error occurs, indicate the cause of the error.stream
{ReadStream} Media resource readable stream.
opt
{Object} Get options.offset
{Integer} Video offset in bytes. default: 0.length
{Integer} Video length in bytes. default: hole video file size.
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
mediacenter.list(function(error, list) {
if (list) {
mediacenter.get(list[0].id, true, function(e, r) {
if (r) {
r.pipe(dest);
}
})
}
}, `ext='.jpg'`, 1);
mediacenter.get(id, thumbnail, callback, dest[, opt])
id
{Integer} Media resource id.thumbnail
{Boolean} Whether to get the thumbnail.callback
{Function} Callback function.error
{Error} If an error occurs, indicate the cause of the error.
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.
Obtain the specified media resources and automatically store them in the specified file.
Example
// Get the random thumbnail
mediacenter.list(function(error, list) {
if (list) {
mediacenter.get(list[0].id, false, function(e) {
if (error) {
console.error(error.message);
} else {
console.log('Resource get ok!');
}
}, `./dest${list[0].ext}`);
}
}, undefined, 1);
mediacenter.save(chunk[, opt], callback, ext)
chunk
{Buffer} Media resource data.opt
{Object} Media resource options.callback
{Function} Callback function.error
{Error} If an error occurs, indicate the cause of the error.id
{Integer} Media resource id.
ext
{String} Media resource extension name.
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');
mediacenter.save(chunk, function(error) {
if (error) {
console.error(error.message);
} else {
console.log('Resource save ok!');
}
}, '.jpg');
mediacenter.save(path[, opt], callback)
path
{String} Media resource file path.opt
{Object} Media resource options.callback
{Function} Callback function.error
{Error} If an error occurs, indicate the cause of the error.id
{Integer} Media resource id.
Save a file into the media center.
Example
mediacenter.save('./aaa.jpg', function(error) {
if (error) {
console.error(error.message);
} else {
console.log('Resource save ok!');
}
});
mediacenter.save(readable[, opt], callback, ext, size)
readable
{ReadStream} Media resource readable stream.opt
{Object} Media resource options.callback
{Function} Callback function.error
{Error} If an error occurs, indicate the cause of the error.id
{Integer} Media resource id.
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 error will be generated.
Example
// Readable stream can be web request object.
mediacenter.save(rstream, function(error) {
if (error) {
console.error(error.message);
} else {
console.log('Resource save ok!');
}
}, '.mp4', mp4Size);
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';