spaceship/lib/spaceship/connect_api/models/app_preview.rb in fastlane-2.150.0.rc3 vs spaceship/lib/spaceship/connect_api/models/app_preview.rb in fastlane-2.150.0.rc4
- old
+ new
@@ -1,8 +1,11 @@
require_relative '../model'
require_relative '../file_uploader'
+require 'spaceship/globals'
+require 'digest/md5'
+
module Spaceship
class ConnectAPI
class AppPreview
include Spaceship::ConnectAPI::Model
@@ -32,42 +35,91 @@
def self.type
return "appPreviews"
end
+ def complete?
+ (asset_delivery_state || {})["state"] == "COMPLETE"
+ end
+
#
# API
#
- def self.create(app_preview_set_id: nil, path: nil)
+ def self.get(app_preview_id: nil)
+ Spaceship::ConnectAPI.get_app_preview(app_preview_id: app_preview_id).first
+ end
+
+ # Creates an AppPreview in an AppPreviewSet
+ # Setting the optional frame_time_code will force polling until video is done processing
+ # @param app_preview_set_id The AppPreviewSet id
+ # @param path The path of the file
+ # @param frame_time_code The time code for the preview still frame (ex: "00:00:07:01")
+ def self.create(app_preview_set_id: nil, path: nil, frame_time_code: nil)
require 'faraday'
filename = File.basename(path)
filesize = File.size(path)
- payload = File.binread(path)
+ bytes = File.binread(path)
post_attributes = {
fileSize: filesize,
fileName: filename
}
- post_resp = Spaceship::ConnectAPI.post_app_preview(
+ # Create placeholder
+ preview = Spaceship::ConnectAPI.post_app_preview(
app_preview_set_id: app_preview_set_id,
attributes: post_attributes
).to_models.first
- upload_operation = post_resp.upload_operations.first
- Spaceship::ConnectAPI::FileUploader.upload(upload_operation, payload)
+ # Upload the file
+ upload_operations = preview.upload_operations
+ Spaceship::ConnectAPI::FileUploader.upload(upload_operations, bytes)
+ # Update file uploading complete
patch_attributes = {
uploaded: true,
- sourceFileChecksum: "checksum-holder"
+ sourceFileChecksum: Digest::MD5.hexdigest(bytes)
}
- Spaceship::ConnectAPI.patch_app_preview(
- app_preview_id: post_resp.id,
- attributes: patch_attributes
- ).to_models.first
+ begin
+ preview = Spaceship::ConnectAPI.patch_app_preview(
+ app_preview_id: preview.id,
+ attributes: patch_attributes
+ ).to_models.first
+ rescue => error
+ puts("Failed to patch app preview. Update may have gone through so verifying") if Spaceship::Globals.verbose?
+
+ preview = Spaceship::ConnectAPI::AppPreview.get(app_preview_id: preview.id)
+ raise error unless preview.complete?
+ end
+
+ # Poll for video processing completion to set still frame time
+ unless frame_time_code.nil?
+ loop do
+ unless preview.video_url.nil?
+ puts("Preview processing complete!") if Spaceship::Globals.verbose?
+ preview = preview.update(attributes: {
+ previewFrameTimeCode: frame_time_code
+ })
+ puts("Updated preview frame time code!") if Spaceship::Globals.verbose?
+ break
+ end
+
+ sleep_time = 30
+ puts("Waiting #{sleep_time} seconds before checking status of processing...") if Spaceship::Globals.verbose?
+ sleep(sleep_time)
+
+ preview = Spaceship::ConnectAPI::AppPreview.get(app_preview_id: preview.id)
+ end
+ end
+
+ preview
+ end
+
+ def update(attributes: nil)
+ Spaceship::ConnectAPI.patch_app_preview(app_preview_id: id, attributes: attributes)
end
def delete!(filter: {}, includes: nil, limit: nil, sort: nil)
Spaceship::ConnectAPI.delete_app_preview(app_preview_id: id)
end