PageRenderTime 25ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/casbah-gridfs/src/main/scala/GridFS.scala

http://github.com/mongodb/casbah
Scala | 247 lines | 108 code | 41 blank | 98 comment | 0 complexity | 1ea7fc5b814db5c4cbfe88919f982586 MD5 | raw file
Possible License(s): Apache-2.0
  1. /**
  2. * Copyright (c) 2010 MongoDB, Inc. <http://mongodb.com>
  3. * Copyright (c) 2009, 2010 Novus Partners, Inc. <http://novus.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. * For questions and comments about this product, please see the project page at:
  18. *
  19. * http://github.com/mongodb/casbah
  20. *
  21. */
  22. package com.mongodb.casbah
  23. package gridfs
  24. import java.io.{ File, InputStream }
  25. import scala.beans.BeanInfo
  26. import com.mongodb.gridfs.{
  27. GridFS => MongoGridFS,
  28. GridFSDBFile => MongoGridFSDBFile,
  29. GridFSFile => MongoGridFSFile,
  30. GridFSInputFile => MongoGridFSInputFile
  31. }
  32. import com.mongodb.casbah.Imports._
  33. import com.mongodb.casbah.gridfs.Imports._
  34. import com.mongodb.casbah.commons.Logging
  35. import com.github.nscala_time.time.Imports.{ DateTime, LocalDateTime }
  36. /**
  37. * Companion object for GridFS.
  38. * Entry point for creation of GridFS Instances.
  39. *
  40. * @since 1.0
  41. */
  42. object GridFS extends Logging {
  43. def apply(db: MongoDB): GridFS = {
  44. log.info("Creating a new GridFS Entry against DB '%s', using default bucket ('%s')", db.name, MongoGridFS.DEFAULT_BUCKET)
  45. new GridFS(new MongoGridFS(db.underlying))
  46. }
  47. def apply(db: MongoDB, bucket: String): GridFS = {
  48. log.info("Creating a new GridFS Entry against DB '%s', using specific bucket ('%s')", db.name, bucket)
  49. new GridFS(new MongoGridFS(db.underlying, bucket))
  50. }
  51. }
  52. class GridFS protected[gridfs] (override val underlying: MongoGridFS) extends GenericGridFS with Iterable[GridFSDBFile] {
  53. type FileWriteOp = GridFSInputFile => Unit
  54. def iterator: Iterator[GridFSDBFile] = new Iterator[GridFSDBFile] {
  55. val fileSet: MongoCursor = files
  56. def count(): Int = fileSet.count
  57. override def length: Int = fileSet.length
  58. def numSeen(): Int = fileSet.numSeen
  59. def curr: GridFSDBFile = next()
  60. def explain(): CursorExplanation = fileSet.explain
  61. @SuppressWarnings(Array("deprecation"))
  62. def next(): GridFSDBFile = {
  63. val gridfsfile = fileSet.next().asInstanceOf[GridFSDBFileSafeJoda]
  64. gridfsfile.setGridFS(underlying)
  65. new GridFSDBFile(gridfsfile)
  66. }
  67. def hasNext: Boolean = fileSet.hasNext
  68. }
  69. /**
  70. * Create a new GridFS File from a scala.io.Source
  71. *
  72. * Uses a loan pattern, so you need to pass a curried function which expects a GridFSInputFile
  73. * as a parameter.
  74. * It AUTOMATICALLY saves the GridFS file at it's end, so throw an exception if you want to fail.
  75. * If you don't want automatic saving/loaning please see the createFile method instead.
  76. * @see createFile
  77. * @return The ID of the created File (Option[AnyRef])
  78. */
  79. def apply(data: scala.io.Source)(op: FileWriteOp): Nothing = withNewFile(data)(op)
  80. /**
  81. * Create a new GridFS File from a Byte Array
  82. *
  83. * Uses a loan pattern, so you need to pass a curried function which expects a GridFSInputFile
  84. * as a parameter.
  85. * It AUTOMATICALLY saves the GridFS file at it's end, so throw an exception if you want to fail.
  86. * If you don't want automatic saving/loaning please see the createFile method instead.
  87. * @see createFile
  88. * @return The ID of the created File (Option[AnyRef])
  89. */
  90. def apply(data: Array[Byte])(op: FileWriteOp): Option[AnyRef] = withNewFile(data)(op)
  91. /**
  92. * Create a new GridFS File from a java.io.File
  93. *
  94. * Uses a loan pattern, so you need to pass a curried function which expects a GridFSInputFile
  95. * as a parameter.
  96. * It AUTOMATICALLY saves the GridFS file at it's end, so throw an exception if you want to fail.
  97. * If you don't want automatic saving/loaning please see the createFile method instead.
  98. * @see createFile
  99. * @return The ID of the created File (Option[AnyRef])
  100. */
  101. def apply(f: File)(op: FileWriteOp): Option[AnyRef] = withNewFile(f)(op)
  102. /**
  103. * Create a new GridFS File from a java.io.InputStream
  104. *
  105. * Uses a loan pattern, so you need to pass a curried function which expects a GridFSInputFile
  106. * as a parameter.
  107. * It AUTOMATICALLY saves the GridFS file at it's end, so throw an exception if you want to fail.
  108. * If you don't want automatic saving/loaning please see the createFile method instead.
  109. * @see createFile
  110. * @return The ID of the created File (Option[AnyRef])
  111. */
  112. def apply(in: InputStream)(op: FileWriteOp): Option[AnyRef] = withNewFile(in)(op)
  113. /**
  114. * Create a new GridFS File from a java.io.InputStream and a specific filename
  115. *
  116. * Uses a loan pattern, so you need to pass a curried function which expects a GridFSInputFile
  117. * as a parameter.
  118. * It AUTOMATICALLY saves the GridFS file at it's end, so throw an exception if you want to fail.
  119. * If you don't want automatic saving/loaning please see the createFile method instead.
  120. * @see createFile
  121. * @return The ID of the created File (Option[AnyRef])
  122. */
  123. def apply(in: InputStream, filename: String)(op: FileWriteOp): Option[AnyRef] = withNewFile(in, filename)(op)
  124. /**
  125. * createFile
  126. *
  127. * Creates a new file in GridFS
  128. */
  129. def createFile(data: scala.io.Source): GridFSInputFile = throw new UnsupportedOperationException("Currently no support for scala.io.Source")
  130. def createFile(data: Array[Byte]): GridFSInputFile = underlying.createFile(data)
  131. def createFile(f: File): GridFSInputFile = underlying.createFile(f)
  132. def createFile(in: InputStream): GridFSInputFile = underlying.createFile(in)
  133. def createFile(in: InputStream, filename: String): GridFSInputFile = underlying.createFile(in, filename)
  134. def withNewFile(data: scala.io.Source)(op: FileWriteOp): Nothing = throw new UnsupportedOperationException("Currently no support for scala.io.Source")
  135. /**
  136. * Loan pattern style file creation.
  137. * @return The ID of the created File (Option[AnyRef])
  138. */
  139. def withNewFile(data: Array[Byte])(op: FileWriteOp): Option[AnyRef] = loan(createFile(data)) {
  140. fh =>
  141. op(fh)
  142. fh.save()
  143. fh.validate()
  144. Option(fh.id)
  145. }
  146. /**
  147. * Loan pattern style file creation.
  148. * @return The ID of the created File (Option[AnyRef])
  149. */
  150. def withNewFile(f: File)(op: FileWriteOp): Option[AnyRef] = loan(createFile(f)) {
  151. fh =>
  152. op(fh)
  153. fh.save()
  154. fh.validate()
  155. Option(fh.id)
  156. }
  157. /**
  158. * Loan pattern style file creation.
  159. * @return The ID of the created File (Option[AnyRef])
  160. */
  161. def withNewFile(in: InputStream)(op: FileWriteOp): Option[AnyRef] = loan(createFile(in)) {
  162. fh =>
  163. op(fh)
  164. fh.save()
  165. fh.validate()
  166. Option(fh.id)
  167. }
  168. /**
  169. * Loan pattern style file creation.
  170. * @return The ID of the created File (Option[AnyRef])
  171. */
  172. def withNewFile(in: InputStream, filename: String)(op: FileWriteOp): Option[AnyRef] =
  173. loan(createFile(in, filename)) {
  174. fh =>
  175. op(fh)
  176. fh.save()
  177. fh.validate()
  178. Option(fh.id)
  179. }
  180. def findOne[A <% DBObject](query: A): Option[GridFSDBFile] = {
  181. filesCollection.findOne(query) match {
  182. case None => None
  183. case x => {
  184. val gridfsFile = x.get
  185. gridfsFile.setGridFS(underlying)
  186. Some(new GridFSDBFile(gridfsFile))
  187. }
  188. }
  189. }
  190. def findOne(id: ObjectId): Option[GridFSDBFile] = findOne(MongoDBObject("_id" -> id))
  191. def findOne(filename: String): Option[GridFSDBFile] = findOne(MongoDBObject("filename" -> filename))
  192. }
  193. @BeanInfo
  194. class GridFSFile(_underlying: MongoGridFSFile) extends GenericGridFSFile(_underlying) with ConvertToDate
  195. @BeanInfo
  196. class GridFSDBFile(_underlying: MongoGridFSDBFile) extends GenericGridFSDBFile(_underlying) with ConvertToDate
  197. @BeanInfo
  198. class GridFSInputFile(_underlying: MongoGridFSInputFile) extends GenericGridFSInputFile(_underlying) with ConvertToDate
  199. trait ConvertToDate {
  200. type DateType = java.util.Date
  201. def convertDate(in: AnyRef): DateType = in match {
  202. case d: java.util.Date => d
  203. case j: DateTime => j.toDate
  204. case l: LocalDateTime => l.toDateTime.toDate
  205. }
  206. }