arql 0.3.29 → 0.3.31

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,161 @@
1
+ * OSS 数据下载和查看
2
+
3
+ 有的系统使用云存储服务来存储系统中的文件数据,并在数据库中存储文件在 OSS 上的 Key。我们希望可以在 Arql 中直接下载和查看这些文件。
4
+
5
+ ** 首先需要安装和配置 =rclone= 工具
6
+
7
+ macOS 示例:
8
+
9
+ #+BEGIN_EXAMPLE
10
+ brew install rclone
11
+ #+END_EXAMPLE
12
+
13
+
14
+ ** 配置 rclone
15
+
16
+ 在 =~/.config/rclone/rclone.conf= 文件中添加系统所使用的 OSS 配置,例如:
17
+
18
+ #+BEGIN_EXAMPLE
19
+ [my_system]
20
+ type = s3
21
+ provider = Alibaba
22
+ env_auth = false
23
+ access_key_id = LTAxuiwqsayuuyea
24
+ secret_access_key = sauiqwYUwqhjsdayuwehjkwehjwehj
25
+ endpoint = oss-cn-beijing.aliyuncs.com
26
+ acl = private
27
+ storage_class = STANDARD
28
+ #+END_EXAMPLE
29
+
30
+
31
+ ** 在 =~/.arql.d/init.yaml= 中添加配置
32
+
33
+ #+BEGIN_SRC yaml
34
+ dev:
35
+ created_at: ["create_time", "gmt_created"]
36
+ updated_at: ["update_time", "gmt_modified"]
37
+ host: db.dev.com
38
+ port: 3306
39
+ username: admin
40
+ password: 123456
41
+ database: dev_db
42
+ rclone_name: "my_system"
43
+ oss_bucket: "my_system_dev_bucket"
44
+
45
+ prod:
46
+ created_at: ["create_time", "gmt_created"]
47
+ updated_at: ["update_time", "gmt_modified"]
48
+ host: db.prod.com
49
+ port: 3306
50
+ username: admin
51
+ password: 123456
52
+ database: prod_db
53
+ rclone_name: "my_system"
54
+ oss_bucket: "my_system_prod_bucket"
55
+ #+END_SRC
56
+
57
+
58
+ ** 创建一个文件 =~/.arql.d/oss.rb=
59
+
60
+ #+BEGIN_SRC ruby
61
+ class OSS
62
+ def pry_source_location; end
63
+
64
+ attr_accessor :path, :bucket
65
+
66
+ def initialize(path, bucket: nil)
67
+ @path = path
68
+ unless bucket
69
+ bucket = Arql::App.config['oss_bucket']
70
+ end
71
+ @bucket = bucket
72
+ end
73
+
74
+ def rclone_name
75
+ Arql::App.config['rclone_name']
76
+ end
77
+
78
+ def info
79
+ `rclone lsl #{rclone_name}:#{bucket}/#{path}`
80
+ end
81
+
82
+ def download(dir = nil, keep_structure = false)
83
+ dir = File.expand_path(dir) if dir
84
+ dir ||= Dir.mktmpdir('arql-attachment-')
85
+ if keep_structure
86
+ dest_dir = dir + '/' + File.dirname(path)
87
+ FileUtils.mkdir_p(dest_dir)
88
+ system <<~EOF
89
+ rclone copy #{rclone_name}:#{bucket}/#{path} #{dest_dir}/
90
+ EOF
91
+ dest_dir + '/' + File.basename(path)
92
+ else
93
+ system <<~EOF
94
+ rclone copy #{rclone_name}:#{bucket}/#{path} #{dir}/
95
+ EOF
96
+ dir + '/' + File.basename(path)
97
+ end
98
+ end
99
+
100
+ def copy(dest_name, dest_bucket)
101
+ path_prefix = File.dirname(path)
102
+ system "rclone copy -P #{rclone_name}:#{bucket}/#{path} #{dest_name}:#{dest_bucket}/#{path_prefix}/"
103
+ end
104
+
105
+ def cat
106
+ `rclone cat #{rclone_name}:#{bucket}/#{path}`
107
+ end
108
+
109
+ def to_rclone_url
110
+ "#{rclone_name}:#{bucket}/#{path}"
111
+ end
112
+
113
+ def open
114
+ file = download
115
+ system <<~EOF
116
+ open #{file}
117
+ EOF
118
+ file
119
+ end
120
+
121
+ def preview
122
+ file = download
123
+ system <<~EOF
124
+ qlmanage -p #{file} &> /dev/null
125
+ EOF
126
+ file
127
+ end
128
+
129
+ def emacs_open
130
+ file = download
131
+ system <<~EOF
132
+ emacsclient -q --eval "(switch-to-buffer-other-window (current-buffer))" &> /dev/null
133
+ emacsclient -n '#{file}'
134
+ EOF
135
+ file
136
+ end
137
+ end
138
+
139
+ class String
140
+ def oss(bucket: nil)
141
+ OSS.new(self, bucket: bucket)
142
+ end
143
+ end
144
+ #+END_SRC
145
+
146
+ ** 在 =~/.arql.d/init.rb= 中引入这个文件
147
+
148
+ #+BEGIN_SRC ruby
149
+ load(File.absolute_path(File.dirname(__FILE__) + "/oss.rb"))
150
+ #+END_SRC
151
+
152
+ ** 用法
153
+
154
+ 假设 user 表中有一个 avatar 字段存储了用户头像在 OSS 上的 Key,我们可以这样查看和下载头像:
155
+
156
+ #+BEGIN_SRC ruby
157
+ User.first.avatar.oss.preview # 使用 macOS Quick Look 预览
158
+ User.first.avatar.oss.download # 下载到临时目录,并返回文件路径
159
+ User.first.avatar.oss.open # 下载到临时目录,并使用系统(macOS)默认程序打开
160
+ User.first.avatar.oss.cat # 直接输出文件内容
161
+ #+END_SRC
data/oss-files.org ADDED
@@ -0,0 +1,163 @@
1
+ * Download and Preview Files in OSS
2
+
3
+ Some systems use cloud storage services to store file data in the system and store the key of the file on OSS in the database. We hope to be able to download and view these files directly in Arql.
4
+
5
+ ** First you need to install and configure =rclone= tool
6
+
7
+ macOS example:
8
+
9
+ #+BEGIN_EXAMPLE
10
+ brew install rclone
11
+ #+END_EXAMPLE
12
+
13
+
14
+ ** Configure rclone
15
+
16
+ Add the OSS configuration used by the system to the =~/.config/rclone/rclone.conf= file, for example:
17
+
18
+ #+BEGIN_EXAMPLE
19
+ [my_system]
20
+ type = s3
21
+ provider = Alibaba
22
+ env_auth = false
23
+ access_key_id = LTAxuiwqsayuuyea
24
+ secret_access_key = sauiqwYUwqhjsdayuwehjkwehjwehj
25
+ endpoint = oss-cn-beijing.aliyuncs.com
26
+ acl = private
27
+ storage_class = STANDARD
28
+ #+END_EXAMPLE
29
+
30
+
31
+ ** Add configuration in =~/.arql.d/init.yaml=
32
+
33
+ #+BEGIN_SRC yaml
34
+ dev:
35
+ created_at: ["create_time", "gmt_created"]
36
+ updated_at: ["update_time", "gmt_modified"]
37
+ host: db.dev.com
38
+ port: 3306
39
+ username: admin
40
+ password: 123456
41
+ database: dev_db
42
+ rclone_name: "my_system"
43
+ oss_bucket: "my_system_dev_bucket"
44
+
45
+ prod:
46
+ created_at: ["create_time", "gmt_created"]
47
+ updated_at: ["update_time", "gmt_modified"]
48
+ host: db.prod.com
49
+ port: 3306
50
+ username: admin
51
+ password: 123456
52
+ database: prod_db
53
+ rclone_name: "my_system"
54
+ oss_bucket: "my_system_prod_bucket"
55
+ #+END_SRC
56
+
57
+
58
+ ** Create a file =~/.arql.d/oss.rb=
59
+
60
+ #+BEGIN_SRC ruby
61
+ class OSS
62
+ def pry_source_location; end
63
+
64
+ attr_accessor :path, :bucket
65
+
66
+ def initialize(path, bucket: nil)
67
+ @path = path
68
+ unless bucket
69
+ bucket = Arql::App.config['oss_bucket']
70
+ end
71
+ @bucket = bucket
72
+ end
73
+
74
+ def rclone_name
75
+ Arql::App.config['rclone_name']
76
+ end
77
+
78
+ def info
79
+ `rclone lsl #{rclone_name}:#{bucket}/#{path}`
80
+ end
81
+
82
+ def download(dir = nil, keep_structure = false)
83
+ dir = File.expand_path(dir) if dir
84
+ dir ||= Dir.mktmpdir('arql-attachment-')
85
+ if keep_structure
86
+ dest_dir = dir + '/' + File.dirname(path)
87
+ FileUtils.mkdir_p(dest_dir)
88
+ system <<~EOF
89
+ rclone copy #{rclone_name}:#{bucket}/#{path} #{dest_dir}/
90
+ EOF
91
+ dest_dir + '/' + File.basename(path)
92
+ else
93
+ system <<~EOF
94
+ rclone copy #{rclone_name}:#{bucket}/#{path} #{dir}/
95
+ EOF
96
+ dir + '/' + File.basename(path)
97
+ end
98
+ end
99
+
100
+ def copy(dest_name, dest_bucket)
101
+ path_prefix = File.dirname(path)
102
+ system "rclone copy -P #{rclone_name}:#{bucket}/#{path} #{dest_name}:#{dest_bucket}/#{path_prefix}/"
103
+ end
104
+
105
+ def cat
106
+ `rclone cat #{rclone_name}:#{bucket}/#{path}`
107
+ end
108
+
109
+ def to_rclone_url
110
+ "#{rclone_name}:#{bucket}/#{path}"
111
+ end
112
+
113
+ def open
114
+ file = download
115
+ system <<~EOF
116
+ open #{file}
117
+ EOF
118
+ file
119
+ end
120
+
121
+ def preview
122
+ file = download
123
+ system <<~EOF
124
+ qlmanage -p #{file} &> /dev/null
125
+ EOF
126
+ file
127
+ end
128
+
129
+ def emacs_open
130
+ file = download
131
+ system <<~EOF
132
+ emacsclient -q --eval "(switch-to-buffer-other-window (current-buffer))" &> /dev/null
133
+ emacsclient -n '#{file}'
134
+ EOF
135
+ file
136
+ end
137
+ end
138
+
139
+ class String
140
+ def oss(bucket: nil)
141
+ OSS.new(self, bucket: bucket)
142
+ end
143
+ end
144
+ #+END_SRC
145
+
146
+
147
+ ** Import this file in =~/.arql.d/init.rb=
148
+
149
+ #+BEGIN_SRC ruby
150
+ load(File.absolute_path(File.dirname(__FILE__) + "/oss.rb"))
151
+ #+END_SRC
152
+
153
+ ** Usage
154
+
155
+
156
+ Assuming that the user table has an avatar field that stores the key of the user's avatar on OSS, we can view and download the avatar like this:
157
+
158
+ #+BEGIN_SRC ruby
159
+ User.first.avatar.oss.preview # Preview using macOS Quick Look
160
+ User.first.avatar.oss.download # Download to a temporary directory and return the file path
161
+ User.first.avatar.oss.open # Download to a temporary directory and open with the system (macOS) default program
162
+ User.first.avatar.oss.cat # Output file content directly
163
+ #+END_SRC