PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mongodb/casbah
Scala | 195 lines | 104 code | 48 blank | 43 comment | 0 complexity | b992726240f9b4c36eb72314a85704e9 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 scala.beans.BeanInfo
  25. import scala.collection.JavaConverters._
  26. import scala.language.reflectiveCalls
  27. import com.github.nscala_time.time.Imports._
  28. import com.mongodb.gridfs.{ GridFS => MongoGridFS, GridFSDBFile => MongoGridFSDBFile, GridFSFile => MongoGridFSFile, GridFSInputFile => MongoGridFSInputFile }
  29. import com.mongodb.casbah.Imports._
  30. import com.mongodb.casbah.commons.Logging
  31. import scala.collection.mutable
  32. import java.io.InputStream
  33. import scala.io.BufferedSource
  34. abstract class GenericGridFS protected[gridfs] extends Logging {
  35. log.info("Instantiated a new GridFS instance against '%s'", underlying)
  36. val underlying: MongoGridFS
  37. implicit val db: MongoDB = underlying.getDB.asScala
  38. implicit val filesCollection: MongoGenericTypedCollection[GridFSDBFileSafeJoda] =
  39. db(underlying.getBucketName + ".files").setObjectClass(classOf[GridFSDBFileSafeJoda])
  40. /**
  41. * loan
  42. *
  43. * Basic implementation of the Loan pattern -
  44. * the idea is to pass a Unit returning function
  45. * and a Mongo file handle, and work on it within
  46. * a code block.
  47. *
  48. */
  49. protected[gridfs] def loan[T <: GenericGridFSFile](file: T)(op: T => Option[AnyRef]) = op(file)
  50. /** Find by query */
  51. def find[A <% DBObject](query: A): mutable.Buffer[MongoGridFSDBFile] = underlying.find(query).asScala
  52. /** Find by query - returns a single item */
  53. def find(id: ObjectId): MongoGridFSDBFile = underlying.find(id)
  54. /** Find by query */
  55. def find(filename: String): mutable.Buffer[MongoGridFSDBFile] = underlying.find(filename).asScala
  56. def bucketName: String = underlying.getBucketName
  57. /**
  58. * Returns a cursor for this filestore
  59. * of all of the files...
  60. */
  61. def files: MongoCursor = {
  62. new MongoCursor(underlying.getFileList)
  63. }
  64. def files[A <% DBObject](query: A): MongoCursor = {
  65. new MongoCursor(underlying.getFileList(query))
  66. }
  67. def remove[A <% DBObject](query: A): Unit = underlying.remove(query)
  68. def remove(id: ObjectId): Unit = underlying.remove(id)
  69. def remove(filename: String): Unit = underlying.remove(filename)
  70. }
  71. @BeanInfo
  72. abstract class GenericGridFSFile(override val underlying: MongoGridFSFile) extends MongoDBObject with Logging {
  73. type DateType
  74. def convertDate(in: AnyRef): DateType
  75. override def iterator: Iterator[(String, AnyRef)] = underlying.keySet.asScala.map {
  76. k => k -> underlying.get(k)
  77. }.toMap.iterator
  78. def save() {
  79. underlying.save()
  80. }
  81. /**
  82. * validate the object.
  83. * Throws an exception if it fails
  84. * @throws MongoException An error describing the validation failure
  85. */
  86. def validate() {
  87. underlying.validate()
  88. }
  89. def numChunks: Int = underlying.numChunks
  90. def id: AnyRef = underlying.getId
  91. def filename: Option[String] = Option(underlying.getFilename)
  92. // todo - does mongo support mime magic? Should we pull some in here?
  93. def contentType: Option[String] = Option(underlying.getContentType)
  94. def length: Long = underlying.getLength
  95. def chunkSize: Long = underlying.getChunkSize
  96. def uploadDate: DateType = convertDate(underlying.get("uploadDate"))
  97. def aliases: mutable.Buffer[String] = underlying.getAliases.asScala
  98. def metaData: DBObject = underlying.getMetaData
  99. def metaData_=[A <% DBObject](meta: A): Unit = underlying.setMetaData(meta)
  100. def md5: String = underlying.getMD5
  101. override def toString(): String = "{ GridFSFile(id=%s, filename=%s, contentType=%s) }".
  102. format(id, filename, contentType)
  103. }
  104. class GridFSDBFileSafeJoda extends MongoGridFSDBFile {
  105. override def setGridFS(fs: MongoGridFS) {
  106. super.setGridFS(fs)
  107. }
  108. override def put(key: String, v: AnyRef): AnyRef = {
  109. val _v = v match {
  110. case j: DateTime => j.toDate
  111. case l: LocalDateTime => l.toDateTime.toDate
  112. case default => default
  113. }
  114. super.put(key, _v)
  115. }
  116. }
  117. @BeanInfo
  118. abstract class GenericGridFSDBFile protected[gridfs] (override val underlying: MongoGridFSDBFile) extends GenericGridFSFile(underlying) {
  119. def inputStream: InputStream = underlying.getInputStream
  120. def source: BufferedSource = scala.io.Source.fromInputStream(inputStream)
  121. def writeTo(file: java.io.File): Long = underlying.writeTo(file)
  122. def writeTo(out: java.io.OutputStream): Long = underlying.writeTo(out)
  123. def writeTo(filename: String): Long = underlying.writeTo(filename)
  124. override def toString(): String = "{ GridFSDBFile(id=%s, filename=%s, contentType=%s) }".
  125. format(id, filename, contentType)
  126. override def put(key: String, v: AnyRef): Option[AnyRef] = {
  127. val _v = v match {
  128. case j: DateTime => j.toDate
  129. case l: LocalDateTime => l.toDateTime.toDate
  130. case default => default
  131. }
  132. super.put(key, _v)
  133. }
  134. }
  135. @BeanInfo
  136. abstract class GenericGridFSInputFile protected[gridfs] (override val underlying: MongoGridFSInputFile) extends GenericGridFSFile(underlying) {
  137. def filename_=(name: String): Unit = underlying.setFilename(name)
  138. def contentType_=(cT: String): Unit = underlying.setContentType(cT)
  139. override def put(key: String, v: AnyRef): Option[AnyRef] = {
  140. val _v = v match {
  141. case j: DateTime => j.toDate
  142. case l: LocalDateTime => l.toDateTime.toDate
  143. case default => default
  144. }
  145. super.put(key, _v)
  146. }
  147. }