3

我正在开发一个 CMS,我希望用户能够上传他们自己的图像、CSS 文件等。为了资源的安全,我不想将上传的文件存储在应用程序结构/部署的 WAR 中。

从非 grails 位置的控制器提供 grails 内容的最简单方法是什么?

4

3 回答 3

1

After much research and questioning, I decided to package the static resources (such as images) in the database. The advantages of this are:

  1. You don't need to package your images within your application (where a redeploy could cause you to lose them all)
  2. You don't need to store your images in another location on the server, away from your application.
  3. You don't need to work with a 3rd party service such as Amazon S3.
  4. Writing a simple controller to serve files from the database is relatively simple.

The disadvantages are:

  1. You cannot access/manipulate the files on the filesystem.
  2. The database grows and can become very slow.
  3. You need to make a database call every time someone requests an image.

I decided to go with the solution, even though there are these disadvantages. This is how I dealt with them:

  1. This isn't a problem as it's a 100% online system.
  2. My images are small and there aren't many of them, so this isn't a problem.
  3. Made use of a efficient and easy caching mechanisms. See below:

I'm using Smart Browser Caching by making use of etags (documentation) and using EHCache (documentation) to cache the images server side. As soon as a new image is uploaded, the e-tag is changed, and the cache is cleared, forcing the browser to download a fresh copy.

So far, the loss of performance from the MySQL database has been un-noticable, and performance is lightning fast.

于 2012-11-19T16:13:36.703 回答
1

通过适当的工具提供静态内容是正确的方法,所有的 Web 服务器都提供了实现这一点的可能性。如果您想在 Grails 应用程序中实现对这些内容的管理,请在 Config.groovy 中配置静态内容的目的地,仅此而已。

不要通过控制器实现从文件系统加载内容,你是在重新发明轮子;-) 而且 Web 服务器的性能要高得多;-)

于 2012-10-20T07:59:45.757 回答
1

我将添加一个指向远程文件系统文件夹的上下文别名,正如 cdavis226 在他的帖子中所解释的那样:

http://web.archive.org/web/20190906142211/http://grails.1312388.n4.nabble.com/How-to-configure-context-xml-aliases-for-Tomcat-7-td4632149.html

如果您使用的是 Grails 默认 Tomcat 服务器,则此解决方案有效。

于 2013-01-22T17:34:44.380 回答