Class: Appydave::Tools::Dam::ShareOperations

Inherits:
Object
  • Object
show all
Defined in:
lib/appydave/tools/dam/share_operations.rb

Overview

Generate shareable pre-signed URLs for S3 files

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(brand, project, brand_info: nil, brand_path: nil, s3_client: nil) ⇒ ShareOperations

Returns a new instance of ShareOperations.



13
14
15
16
17
18
19
20
21
# File 'lib/appydave/tools/dam/share_operations.rb', line 13

def initialize(brand, project, brand_info: nil, brand_path: nil, s3_client: nil)
  @project = project

  # Use injected dependencies or load from configuration
  @brand_info = brand_info || load_brand_info(brand)
  @brand = @brand_info.key # Use resolved brand key, not original input
  @brand_path = brand_path || Config.brand_path(@brand)
  @s3_client_override = s3_client # Store override but don't create client yet (lazy loading)
end

Instance Attribute Details

#brandObject (readonly)

Returns the value of attribute brand.



11
12
13
# File 'lib/appydave/tools/dam/share_operations.rb', line 11

def brand
  @brand
end

#brand_infoObject (readonly)

Returns the value of attribute brand_info.



11
12
13
# File 'lib/appydave/tools/dam/share_operations.rb', line 11

def brand_info
  @brand_info
end

#brand_pathObject (readonly)

Returns the value of attribute brand_path.



11
12
13
# File 'lib/appydave/tools/dam/share_operations.rb', line 11

def brand_path
  @brand_path
end

#projectObject (readonly)

Returns the value of attribute project.



11
12
13
# File 'lib/appydave/tools/dam/share_operations.rb', line 11

def project
  @project
end

Instance Method Details

#build_s3_key(file) ⇒ Object



105
106
107
108
# File 'lib/appydave/tools/dam/share_operations.rb', line 105

def build_s3_key(file)
  # S3 key format: staging/v-brand/project/file
  "staging/v-#{brand}/#{project}/#{file}"
end

#copy_to_clipboard(urls) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/appydave/tools/dam/share_operations.rb', line 219

def copy_to_clipboard(urls)
  # Copy all URLs to clipboard (newline-separated)
  text = urls.map { |item| item[:url] }.join("\n")

  Clipboard.copy(text)
  puts ''
  puts '📋 Copied to clipboard!'
rescue StandardError => e
  puts ''
  puts "⚠️  Could not copy to clipboard: #{e.message}"
  puts '   (URLs shown above for manual copy)'
end

#detect_content_type(filename) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/appydave/tools/dam/share_operations.rb', line 139

def detect_content_type(filename)
  ext = File.extname(filename).downcase
  case ext
  when '.mp4'
    'video/mp4'
  when '.mov'
    'video/quicktime'
  when '.avi'
    'video/x-msvideo'
  when '.mkv'
    'video/x-matroska'
  when '.webm'
    'video/webm'
  when '.m4v'
    'video/x-m4v'
  when '.jpg', '.jpeg'
    'image/jpeg'
  when '.png'
    'image/png'
  when '.gif'
    'image/gif'
  when '.pdf'
    'application/pdf'
  when '.json'
    'application/json'
  when '.srt', '.vtt', '.txt', '.md'
    'text/plain'
  else
    'application/octet-stream'
  end
end

#file_exists_in_s3?(s3_key) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
# File 'lib/appydave/tools/dam/share_operations.rb', line 110

def file_exists_in_s3?(s3_key)
  s3_client.head_object(bucket: brand_info.aws.s3_bucket, key: s3_key)
  true
rescue Aws::S3::Errors::NotFound
  false
end

#format_time_remaining(expiry_time) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
# File 'lib/appydave/tools/dam/share_operations.rb', line 207

def format_time_remaining(expiry_time)
  seconds = expiry_time - Time.now
  days = (seconds / 86_400).floor
  hours = ((seconds % 86_400) / 3600).floor

  if days.positive?
    "in #{days} day#{'s' if days > 1}"
  else
    "in #{hours} hour#{'s' if hours > 1}"
  end
