Class: Jekyll::RemoteAsset::Generator
- Inherits:
-
Generator
- Object
- Generator
- Jekyll::RemoteAsset::Generator
- Defined in:
- lib/jekyll_remote_assets.rb
Constant Summary collapse
- REQUEST_TOKEN_URL =
"https://api.dropbox.com/1/oauth/request_token"- ACCESS_TOKEN_URL =
"https://api.dropbox.com/1/oauth/access_token"- FILES_PUT_URL =
"https://api-content.dropbox.com/1/files_put/auto"- FILE_METADATA_URL =
"https://api.dropbox.com/1/metadata/auto"- SHARES_URL =
"https://api.dropbox.com/1/shares/auto"
Instance Method Summary collapse
- #build_oauth1_header(app_key, app_secret, token = nil, token_secret = nil) ⇒ Object
- #config_oauth(plugin_config) ⇒ Object
- #generate(site) ⇒ Object
- #init_cache(plugin_config) ⇒ Object
- #nonce(size = 7) ⇒ Object
Instance Method Details
#build_oauth1_header(app_key, app_secret, token = nil, token_secret = nil) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/jekyll_remote_assets.rb', line 95 def build_oauth1_header(app_key, app_secret, token=nil, token_secret=nil) header_params = { oauth_version: "1.0", oauth_consumer_key: app_key, oauth_signature_method: "PLAINTEXT", oauth_token: token, oauth_signature: "#{ app_secret }&#{ token_secret }" } result = header_params.map do |k, v| "#{ k }=\"#{ v }\"" if v end return "OAuth #{ result.compact.join ', ' }" end |
#config_oauth(plugin_config) ⇒ Object
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 |
# File 'lib/jekyll_remote_assets.rb', line 36 def config_oauth(plugin_config) @oauth_config = {} config_file = plugin_config["config"] || __dir__ + "/.remote_assets_config" if not File.exist?(config_file) puts "1. Create a new application on the Dropbox App Console, if you haven't already: https://www.dropbox.com/developers/apps" $stdin.gets puts "2. Please enter your app key. " @oauth_config[:app_key] = $stdin.gets.strip puts "3. Please enter your app secret. " @oauth_config[:app_secret] = $stdin.gets.strip response = Unirest.post REQUEST_TOKEN_URL, headers: { "Authorization" => build_oauth1_header(@oauth_config[:app_key], @oauth_config[:app_secret]) } request_tokens = CGI::parse(response.body) puts "4. Visit https://www.dropbox.com/1/oauth/authorize?oauth_token=#{ request_tokens['oauth_token'][0] } and approve this app. Press enter when finished." $stdin.gets response = Unirest.post ACCESS_TOKEN_URL, headers: {"Authorization" => build_oauth1_header(@oauth_config[:app_key], @oauth_config[:app_secret], request_tokens['oauth_token'][0], request_tokens['oauth_token_secret'][0]) } access_tokens = CGI::parse(response.body) @oauth_config[:access_token] = access_tokens['oauth_token'][0] @oauth_config[:access_token_secret] = access_tokens['oauth_token_secret'][0] File.open(config_file, 'w+') do |f| YAML.dump(@oauth_config, f) end else File.open(config_file) do |f| @oauth_config = YAML.load_file(f) end end end |
#generate(site) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 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 162 163 164 165 166 167 |
# File 'lib/jekyll_remote_assets.rb', line 111 def generate(site) plugin_config = site.config["remote_assets"] || {} config_oauth(plugin_config) init_cache(plugin_config) file_set = Set.new Dir.glob("_assets/**/*") do |filename| begin next if File.directory?(filename) name = filename[filename.index('/')..-1] overwrite = plugin_config['overwrite'] || true File.open(filename) do |f| file_set.add filename md5 = Digest::MD5.file(filename).hexdigest if @cache[filename] and @cache[filename][:md5] == md5 # if it's cached, make sure the file is still in the Dropbox drive response = Unirest.get FILE_METADATA_URL + name, headers: {"Authorization" => build_oauth1_header(@oauth_config[:app_key], @oauth_config[:app_secret], @oauth_config[:access_token], @oauth_config[:access_token_secret])} if response.body["error"] == nil && !response.body["is_deleted"] site.remote_assets[name[1..name.length]] = @cache[filename][:url] next end end # upload the file response = Unirest.put FILES_PUT_URL + name + "?overwrite=#{ overwrite }", headers: {"Authorization" => build_oauth1_header(@oauth_config[:app_key], @oauth_config[:app_secret], @oauth_config[:access_token], @oauth_config[:access_token_secret]), "Content-Length" => File::size(f), "Content-Type" => "text/plain"}, parameters: f # retrieve the url response = Unirest.post SHARES_URL + name, headers: {"Authorization" => build_oauth1_header(@oauth_config[:app_key], @oauth_config[:app_secret], @oauth_config[:access_token], @oauth_config[:access_token_secret])}, parameters: {short_url: false} uri = URI(response.body["url"]) site.remote_assets[name[1..-1]] = "http://dl.dropboxusercontent.com#{ uri.path }" @cache[filename] = { md5: md5, url: "http://dl.dropboxusercontent.com#{ uri.path }"} end rescue puts "Error uploading #{ filename }" end end @cache.delete_if { |key, value| not file_set.include? key } # TODO: clean up file saving File.open(plugin_config["cache"] || __dir__ + "/.remote_assets_cache", 'w+') do |f| YAML.dump(@cache, f) end end |
#init_cache(plugin_config) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/jekyll_remote_assets.rb', line 75 def init_cache(plugin_config) @cache = {} cache_file = plugin_config["cache"] || __dir__ + "/.remote_assets_cache" # TODO: fix first-time set up of cache if not File.exist?(cache_file) File.open(cache_file, 'w+') do |f| YAML.dump(@cache, f) end end File.open(cache_file) do |f| @cache = YAML.load_file(f) end end |
#nonce(size = 7) ⇒ Object
91 92 93 |
# File 'lib/jekyll_remote_assets.rb', line 91 def nonce(size = 7) Base64.encode64(OpenSSL::Random.random_bytes(size)).gsub(/\W/, '') end |