Merge pull request #1541 from kreuzwerker/permanent-dropbox-links

Permanent dropbox links

Dominik Sander vor 8 Jahren
Ursprung
Commit
47715c3349
2 geänderte Dateien mit 81 neuen Zeilen und 34 gelöschten Zeilen
  1. 20 4
      app/models/agents/dropbox_file_url_agent.rb
  2. 61 30
      spec/models/agents/dropbox_file_url_agent_spec.rb

+ 20 - 4
app/models/agents/dropbox_file_url_agent.rb

@@ -6,7 +6,7 @@ module Agents
6 6
     no_bulk_receive!
7 7
 
8 8
     description <<-MD
9
-      The Dropbox File Url Agent is used to work with Dropbox. It takes a file path (or multiple file paths) and emits events with [temporary links](https://www.dropbox.com/developers/core/docs#media).
9
+      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).
10 10
 
11 11
       #{'## 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?}
12 12
 
@@ -28,6 +28,8 @@ module Agents
28 28
 
29 29
       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.
30 30
 
31
+      Set `link_type` to `'temporary'` if you want temporary links, or to `'permanent'` for permanent ones.
32
+
31 33
     MD
32 34
 
33 35
     event_description <<-MD
@@ -39,21 +41,35 @@ module Agents
39 41
           }
40 42
     MD
41 43
 
44
+    def default_options
45
+      {
46
+        'link_type' => 'temporary'
47
+      }
48
+    end
49
+
42 50
     def working?
43 51
       !recent_error_logs?
44 52
     end
45 53
 
46 54
     def receive(events)
47
-      events.map { |e| e.payload['paths'].split(',').map(&:strip) }
48
-        .flatten.each { |path| create_event payload: url_for(path) }
55
+      events.flat_map { |e| e.payload['paths'].split(',').map(&:strip) }
56
+        .each do |path|
57
+          create_event payload: (options['link_type'] == 'permanent' ? permanent_url_for(path) : temporary_url_for(path))
58
+        end
49 59
     end
50 60
 
51 61
     private
52 62
 
53
-    def url_for(path)
63
+    def temporary_url_for(path)
54 64
       dropbox.find(path).direct_url
55 65
     end
56 66
 
67
+    def permanent_url_for(path)
68
+      result = dropbox.find(path).share_url({ :short_url => false })
69
+      result.url = result.url.gsub('?dl=0','?dl=1') # cause the url to point to the file, instead of to a preview page for the file
70
+      result
71
+    end
72
+
57 73
   end
58 74
 
59 75
 end

+ 61 - 30
spec/models/agents/dropbox_file_url_agent_spec.rb

@@ -24,51 +24,82 @@ describe Agents::DropboxFileUrlAgent do
24 24
   end
25 25
 
26 26
   describe "#receive" do
27
-
28
-    let(:first_dropbox_url_payload)  { { 'url' => 'http://dropbox.com/first/path/url' } }
29
-    let(:second_dropbox_url_payload) { { 'url' => 'http://dropbox.com/second/path/url' } }
30
-    let(:third_dropbox_url_payload)  { { 'url' => 'http://dropbox.com/third/path/url' } }
31
-
32
-    def create_event(payload)
27
+    def event(payload)
33 28
       event = Event.new(payload: payload)
34 29
       event.agent = agents(:bob_manual_event_agent)
35
-      event.save!
36 30
       event
37 31
     end
38 32
 
39
-    before(:each) do
40
-      stub.proxy(Dropbox::API::Client).new do |api|
41
-        stub(api).find('/first/path')  { stub(Dropbox::API::File.new).direct_url { first_dropbox_url_payload } }
42
-        stub(api).find('/second/path') { stub(Dropbox::API::File.new).direct_url { second_dropbox_url_payload } }
43
-        stub(api).find('/third/path')  { stub(Dropbox::API::File.new).direct_url { third_dropbox_url_payload } }
33
+    context 'with temporaty urls' do
34
+      let(:first_dropbox_url_payload)  { { 'url' => 'http://dropbox.com/first/path/url' } }
35
+      let(:second_dropbox_url_payload) { { 'url' => 'http://dropbox.com/second/path/url' } }
36
+      let(:third_dropbox_url_payload)  { { 'url' => 'http://dropbox.com/third/path/url' } }
37
+
38
+      before(:each) do
39
+        stub.proxy(Dropbox::API::Client).new do |api|
40
+          stub(api).find('/first/path')  { stub(Dropbox::API::File.new).direct_url { first_dropbox_url_payload } }
41
+          stub(api).find('/second/path') { stub(Dropbox::API::File.new).direct_url { second_dropbox_url_payload } }
42
+          stub(api).find('/third/path')  { stub(Dropbox::API::File.new).direct_url { third_dropbox_url_payload } }
43
+        end
44 44
       end