end

Generate shareable link for file(s)

Parameters:

  • files (String, Array<String>)

    File name(s) to share

  • expires (String) (defaults to: '7d')

    Expiry time (e.g., ‘7d’, ‘24h’)

  • download (Boolean) (defaults to: false)

    Force download vs inline viewing (default: false for inline)

Returns:

  • (Hash)

    Result with :success, :urls, :expiry keys



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
# File 'lib/appydave/tools/dam/share_operations.rb', line 33

def generate_links(files:, expires: '7d', download: false)
  expires_in = parse_expiry(expires)
  expiry_time = Time.now + expires_in

  file_list = Array(files)
  urls = []

  file_list.each do |file|
    s3_key = build_s3_key(file)

    # Check if file exists in S3
    unless file_exists_in_s3?(s3_key)
      puts "⚠️  File not found in S3: #{file}"
      puts "   Upload first with: dam s3-up #{brand} #{project}"
      next
    end

    url = generate_presigned_url(s3_key, expires_in, download: download)
    urls << { file: file, url: url }
  end

  return { success: false, error: 'No files found in S3' } if urls.empty?

  # Show output
  show_results(urls, expiry_time, download: download)

  # Copy to clipboard
  copy_to_clipboard(urls)

  { success: true, urls: urls, expiry: expiry_time }
end

#generate_presigned_url(s3_key, expires_in_seconds, download: false) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/appydave/tools/dam/share_operations.rb', line 117

def generate_presigned_url(s3_key, expires_in_seconds, download: false)
  presigner = Aws::S3::Presigner.new(client: s3_client)

  # Extract just the filename
  filename = File.basename(s3_key)

  # Use 'attachment' to force download, 'inline' to view in browser
  disposition = download ? 'attachment' : 'inline'

  # Detect MIME type from file extension
  content_type = detect_content_type(filename)

  presigner.presigned_url(
    :get_object,
    bucket: brand_info.aws.s3_bucket,
    key: s3_key,
    expires_in: expires_in_seconds,
    response_content_disposition: "#{disposition}; filename=\"#{filename}\"",
    response_content_type: content_type
  )
end

#parse_expiry(expiry_string) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/appydave/tools/dam/share_operations.rb', line 171

def parse_expiry(expiry_string)
  case expiry_string
  when /^(\d+)h$/
    hours = ::Regexp.last_match(1).to_i
    raise ArgumentError, 'Expiry must be at least 1 hour' if hours < 1
    raise ArgumentError, 'Expiry cannot exceed 168 hours (7 days)' if hours > 168

    hours * 3600
  when /^(\d+)d$/
    days = ::Regexp.last_match(1).to_i
    raise ArgumentError, 'Expiry must be at least 1 day' if days < 1
    raise ArgumentError, 'Expiry cannot exceed 7 days' if days > 7

    days * 86_400
  else
    raise ArgumentError, "Invalid expiry format. Use: 24h, 7d, etc. (got: #{expiry_string})"
  end
end

#s3_clientObject

Lazy-load S3 client (only create when actually needed)



24
25
26
# File 'lib/appydave/tools/dam/share_operations.rb', line 24

def s3_client
  @s3_client ||= @s3_client_override || create_s3_client(@brand_info)
end

#show_results(urls, expiry_time, download: false) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/appydave/tools/dam/share_operations.rb', line 190

def show_results(urls, expiry_time, download: false)
  puts ''
  mode = download ? '📤 Shareable Link(s) - Download Mode' : '🎬 Shareable Link(s) - View in Browser'
  puts mode
  puts ''

  urls.each do |item|
    puts "📄 #{item[:file]}"
    puts "   #{item[:url]}"
    puts ''
  end

  expiry_date = expiry_time.strftime('%Y-%m-%d %H:%M:%S %Z')
  puts "⏰ Expires: #{expiry_date}"
  puts "   (#{format_time_remaining(expiry_time)})"
end