Photo model

This class inherited from the Photo of blog frontend.

Photo model

The photo model contains three attributes. The first attribute is a title containing the description of photo. The second attribute is a name of image file. The third attribute is a link to the article, which owns photo.

Images from users can be large and have different types. There is no need to store files in original state. Therefore, images will be converted into a common format. To do this, use the ImageConverter behavior. Its incoming data is the File model describing uploaded file. The size property of the behavior defines maximum height or width of picture scale.

In addition to the full image, you need to display previews of various sizes. The best way is to create separate files for each thumbnail size. Use the neededThumbs property that contains a list of the required dimensions.

To protect the copyright, or create unique photos, use a watermark. It will automatically be applied to each image. Set an image size and a corresponding watermark file in the watermark property.

Files can be given for a direct link as static resources or only after verification of access rights. In the case of restricted access converted clear images are placed in a private part of a website (storeDir: path.join(__dirname, '../uploads/photos')). These sources are available in the admin module only. But all watermarked thumbnails placed in the frontend resources (thumbDir: path.join(__dirname, '../../../web/photos')).

The filenameAttr property defines a model attribute that stores file name.


const Base = require('../../../model/Photo');
const path = require('path');

module.exports = class Photo extends Base {

  static getConstants () {
    return {
      TABLE: 'photo',
      ATTRS: [
      RULES: [
        ['title', 'string', {min: 3, max: 255}],
        ['file', 'required', {on: ['create']}],
        ['file', 'file', {onlyImage: true}],
        ['articleId', 'filter', {filter: 'ObjectId'}],
        ['articleId', 'exist', {
          targetClass: require('./Article'),
          targetAttr: this.PK
      BEHAVIORS: {
        photo: {
          Class: require('../component/behaviors/ImageConverter'),
          FileClass: require('./File'),
          filenameAttr: 'filename',
          storeDir: path.join(__dirname, '../uploads/photos'),
          thumbDir: path.join(__dirname, '../../../web/photos'),
          size: 720,
          neededThumbs: [720, 360, 128],
          watermark: {
            720: path.join(__dirname, '../data/photo-watermark.png')

const Article = require('./Article');

The relArticle relation finds article related to a photo.


relArticle () {
  return this.hasOne(Article, Article.PK, 'articleId');