File: grid_fs_operations.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (114 lines) | stat: -rw-r--r-- 3,019 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# frozen_string_literal: true
# rubocop:todo all

module Unified

  module GridFsOperations

    def gridfs_find(op)
      bucket = entities.get(:bucket, op.use!('object'))
      use_arguments(op) do |args|
        filter = args.use!('filter')

        opts = extract_options(args, 'allowDiskUse',
          'skip', 'hint','timeoutMS',
          'noCursorTimeout', 'sort', 'limit')

        bucket.find(filter,opts).to_a
      end
    end

    def delete(op)
      bucket = entities.get(:bucket, op.use!('object'))
      use_arguments(op) do |args|
        opts = {}
        if timeout_ms = args.use('timeoutMS')
          opts[:timeout_ms] = timeout_ms
        end
        bucket.delete(args.use!('id'), opts)
      end
    end

    def download(op)
      bucket = entities.get(:bucket, op.use!('object'))
      use_arguments(op) do |args|
        opts = {}
        if timeout_ms = args.use('timeoutMS')
          opts[:timeout_ms] = timeout_ms
        end
        stream = bucket.open_download_stream(args.use!('id'), opts)
        stream.read
      end
    end

    def download_by_name(op)
      bucket = entities.get(:bucket, op.use!('object'))
      use_arguments(op) do |args|
        opts = {}
        if revision = args.use('revision')
          opts[:revision] = revision
        end
        stream = bucket.open_download_stream_by_name(args.use!('filename'), opts)
        stream.read
      end
    end

    def upload(op)
      bucket = entities.get(:bucket, op.use!('object'))
      use_arguments(op) do |args|
        opts = {}
        if chunk_size = args.use('chunkSizeBytes')
          opts[:chunk_size] = chunk_size
        end
        if metadata = args.use('metadata')
          opts[:metadata] = metadata
        end
        if content_type = args.use('contentType')
          opts[:content_type] = content_type
        end
        if disable_md5 = args.use('disableMD5')
          opts[:disable_md5] = disable_md5
        end
        if timeout_ms = args.use('timeoutMS')
          opts[:timeout_ms] = timeout_ms
        end
        contents = transform_contents(args.use!('source'))
        file_id = nil
        bucket.open_upload_stream(args.use!('filename'), **opts) do |stream|
          stream.write(contents)
          file_id = stream.file_id
        end
        file_id
      end
    end

    def drop(op)
      bucket = entities.get(:bucket, op.use!('object'))
      use_arguments(op) do |args|
        opts = {}
        if timeout_ms = args.use('timeoutMS')
          opts[:timeout_ms] = timeout_ms
        end
        bucket.drop(opts)
      end
    end

    private

    def transform_contents(contents)
      if Hash === contents
        if contents.length != 1
          raise NotImplementedError, "Wanted hash with one element"
        end
        if contents.keys.first != '$$hexBytes'
          raise NotImplementedError, "$$hexBytes is the only key supported"
        end

        decode_hex_bytes(contents.values.first)
      else
        contents
      end
    end

  end
end