Skip to content

Commit

Permalink
Supporting external upload directory.
Browse files Browse the repository at this point in the history
  • Loading branch information
dozed committed Aug 2, 2012
1 parent 19ac188 commit 753b21c
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ target
src/main/resources/fink.properties
src/main/webapp/uploads

/.cache
11 changes: 3 additions & 8 deletions src/main/scala/fink/support/MediaManager.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ case class SquareImageSpec(override val name: String, width: Int) extends ImageS

object MediaManager {

var base = "./uploads"
def baseDirectory = base
def baseDirectory = Config.mediaDirectory

val specs = Array(
FullImageSpec("full"),
Expand All @@ -35,11 +34,6 @@ object MediaManager {
KeepRatioImageSpec("big", 700)
)

// check if the base upload folder exists in the filesystem
protected def sanitizeEnv = {
checkDirectory(baseDirectory)
}

protected def checkDirectory(dir: String) = {
val target = new File(dir)
if (!target.exists) target.mkdirs
Expand All @@ -49,7 +43,8 @@ object MediaManager {
protected def isImage(item: FileItem, name: String, ext: String): Boolean = true

def processUpload(item: FileItem): Option[String] = {
sanitizeEnv
checkDirectory(baseDirectory)

val m = Pattern.compile("(.*)\\.(.*)$").matcher(item.getName)
if (!m.matches) return None

Expand Down
49 changes: 25 additions & 24 deletions src/main/scala/fink/web/Admin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,39 @@ import org.scalatra.servlet._

import net.liftweb.json._
import net.liftweb.json.Serialization.{read, write}
import java.io.File

class Admin extends ScalatraServlet with RepositorySupport with AuthenticationRoutes with ResourceRoutes with ScalateSupport with FileUploadSupport {

override implicit val jsonFormats = Serialization.formats(ShortTypeHints(List(classOf[Page], classOf[Category], classOf[Tag]))) + FieldSerializer[Post]() + FieldSerializer[Gallery]() + FieldSerializer[Image]()
override implicit val jsonFormats = Serialization.formats(ShortTypeHints(List(classOf[Page], classOf[Category], classOf[Tag]))) + FieldSerializer[Post]() + FieldSerializer[Gallery]() + FieldSerializer[Image]()

def adminTemplateBase = "/WEB-INF/admin"
def adminTemplateBase = "/WEB-INF/admin"

def uri(uri: String) = {
if (uri.startsWith("/")) {
request.getContextPath + uri
} else {
uri
}
}
def uri(uri: String) = {
if (uri.startsWith("/")) {
request.getContextPath + uri
} else {
uri
}
}

override def init(config: ServletConfig) {
super.init(config)
MediaManager.base = config.getServletContext().getRealPath("/uploads")
}
before() {
contentType = "text/html"
}

before() {
contentType = "text/html"
}
get("/") {
if (request.getPathInfo == null) redirect("/admin/")
templateAttributes("layout") = ("/admin/layouts/default.jade")
jade("/admin/index.jade")
}

get("/") {
if (request.getPathInfo == null) redirect("/admin/")
templateAttributes("layout") = ("/admin/layouts/default.jade")
jade("/admin/index.jade")
}
get("/uploads/:file") {
val filename = "%s/%s".format(Config.mediaDirectory, params("file"))
new File(filename)
}

notFound {
halt(404, <h1>Not found. Bummer.</h1>)
}
notFound {
halt(404, <h1>Not found. Bummer.</h1>)
}

}
7 changes: 7 additions & 0 deletions src/main/scala/fink/web/Frontend.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.scalatra.ScalatraServlet
import org.scalatra.servlet._

import org.joda.time.format.DateTimeFormat
import java.io.File

class Frontend extends ScalatraServlet with ScalateSupport with RepositorySupport {

Expand Down Expand Up @@ -88,6 +89,12 @@ class Frontend extends ScalatraServlet with ScalateSupport with RepositorySuppor
}
}

get("/uploads/:file") {
println(1)
val filename = "%s/%s".format(Config.mediaDirectory, params("file"))
new File(filename)
}

notFound {
halt(404, "Not found.")
}
Expand Down
1 change: 0 additions & 1 deletion src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
<url-pattern>/images/*</url-pattern>
<url-pattern>/css/*</url-pattern>
<url-pattern>/js/*</url-pattern>
<url-pattern>/uploads/*</url-pattern>
<url-pattern>/assets/*</url-pattern>
<url-pattern>/themes/*</url-pattern>
<url-pattern>/admin/app/*</url-pattern>
Expand Down
9 changes: 5 additions & 4 deletions src/main/webapp/admin/js/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ function EditGalleryController($scope, $location, $routeParams, Gallery, Tag, Im
var self = this;
self.original = null;

$scope.fink_base = fink_base
$scope.tags = Tag.query();
$scope.galleries = Gallery.query();

Expand All @@ -122,7 +123,7 @@ function EditGalleryController($scope, $location, $routeParams, Gallery, Tag, Im
'multi' : true,
'method' : 'POST',
'debug' : false,
'uploader' : '/admin/api/images',
'uploader' : fink_base+'/admin/api/images',
'checkExisting' : false,
'fileObjName' : 'file',
'fileTypeExts' : '*.jpg;*.jpeg;*.gif;*.png',
Expand All @@ -138,7 +139,7 @@ function EditGalleryController($scope, $location, $routeParams, Gallery, Tag, Im
},
'onUploadSuccess' : function(file, data, response) {
var imageId = data;
$.post("/admin/api/galleries/"+galleryId+"/images/"+imageId, function(data, status) {
$.post(fink_base+"/admin/api/galleries/"+galleryId+"/images/"+imageId, function(data, status) {
console.log(data + " - " + status);
})
}
Expand All @@ -153,7 +154,7 @@ function EditGalleryController($scope, $location, $routeParams, Gallery, Tag, Im
// console.log("done");
// });
var order = _.map($("#albums-images").sortable('toArray'), function(id) { return id.split('-')[1]; }).join(',');
$.post("/admin/api/galleries/"+$scope.gallery.id+"/images", {order: order}, function() {
$.post("fink_base+/admin/api/galleries/"+$scope.gallery.id+"/images", {order: order}, function() {
console.log("done");
});
}
Expand All @@ -174,7 +175,7 @@ function EditGalleryController($scope, $location, $routeParams, Gallery, Tag, Im
}

$scope.removeImage = function(image) {
$.delete("/admin/api/galleries/"+$scope.gallery.id+"/images/"+image.id, function(data, status) {
$.delete(fink_base+"/admin/api/galleries/"+$scope.gallery.id+"/images/"+image.id, function(data, status) {
Gallery.get({galleryId: $routeParams.galleryId}, function(gallery) {
$scope.gallery = new Gallery(gallery);
})
Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/admin/partials/galleries-details.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<a ng-click="removeImage(image)"><i class="trash"></i></a>
</div>
<div>
<img src="/uploads/{{image.hash}}-medium.jpg" alt="{{image.title}}">
<img src="{{fink_base}}/admin/uploads/{{image.hash}}-medium.jpg" alt="{{image.title}}">
</div>
</div>
</div>
Expand Down

0 comments on commit 753b21c

Please sign in to comment.