Class: Workarea::DirectUpload

Inherits:
Object
  • Object
show all
Defined in:
app/services/workarea/direct_upload.rb,
app/services/workarea/direct_upload/asset.rb,
app/services/workarea/direct_upload/processor.rb,
app/services/workarea/direct_upload/product_image.rb

Defined Under Namespace

Modules: Processor Classes: Asset, InvalidTypeError, ProductImage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, filename) ⇒ DirectUpload

Returns a new instance of DirectUpload.

Raises:



31
32
33
34
35
36
# File 'app/services/workarea/direct_upload.rb', line 31

def initialize(type, filename)
  @type = type.to_s
  @filename = filename

  raise InvalidTypeError unless processor.present?
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



27
28
29
# File 'app/services/workarea/direct_upload.rb', line 27

def filename
  @filename
end

#typeObject (readonly)

Returns the value of attribute type.



27
28
29
# File 'app/services/workarea/direct_upload.rb', line 27

def type
  @type
end

Class Method Details

.ensure_cors!(request_url) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/services/workarea/direct_upload.rb', line 5

def self.ensure_cors!(request_url)
  uri = URI.parse(request_url)
  url = "#{uri.scheme}://#{uri.host}"
  url += ":#{uri.port}" unless uri.port.in? [80, 443]

  redis_key = "cors_#{url.optionize}"
  return if Workarea.redis.get(redis_key) == 'true'

  response = Workarea.s3.get_bucket_cors(Configuration::S3.bucket)
  cors = response.data[:body]
  cors['CORSConfiguration'] << {
    'ID' => "direct_upload_#{url}",
    'AllowedMethod' => 'PUT',
    'AllowedOrigin' => url,
    'AllowedHeader' => '*'
  }
  cors['CORSConfiguration'].uniq!

  Workarea.s3.put_bucket_cors(Configuration::S3.bucket, cors)
  Workarea.redis.set(redis_key, 'true')
end

Instance Method Details

#delete!Object



65
66
67
# File 'app/services/workarea/direct_upload.rb', line 65

def delete!
  Workarea.s3.delete_object(Configuration::S3.bucket, key)
end

#fileObject



58
59
60
61
62
63
# File 'app/services/workarea/direct_upload.rb', line 58

def file
  @file ||= begin
    bucket = Workarea.s3.directories.new(key: Configuration::S3.bucket)
    bucket.files.get(key)&.body
  end
end

#keyObject



38
39
40
# File 'app/services/workarea/direct_upload.rb', line 38

def key
  "#{type.underscore}_direct_upload/#{filename}"
end

#process!Object



53
54
55
56
# File 'app/services/workarea/direct_upload.rb', line 53

def process!
  processor.perform
  delete!
end

#upload_urlObject



42
43
44
45
46
47
48
49
50
51
# File 'app/services/workarea/direct_upload.rb', line 42

def upload_url
  if Configuration::S3.configured?
    Workarea.s3.put_object_url(Configuration::S3.bucket, key, time_to_access)
  else
    Admin::Engine
      .routes
      .url_helpers
      .upload_direct_uploads_path(type: type, filename: filename)
  end
end