controllers/quiz_controller.js
/**
 * Crea una promesa para actualizar un attachment en la tabla Attachments.
 */
Crea una promesa para actualizar un attachment en la tabla Attachments.
Crea una promesa para actualizar un attachment en la tabla Attachments .
function updateAttachment(req, uploadResult, quiz) {
    if (!uploadResult) {
        return Promise.resolve();
    }
    var old_public_id = quiz.Attachment ? quiz.Attachment.public_id : null;
    return quiz.getAttachment()
    .then(function(attachment) {
        if (!attachment) {
            attachment = models.Attachment.build({ QuizId: quiz.id });
        }
        attachment.public_id = uploadResult.public_id;
        attachment.url = uploadResult.url;
        attachment.filename = req.file.originalname;
        attachment.mime = req.file.mimetype;
        return attachment.save();
    })
    .then(function(attachment) {
        req.flash('success', 'Imagen nueva guardada con éxito.');
        if (old_public_id) {
            cloudinary.api.delete_resources(old_public_id);
        }
    })
    .catch(function(error) { 
        req.flash('error', 'No se ha podido salvar la nueva imagen: '+error.message);
        cloudinary.api.delete_resources(uploadResult.public_id);
    });
}
function updateAttachment ( req , uploadResult , quiz ) { if ( ! uploadResult ) { return Promise . resolve ( ) ; } var old_public_id = quiz . Attachment ? quiz . Attachment . public_id : null ; return quiz . getAttachment ( ) . then ( function ( attachment ) { if ( ! attachment ) { attachment = models . Attachment . build ( { QuizId : quiz . id } ) ; } attachment . public_id = uploadResult . public_id ; attachment . url = uploadResult . url ; attachment . filename = req . file . originalname ; attachment . mime = req . file . mimetype ; return attachment . save ( ) ; } ) . then ( function ( attachment ) { req . flash ( 'success' , 'Imagen nueva guardada con éxito.') ; if ( old_public_id ) { cloudinary . api . delete_resources ( old_public_id ) ; } } ) . catch ( function ( error ) { req . flash ( 'error' , 'No se ha podido salvar la nueva imagen: ' + error . message ) ; cloudinary . api . delete_resources ( uploadResult . public_id ) ; } ) ; }
Crea una promesa para actualizar un attachment en la tabla Attachments.
Crea una promesa para actualizar un attachment en la tabla Attachments .
// Recordar public_id de la imagen antigua. // Ignoro errores de validacion en imagenes