This repository has been archived by the owner on Jul 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Bug 880569 - Publish Unpublished makes to the Make API
- Loading branch information
Christopher De Cairos
committed
Jun 15, 2013
1 parent
137998b
commit 5fcadba
Showing
1 changed file
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/** | ||
* This Script is designed to upgrade existing popcorn projects | ||
* generated by Popcorn Maker PRE Webmaker V2. It will publish | ||
* each project the does not contain a MakeID to the MakeAPI | ||
* and add the newly generated MakeID to the record. | ||
**/ | ||
|
||
var async = require( "async" ), | ||
path = require( "path" ), | ||
config = require( "../lib/config" ), | ||
|
||
// Database information | ||
dbConfig = config.database, | ||
sequelize = new (require( "sequelize" ))( | ||
dbConfig.database, | ||
dbConfig.username, | ||
dbConfig.password, | ||
dbConfig.options | ||
), | ||
|
||
// MakeAPI lib | ||
makeapi = require( "makeapi" ).makeAPI({ | ||
apiURL: config.MAKE_ENDPOINT, | ||
auth: config.MAKE_USERNAME + ":" + config.MAKE_PASSWORD | ||
}), | ||
|
||
// Project model | ||
Project = sequelize.import( path.normalize( __dirname + "/../lib/models/project" ) ), | ||
|
||
// helper vars | ||
limit = 50, | ||
completed = 0 | ||
hostname = config.dirs.embedHostname ? config.dirs.embedHostname : config.hostname, | ||
prefix = config.publishStore.namePrefix || "", | ||
suffix = config.publishStore.nameSuffix || ""; | ||
|
||
function expand( name ) { | ||
name = name + ''; | ||
return path.join( prefix, name ) + suffix; | ||
} | ||
|
||
function generatePublishUrl( id ) { | ||
return ( hostname + '/' + expand( id.toString( 36 ) ) ).replace( /\\/g, '/' ); | ||
} | ||
|
||
// Add the newly generated Make ID to the project | ||
function updateWithMakeID( project, makeid, callback ) { | ||
project.makeid = makeid; | ||
project.save().error(function( error ){ | ||
callback(error); | ||
}).success(function() { | ||
callback(); | ||
}); | ||
} | ||
|
||
// Build the make object, submit to MakeAPI | ||
function createMake( project, remixID, callback ) { | ||
|
||
var makeObj = { | ||
title: project.name, | ||
author: project.author || "", | ||
url: generatePublishUrl( project.id ), | ||
email: project.email, | ||
contentType: "application/x-popcorn", | ||
thumbnail: project.thumbnail, | ||
description: project.description || "", | ||
remixedFrom: remixID || null | ||
}; | ||
|
||
makeapi.create({ | ||
maker: "migrationScript", | ||
make: makeObj | ||
}, function( err, make ) { | ||
if ( err ) { | ||
console.log( err ); | ||
callback( err ); | ||
} | ||
updateWithMakeID( project, make.id, callback ); | ||
}); | ||
} | ||
|
||
// Resolve remix dependencies | ||
function resolveMake( project, callback ) { | ||
if ( project.remixedFrom ) { | ||
Project.find( project.remixedFrom ).success(function( remixProject ) { | ||
if ( remixProject ) { | ||
createMake( project, remixProject.makeid, callback ); | ||
return; | ||
} | ||
// deleted remix | ||
createMake( project, null, callback ); | ||
}); | ||
} else { | ||
createMake( project, null, callback ); | ||
} | ||
} | ||
|
||
function getNextSet( callback ) { | ||
Project.findAll({ | ||
where: { | ||
makeid: null | ||
}, | ||
limit: limit, | ||
order: "id ASC" | ||
}).complete(function( err, projects ) { | ||
async.eachSeries( projects, resolveMake, function() { | ||
callback(); | ||
}); | ||
}); | ||
} | ||
|
||
function migrateProjects( err ) { | ||
if ( err ) { | ||
console.log( "There was an error syncing the database: ", JSON.stringify( err ) ); | ||
return; | ||
} | ||
Project.count({ | ||
where: { | ||
makeid: null | ||
} | ||
}).success(function( count ) { | ||
console.log( "Found " + count + " unpublished projects..." ); | ||
async.doWhilst( getNextSet, function() { | ||
completed += limit; | ||
if ( completed < count ) { | ||
return true | ||
} | ||
return false; | ||
}, function( err ) { | ||
if ( err ) { | ||
console.log( "Error: ", err ); | ||
} | ||
console.log( count + " Projects successfully added to the MakeAPI!" ); | ||
}); | ||
}); | ||
} | ||
|
||
sequelize.sync().complete( migrateProjects ); |