dropbox_file_url_agent.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 " +
  27. Utils.pretty_print(
  28. if options['link_type'] == 'permanent'
  29. {
  30. url: "https://www.dropbox.com/s/abcde3/example?dl=1",
  31. ".tag": "file",
  32. id: "id:abcde3",
  33. name: "hi",
  34. path_lower: "/huginn/hi",
  35. link_permissions: {
  36. resolved_visibility: { ".tag": "public" },
  37. requested_visibility: { ".tag": "public" },
  38. can_revoke: true
  39. },
  40. client_modified: "2017-10-14T18:38:39Z",
  41. server_modified: "2017-10-14T18:38:45Z",
  42. rev: "31db0615354b",
  43. size: 0
  44. }
  45. else
  46. {
  47. url: "https://dl.dropboxusercontent.com/apitl/1/somelongurl",
  48. metadata: {
  49. name: "hi",
  50. path_lower: "/huginn/hi",
  51. path_display: "/huginn/hi",
  52. id: "id:abcde3",
  53. client_modified: "2017-10-14T18:38:39Z",
  54. server_modified: "2017-10-14T18:38:45Z",
  55. rev: "31db0615354b",
  56. size: 0,
  57. content_hash: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
  58. }
  59. }
  60. end
  61. )
  62. end
  63. def default_options
  64. {
  65. 'link_type' => 'temporary'
  66. }
  67. end
  68. def working?
  69. !recent_error_logs?
  70. end
  71. def receive(events)
  72. events.flat_map { |e| e.payload['paths'].split(',').map(&:strip) }
  73. .each do |path|
  74. create_event payload: (options['link_type'] == 'permanent' ? permanent_url_for(path) : temporary_url_for(path))
  75. end
  76. end
  77. private
  78. def temporary_url_for(path)
  79. dropbox.find(path).direct_url.response.tap do |response|
  80. response['url'] = response.delete('link')
  81. end
  82. end
  83. def permanent_url_for(path)
  84. dropbox.find(path).share_url.response.tap do |response|
  85. response['url'].gsub!('?dl=0', '?dl=1')
  86. end
  87. end
  88. end
  89. end