Database Reference
In-Depth Information
def
def upload_new_photo (
input_file , parent_id , slug , title , author , tags , details ):
fs = GridFS ( db , 'cms.assets' )
now = datetime . utcnow ()
with
with fs . new_file (
content_type = 'image/jpeg' ,
metadata = dict (
nonce = bson . ObjectId (),
type = 'photo' ,
locked = now ,
parent_id = parent_id ,
slug = slug ,
title = title ,
created = now ,
author = author ,
tags = tags ,
detail = detail )) aas upload_file :
while
while True :
chunk = input_file . read ( upload_file . chunk_size )
iif not
break
upload_file . write ( chunk )
# unlock the file
db . assets . files . update (
{ '_id' : upload_file . _id },
{ '$set' : { 'locked' : None } } )
not chunk : break
Thoughmostofourphotoinformationgoesintothe metadata field,theMIMEcon-
tent type is one of the supported fields at the “top level” of GridFS files.
When creating a photo, we set a locked value to indicate when the upload started.
This helps us detect stalled uploads and conflicting updates later.
Since we're storing files in GridFS in chunks of chunk_size , we read them from
the client using the same buffer size.
Finally, we unlock the record, signifying that the upload is completed.
Becauseuploadingthephotospansmultipledocumentsandisanonatomicoperation,we
“lock”thefileduringuploadbywritingthecurrent datetime intherecord.Thefollowing
code shows how the locked field is used to manage updates to the photo content:
 
 
 
 
 
Search WWH ::




Custom Search