pyc-website
main website for pyc inc.
git clone https://9o.is/git/pyc-website.git
commit b5c6e53c47648f6edf13f865ff4adb712134c2a3 parent 35d2aadebfeb87a693362a09c94ca875821a2f1b Author: Jul <jul@9o.is> Date: Wed, 9 Jul 2014 05:48:06 -0400 move resful services to 'rest' package Diffstat:
| M | src/main/scala/bootstrap/liftweb/Boot.scala | | | 8 | ++++---- |
| D | src/main/scala/inc/pyc/lib/IdVerification.scala | | | 129 | ------------------------------------------------------------------------------- |
| A | src/main/scala/inc/pyc/rest/IdVerification.scala | | | 135 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | src/main/scala/inc/pyc/rest/Sitemap.scala | | | 22 | ++++++++++++++++++++++ |
| M | src/main/scala/inc/pyc/snippet/Sitemap.scala | | | 9 | --------- |
| M | src/main/scala/inc/pyc/snippet/UserSnip.scala | | | 2 | ++ |
6 files changed, 163 insertions(+), 142 deletions(-)
diff --git a/src/main/scala/bootstrap/liftweb/Boot.scala b/src/main/scala/bootstrap/liftweb/Boot.scala @@ -2,19 +2,18 @@ package bootstrap.liftweb import scala.xml.{Null, UnprefixedAttribute} import javax.mail.internet.MimeMessage - import net.liftweb._ import common._ import http._ import util._ import util.Helpers._ - import inc.pyc.config._ import inc.pyc.lib._ +import inc.pyc.rest._ import inc.pyc.model.{SystemUser, User} - import net.liftmodules.extras.{Gravatar, LiftExtras} import net.liftmodules.mongoauth.MongoAuth +import inc.pyc.rest.IdVerificationUpload /** * A class that's instantiated early and run. It allows the application @@ -81,7 +80,8 @@ class Boot extends Loggable { // Google Analytics GoogleAnalytics.angularInit() - LiftRules.statelessDispatch.append(inc.pyc.snippet.Sitemap) + // Website sitemap + Sitemap.init() // ID Verification API IdVerificationUpload.init() diff --git a/src/main/scala/inc/pyc/lib/IdVerification.scala b/src/main/scala/inc/pyc/lib/IdVerification.scala @@ -1,128 +0,0 @@ -package inc.pyc -package lib - -import model._ -import config._ -import net.liftweb._ -import http._ -import util._ -import rest._ -import common._ -import js.JsCmds._ -import net.liftmodules.mongoauth.MongoAuth -import inc.pyc.aws.s3._ - -/** - * The identification files uploaded by a user will be stored here. - * Assure to clean up when done to save memory. - */ -object IdVerificationFiles extends SessionVar[List[FileParamHolder]](Nil) - -/** - * The REST point is used to load identification files to memory only. - * Manipulation should occur somewhere else (like snippet). - */ -object IdVerificationUpload extends RestHelper with Logger { - - def init(): Unit = { - LiftRules.dispatch.append(IdVerificationUpload) - } - - /** - * File can only be a maximum of 5 MB. - */ - def imgTooLarge(file: FileParamHolder) = file.length > 5243000 - - /** - * Only allow images of types: jpeg, png, svg - */ - def imgInvalid(file: FileParamHolder) = ! List( - "image/jpeg", "image/png", "image/svg+xml").exists(_ == file.mimeType) - - /** - * The maximum amount of files allowed in memory per user. - */ - def maxFilesInMemory = 3 - - /** - * Checks if another file can be added to memory or not. - */ - def tooManyFiles = IdVerificationFiles.get.size >= maxFilesInMemory - - /** - * API for uploading files to server. - */ - serve("settings" / "verification" prefix { - case "upload" :: Nil Post req => - for { - user <- User.currentUser ?~ "Must be logged in" ~> 400 - file <- Box(req.uploadedFiles) ?~ "File not found" - } yield { - if(imgInvalid(file)) - ResponseWithReason(BadResponse(), "Can only upload jpeg, png or svg file types.") - else if(imgTooLarge(file)) - ResponseWithReason(BadResponse(), "Image can only be 5MB or less.") - else if(tooManyFiles) - ResponseWithReason(BadResponse(), "Up to "+maxFilesInMemory+" files can be uploaded at once.") - else { - val thisreq = req - IdVerificationFiles.set(IdVerificationFiles.get :+ file) - OkResponse() - } - } - }) -} - -/** - * Other utility functions to help with the process of - * uploading identification files. - */ -object IdVerificationHelper { - - private val s3Obj = new S3( - Props.get("idverify.access.key", "NONE"), - Props.get("idverify.secret.key", "NONE"), - Props.get("idverify.bucket", "NONE")) - - /** - * Create an S3 object for uploading files. - */ - def s3(path: String = "") = s3Obj.copy(path = path) - - /** - * S3 folder formatting: /full_name/id/date/ - */ - def createS3Folder(user: User) = { - val prefix = if(Props.devMode) "dev/" else "" - - prefix + s"%s_%s/%s/%s/".format(user.fname.get, - user.lname.get, user.id.get, new java.util.Date().toString) - } - - /** - * Notifies us of a new user requesting their - * identification to be verified. - * @param thirdParty if the user is requesting verification with 3rd party - */ - def notifyIdentityRequest(user: User, thirdParty: Boolean = false): Unit = { - import net.liftweb.util.Mailer._ - - val msgTxt = - s""" - |${if(thirdParty) "3RD PARTY VERIFICATION" else ""} - |Identity Check Request! - |Full Name: ${user.fname} ${user.lname} - |Id: ${user.id.get} - |Limit: ${user.purchaseLimit.get} - |Email: ${user.email.get} - |Username: ${user.username.get} - """ - - sendMail( - From(MongoAuth.systemFancyEmail), - Subject("PYC: Identity Check"), - To(Emails.idVerification), - PlainMailBodyType(msgTxt) - ) - } -} -\ No newline at end of file diff --git a/src/main/scala/inc/pyc/rest/IdVerification.scala b/src/main/scala/inc/pyc/rest/IdVerification.scala @@ -0,0 +1,134 @@ +package inc.pyc +package rest + +import model._ +import config._ +import net.liftweb._ +import net.liftweb.http._ +import net.liftweb.util._ +import net.liftweb.http.rest._ +import net.liftweb.common._ +import net.liftweb.http.js.JsCmds._ +import net.liftmodules.mongoauth.MongoAuth +import inc.pyc.aws.s3._ +import net.liftweb.http.LiftRulesMocker.toLiftRules +import net.liftweb.util.Mailer.From +import net.liftweb.util.Mailer.PlainMailBodyType +import net.liftweb.util.Mailer.Subject +import net.liftweb.util.Mailer.To +import net.liftweb.util.Mailer.sendMail + +/** + * The identification files uploaded by a user will be stored here. + * Assure to clean up when done to save memory. + */ +object IdVerificationFiles extends SessionVar[List[FileParamHolder]](Nil) + +/** + * The REST point is used to load identification files to memory only. + * Manipulation should occur somewhere else (like snippet). + */ +object IdVerificationUpload extends RestHelper with Logger { + + def init(): Unit = { + LiftRules.dispatch.append(IdVerificationUpload) + } + + /** + * File can only be a maximum of 5 MB. + */ + def imgTooLarge(file: FileParamHolder) = file.length > 5243000 + + /** + * Only allow images of types: jpeg, png, svg + */ + def imgInvalid(file: FileParamHolder) = ! List( + "image/jpeg", "image/png", "image/svg+xml").exists(_ == file.mimeType) + + /** + * The maximum amount of files allowed in memory per user. + */ + def maxFilesInMemory = 3 + + /** + * Checks if another file can be added to memory or not. + */ + def tooManyFiles = IdVerificationFiles.get.size >= maxFilesInMemory + + /** + * API for uploading files to server. + */ + serve("settings" / "verification" prefix { + case "upload" :: Nil Post req => + for { + user <- User.currentUser ?~ "Must be logged in" ~> 400 + file <- Box(req.uploadedFiles) ?~ "File not found" + } yield { + if(imgInvalid(file)) + ResponseWithReason(BadResponse(), "Can only upload jpeg, png or svg file types.") + else if(imgTooLarge(file)) + ResponseWithReason(BadResponse(), "Image can only be 5MB or less.") + else if(tooManyFiles) + ResponseWithReason(BadResponse(), "Up to "+maxFilesInMemory+" files can be uploaded at once.") + else { + val thisreq = req + IdVerificationFiles.set(IdVerificationFiles.get :+ file) + OkResponse() + } + } + }) +} + +/** + * Other utility functions to help with the process of + * uploading identification files. + */ +object IdVerificationHelper { + + private val s3Obj = new S3( + Props.get("idverify.access.key", "NONE"), + Props.get("idverify.secret.key", "NONE"), + Props.get("idverify.bucket", "NONE")) + + /** + * Create an S3 object for uploading files. + */ + def s3(path: String = "") = s3Obj.copy(path = path) + + /** + * S3 folder formatting: /full_name/id/date/ + */ + def createS3Folder(user: User) = { + val prefix = if(Props.devMode) "dev/" else "" + + prefix + s"%s_%s/%s/%s/".format(user.fname.get, + user.lname.get, user.id.get, new java.util.Date().toString) + } + + /** + * Notifies us of a new user requesting their + * identification to be verified. + * @param thirdParty if the user is requesting verification with 3rd party + */ + def notifyIdentityRequest(user: User, thirdParty: Boolean = false): Unit = { + import net.liftweb.util.Mailer._ + + val msgTxt = + s""" + |${if(thirdParty) "3RD PARTY VERIFICATION" else ""} + |Identity Check Request! + |Full Name: ${user.fname} ${user.lname} + |Id: ${user.id.get} + |Limit: ${user.purchaseLimit.get} + |Email: ${user.email.get} + |Username: ${user.username.get} + """ + + sendMail( + From(MongoAuth.systemFancyEmail), + Subject("PYC: Identity Check"), + To(Emails.idVerification), + PlainMailBodyType(msgTxt) + ) + } +} +\ No newline at end of file diff --git a/src/main/scala/inc/pyc/rest/Sitemap.scala b/src/main/scala/inc/pyc/rest/Sitemap.scala @@ -0,0 +1,21 @@ +package inc.pyc +package rest + +import net.liftweb._ +import http._, rest._ + +/** + * http://en.wikipedia.org/wiki/Site_map + */ +object Sitemap extends RestHelper { + serve { + case Req("sitemap" :: Nil, _, GetRequest) => + XmlResponse( + S.render(<lift:embed what="sitemap" />, + S.request.openOrThrowException("No Request").request).head) + } + + def init(): Unit = { + LiftRules.statelessDispatch.append(Sitemap) + } +} +\ No newline at end of file diff --git a/src/main/scala/inc/pyc/snippet/Sitemap.scala b/src/main/scala/inc/pyc/snippet/Sitemap.scala @@ -11,15 +11,6 @@ import common._ import org.joda.time.DateTime -object Sitemap extends RestHelper { - serve { - case Req("sitemap" :: Nil, _, GetRequest) => - XmlResponse( - S.render(<lift:embed what="sitemap" />, - S.request.openOrThrowException("No Request").request).head) - } -} - class SitemapContent { case class Post(url: String, date: DateTime) diff --git a/src/main/scala/inc/pyc/snippet/UserSnip.scala b/src/main/scala/inc/pyc/snippet/UserSnip.scala @@ -2,6 +2,7 @@ package inc.pyc package snippet import lib._ +import rest._ import model._ import field._ import xml._ @@ -15,6 +16,7 @@ import JsonDSL._ import JsonAST.{JValue, JString, JBool} import net.liftmodules.mongoauth.model.ExtSession import net.liftmodules.extras.SnippetHelper +import inc.pyc.rest.IdVerificationFiles /** * Basic User information from a snippet.