Module: Zgrid

Defined in:
lib/class_methods.rb

Class Method Summary collapse

Class Method Details

.delete(gid) ⇒ Object



31
32
33
# File 'lib/class_methods.rb', line 31

def delete(gid)
  FileGrid.delete(gid)
end

.handle_tempmedias(article, action = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/class_methods.rb', line 54

def handle_tempmedias(article,action=nil)
    medias = action ? tempmedias(article) : tempmedias
    tempids = medias.map{|t| t["_id"]}
    unless medias.count.zero?
      usedids=temp_usedids(article,tempids)
      unusedids=tempids-usedids
      usedids.each do |usedid|
        DB["fs.files"].update({"_id" => usedid}, {"$unset" => {"metadata.temp" => true },"$set"=>{"metadata.caller_id"=>article.id.to_s,"metadata.embed"=>true}})    
      end
      unusedids.each do |unusedid|
        delete(unusedid)
      end
    end
end

.rm_caller_files(caller_id) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/class_methods.rb', line 69

def rm_caller_files(caller_id)
  cursor=DB["fs.files"].find("metadata.caller_id" => caller_id.to_s)
  if cursor
    ids=[]
    cursor.to_a.each {|h| ids<<h["_id"]}
    ids.each {|id| FileGrid.delete(id)}
  end
end

.save(filepath, filename = nil, content_type = nil, metadata = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/class_methods.rb', line 19

def save(filepath,filename=nil,content_type=nil,=nil)
  filehash={}
  file_id=FileGrid.put(File.open(filepath),:filename=>filename,:metadata=>)      
  thefile=FileGrid.get(file_id)
  filehash["grid_id"]=file_id
  filehash["content_type"]=content_type
  filehash["length"]=thefile.file_length
  filehash["filename"]=filename
  filehash["metadata"]=
  return filehash
end

.save_temp(temp) ⇒ Object



15
16
17
# File 'lib/class_methods.rb', line 15

def save_temp(temp)
 save(temp.path)
end

.save_upload_file(temp, opts = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/class_methods.rb', line 3

def save_upload_file(temp,opts={})
  filepath=temp.tempfile.path
  filename=temp.original_filename
  content_type=temp.content_type
  =opts[:metadata]
  if opts and (opts[:percent] or opts[:size] or opts[:width])
    Zbox::QmExt.resize(filepath,opts)
  end

  save(filepath,filename,content_type,)
end

.temp_usedids(article, tempids) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/class_methods.rb', line 43

def temp_usedids(article,tempids)
      embedids=[]
      tempids.each do |id|
         embedids<<id if article.body.include?(id.to_s)
      end 
      docs=DB["fs.files"].find("_id"=>{"$in"=>embedids})
      fns=docs.map{|doc| doc["filename"]}
      usedfiles=DB["fs.files"].find("filename"=>{"$in"=>fns},"$or"=>[{"metadata.temp" => true},{"metadata.caller_id"=>article.id.to_s,"metadata.embed"=>true}])
      usedfiles.map{|f| f["_id"]}
end

.tempmedias(article = nil) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/class_methods.rb', line 35

def tempmedias(article=nil)
  if article
    DB["fs.files"].find("$or"=>[{"metadata.temp"=>true},{"metadata.caller_id"=>article.id.to_s,"metadata.embed"=>true}])   
  else
    DB["fs.files"].find("metadata.temp"=>true)   
  end
end