dropbox_file_url_agent.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. module Agents
  2. class DropboxFileUrlAgent < Agent
  3. include DropboxConcern
  4. cannot_be_scheduled!
  5. no_bulk_receive!
  6. can_dry_run!
  7. description <<-MD
  8. The _DropboxFileUrlAgent_ is used to work with Dropbox. It takes a file path (or multiple files paths) and emits events with either [temporary links](https://www.dropbox.com/developers/core/docs#media) or [permanent links](https://www.dropbox.com/developers/core/docs#shares).
  9. #{'## Include the `dropbox-api` and `omniauth-dropbox` gems in your `Gemfile` and set `DROPBOX_OAUTH_KEY` and `DROPBOX_OAUTH_SECRET` in your environment to use Dropbox Agents.' if dependencies_missing?}
  10. The incoming event payload needs to have a `paths` key, with a comma-separated list of files you want the URL for. For example:
  11. {
  12. "paths": "first/path, second/path"
  13. }
  14. __TIP__: You can use the _Event Formatting Agent_ to format events before they come in. Here's an example configuration for formatting an event coming out of a _Dropbox Watch Agent_:
  15. {
  16. "instructions": {
  17. "paths": "{{ added | map: 'path' | join: ',' }}"
  18. },
  19. "matchers": [],
  20. "mode": "clean"
  21. }
  22. An example of usage would be to watch a specific Dropbox directory (with the _DropboxWatchAgent_) and get the URLs for the added or updated files. You could then, for example, send emails with those links.
  23. Set `link_type` to `'temporary'` if you want temporary links, or to `'permanent'` for permanent ones.
  24. MD
  25. event_description do
  26. "Events will looks like this:\n\n %s" % if options['link_type'] == 'permanent'
  27. Utils.pretty_print({
  28. url: "https://www.dropbox.com/s/abcde3/example?dl=1",
  29. :".tag" => "file",
  30. id: "id:abcde3",
  31. name: "hi",
  32. path_lower: "/huginn/hi",
  33. link_permissions: {
  34. resolved_visibility: {:".tag"=>"public"},
  35. requested_visibility: {:".tag"=>"public"},
  36. can_revoke: true
  37. },
  38. client_modified: "2017-10-14T18:38:39Z",
  39. server_modified: "2017-10-14T18:38:45Z",
  40. rev: "31db0615354b",
  41. size: 0
  42. })
  43. else
  44. Utils.pretty_print({
  45. url: "https://dl.dropboxusercontent.com/apitl/1/somelongurl",
  46. metadata: {
  47. name: "hi",
  48. path_lower: "/huginn/hi",
  49. path_display: "/huginn/hi",
  50. id: "id:abcde3",
  51. client_modified: "2017-10-14T18:38:39Z",
  52. server_modified: "2017-10-14T18:38:45Z",
  53. rev: "31db0615354b",
  54. size: 0,
  55. content_hash: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
  56. }
  57. })
  58. end
  59. end
  60. def default_options
  61. {
  62. 'link_type' => 'temporary'
  63. }
  64. end
  65. def working?
  66. !recent_error_logs?
  67. end
  68. def receive(events)
  69. events.flat_map { |e| e.payload['paths'].split(',').map(&:strip) }
  70. .each do |path|
  71. create_event payload: (options['link_type'] == 'permanent' ? permanent_url_for(path) : temporary_url_for(path))
  72. end
  73. end
  74. private
  75. def temporary_url_for(path)
  76. dropbox.find(path).direct_url.response.tap do |response|
  77. response['url'] = response.delete('link')
  78. end
  79. end
  80. def permanent_url_for(path)
  81. dropbox.find(path).share_url.response.tap do |response|
  82. response['url'].gsub!('?dl=0','?dl=1')
  83. end
  84. end
  85. end
  86. end