Class: Appydave::Tools::Dam::ShareOperations
- Inherits:
-
Object
- Object
- Appydave::Tools::Dam::ShareOperations
- Defined in:
- lib/appydave/tools/dam/share_operations.rb
Overview
Generate shareable pre-signed URLs for S3 files
Instance Attribute Summary collapse
-
#brand ⇒ Object
readonly
Returns the value of attribute brand.
-
#brand_info ⇒ Object
readonly
Returns the value of attribute brand_info.
-
#brand_path ⇒ Object
readonly
Returns the value of attribute brand_path.
-
#project ⇒ Object
readonly
Returns the value of attribute project.
Instance Method Summary collapse
- #build_s3_key(file) ⇒ Object
- #copy_to_clipboard(urls) ⇒ Object
- #detect_content_type(filename) ⇒ Object
- #file_exists_in_s3?(s3_key) ⇒ Boolean
- #format_time_remaining(expiry_time) ⇒ Object
-
#generate_links(files:, expires: '7d', download: false) ⇒ Hash
Generate shareable link for file(s).
- #generate_presigned_url(s3_key, expires_in_seconds, download: false) ⇒ Object
-
#initialize(brand, project, brand_info: nil, brand_path: nil, s3_client: nil) ⇒ ShareOperations
constructor
A new instance of ShareOperations.
- #parse_expiry(expiry_string) ⇒ Object
-
#s3_client ⇒ Object
Lazy-load S3 client (only create when actually needed).
- #show_results(urls, expiry_time, download: false) ⇒ Object
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
#brand ⇒ Object (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_info ⇒ Object (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_path ⇒ Object (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 |
#project ⇒ Object (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
97 98 99 100 |
# File 'lib/appydave/tools/dam/share_operations.rb', line 97 def build_s3_key(file) # S3 key format: staging/v-brand/project/file "staging/v-#{brand}/#{project}/#{file}" end |
#copy_to_clipboard(urls) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/appydave/tools/dam/share_operations.rb', line 211 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.}" puts ' (URLs shown above for manual copy)' end |
#detect_content_type(filename) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/appydave/tools/dam/share_operations.rb', line 131 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
102 103 104 105 106 107 |
# File 'lib/appydave/tools/dam/share_operations.rb', line 102 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
199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/appydave/tools/dam/share_operations.rb', line 199 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_links(files:, expires: '7d', download: false) ⇒ Hash
Generate shareable link for file(s)
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
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/appydave/tools/dam/share_operations.rb', line 109 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
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/appydave/tools/dam/share_operations.rb', line 163 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_client ⇒ Object
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
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/appydave/tools/dam/share_operations.rb', line 182 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 |