45
-    end
46
-
47
-    context 'with a single path' do
48 45
 
49
-      before(:each) { @event = create_event(paths: '/first/path') }
46
+      context 'with a single path' do
47
+        before(:each) { @event = event(paths: '/first/path') }
50 48
 
51
-      it 'creates one event with the temporary dropbox link' do
52
-        expect { @agent.receive([@event]) }.to change(Event, :count).by(1)
53
-        expect(Event.last.payload).to eq(first_dropbox_url_payload)
49
+        it 'creates one event with the temporary dropbox link' do
50
+          expect { @agent.receive([@event]) }.to change(Event, :count).by(1)
51
+          expect(Event.last.payload).to eq(first_dropbox_url_payload)
52
+        end
54 53
       end
55 54
 
55
+      context 'with multiple comma-separated paths' do
56
+        before(:each) { @event = event(paths: '/first/path, /second/path, /third/path') }
57
+
58
+        it 'creates one event with the temporary dropbox link for each path' do
59
+          expect { @agent.receive([@event]) }.to change(Event, :count).by(3)
60
+          last_events = Event.last(3)
61
+          expect(last_events[0].payload).to eq(first_dropbox_url_payload)
62
+          expect(last_events[1].payload).to eq(second_dropbox_url_payload)
63
+          expect(last_events[2].payload).to eq(third_dropbox_url_payload)
64
+        end
65
+      end
56 66
     end
57 67
 
58
-    context 'with multiple comma-separated paths' do
68
+    context 'with permanent urls' do
69
+      def response_for(url)
70
+        Dropbox::API::Object.new(
71
+          url:  "https://www.dropbox.com/s/#{url}?dl=0",
72
+          expires: "Tue, 01 Jan 2030 00:00:00 +0000",
73
+          visibility: "PUBLIC"
74
+        )
75
+      end
59 76
 
60
-      before(:each) { @event = create_event(paths: '/first/path, /second/path, /third/path') }
77
+      let(:first_dropbox_url_payload)  { response_for('/first/path') }
78
+      let(:second_dropbox_url_payload) { response_for('/second/path') }
79
+      let(:third_dropbox_url_payload)  { response_for('/third/path') }
80
+
81
+      before(:each) do
82
+        stub.proxy(Dropbox::API::Client).new do |api|
83
+          stub(api).find('/first/path')  { stub(Dropbox::API::File.new).share_url { first_dropbox_url_payload } }
84
+          stub(api).find('/second/path') { stub(Dropbox::API::File.new).share_url { second_dropbox_url_payload } }
85
+          stub(api).find('/third/path')  { stub(Dropbox::API::File.new).share_url { third_dropbox_url_payload } }
86
+        end
87
+        @agent.options['link_type'] = 'permanent'
88
+      end
61 89
 
62
-      it 'creates one event with the temporary dropbox link for each path' do
63
-        expect { @agent.receive([@event]) }.to change(Event, :count).by(3)
64
-        last_events = Event.last(3)
65
-        expect(last_events[0].payload).to eq(first_dropbox_url_payload)
66
-        expect(last_events[1].payload).to eq(second_dropbox_url_payload)
67
-        expect(last_events[2].payload).to eq(third_dropbox_url_payload)
90
+      it 'creates one event with a single path' do
91
+        expect { @agent.receive([event(paths: '/first/path')]) }.to change(Event, :count).by(1)
92
+        expect(Event.last.payload).to eq(first_dropbox_url_payload.to_h)
68 93
       end
69 94
 
95
+      it 'creates one event with the permanent dropbox link for each path' do
96
+        event = event(paths: '/first/path, /second/path, /third/path')
97
+        expect { @agent.receive([event]) }.to change(Event, :count).by(3)
98
+        last_events = Event.last(3)
99
+        expect(last_events[0].payload).to eq(first_dropbox_url_payload.to_h)
100
+        expect(last_events[1].payload).to eq(second_dropbox_url_payload.to_h)
101
+        expect(last_events[2].payload).to eq(third_dropbox_url_payload.to_h)
102
+      end
70 103
     end
71
-
72 104
   end
73
-
74
-end
105
+end