sutty-migration 0.4.0rc1 → 0.4.0rc2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7e6f379b5ad8bdb17dbe4353ac4436f244cbcbee4db468070df6da38befde458
|
|
4
|
+
data.tar.gz: 26e38ae35fdef4c9d702b959ed24ea51c1cf41d457a89c4914a7757a5a942289
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 73cf557ebcf3747e6878e56fb045586573d8b6b2883717fc092a4a85572c3e93361234877deaf4f5aa44af4ae47ac3b6b2c007a707b3cbb57db03afa0af5234b
|
|
7
|
+
data.tar.gz: 8182bf944bee9bdf1afb61517a700aaa87e99b45a2a19f6c405d1840ca1adc5eb866fca462b94cf96af97d7cad227f88e4a259f6c50a7737d6926af437a9fa4a
|
|
@@ -37,6 +37,8 @@ Jekyll::Hooks.register :site, :post_read, priority: :high do |site|
|
|
|
37
37
|
Jekyll::Hooks.trigger :wordpress_attachment, :post_write, attachment
|
|
38
38
|
end
|
|
39
39
|
|
|
40
|
+
Jekyll::Hooks.trigger :migration, :pre_render, wordpress
|
|
41
|
+
|
|
40
42
|
wordpress.posts.each_value do |post|
|
|
41
43
|
Jekyll::Hooks.trigger :wordpress_post, :post_init, post.item
|
|
42
44
|
Jekyll::Hooks.trigger :wordpress_post, :pre_render, post
|
|
@@ -10,6 +10,16 @@ module SuttyMigration
|
|
|
10
10
|
class WordpressXml
|
|
11
11
|
# Represents an attachment or uploaded file.
|
|
12
12
|
class Attachment < Post
|
|
13
|
+
@@connection_failed = false
|
|
14
|
+
|
|
15
|
+
def self.connection_failed
|
|
16
|
+
@@connection_failed
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def self.connection_failed=(value)
|
|
20
|
+
@@connection_failed = value
|
|
21
|
+
end
|
|
22
|
+
|
|
13
23
|
# File URL
|
|
14
24
|
#
|
|
15
25
|
# @return [String]
|
|
@@ -41,6 +51,8 @@ module SuttyMigration
|
|
|
41
51
|
end
|
|
42
52
|
end
|
|
43
53
|
|
|
54
|
+
attr_reader :head
|
|
55
|
+
|
|
44
56
|
# Download the file if it doesn't exist. Optionally show a
|
|
45
57
|
# progress bar.
|
|
46
58
|
#
|
|
@@ -48,30 +60,40 @@ module SuttyMigration
|
|
|
48
60
|
# @return [Boolean]
|
|
49
61
|
def download(progress: true)
|
|
50
62
|
return true if File.exist? dest
|
|
63
|
+
return false if self.class.connection_failed
|
|
51
64
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
65
|
+
@head =
|
|
66
|
+
begin
|
|
67
|
+
Faraday.head(normalized_url)
|
|
68
|
+
rescue Faraday::ConnectionFailed, Faraday::SSLError, Faraday::ServerError => e
|
|
69
|
+
::Jekyll.logger.warn 'Error:', e.message
|
|
70
|
+
self.class.connection_failed = true
|
|
71
|
+
return false
|
|
72
|
+
rescue Faraday::Error => e
|
|
73
|
+
::Jekyll.logger.warn 'Error:', e.message
|
|
59
74
|
return false
|
|
60
75
|
end
|
|
61
76
|
|
|
62
|
-
|
|
77
|
+
if head.status > 399
|
|
78
|
+
::Jekyll.logger.warn 'Error:', "#{head.status} #{normalized_url}"
|
|
79
|
+
return false
|
|
80
|
+
end
|
|
63
81
|
|
|
64
|
-
|
|
65
|
-
::Jekyll.logger.warn 'Error:', "Content length is zero for #{normalized_url}"
|
|
66
|
-
return false
|
|
67
|
-
end
|
|
82
|
+
content_length = head.headers['content-length'].to_i
|
|
68
83
|
|
|
69
|
-
|
|
84
|
+
if content_length.zero?
|
|
85
|
+
::Jekyll.logger.warn 'Error:', "Content length is zero for #{normalized_url}"
|
|
86
|
+
return false
|
|
87
|
+
end
|
|
70
88
|
|
|
71
|
-
|
|
72
|
-
(ProgressBar.create(title: File.basename(dest), total: content_length, output: $stderr) if progress)
|
|
89
|
+
::Jekyll.logger.info 'Downloading:', dest
|
|
73
90
|
|
|
74
|
-
|
|
91
|
+
progress_bar =
|
|
92
|
+
(ProgressBar.create(title: File.basename(dest), total: content_length, output: $stderr) if progress)
|
|
93
|
+
|
|
94
|
+
begin
|
|
95
|
+
FileUtils.mkdir_p File.dirname(dest)
|
|
96
|
+
File.open(dest, 'w') do |f|
|
|
75
97
|
Faraday.get(normalized_url) do |req|
|
|
76
98
|
req.options.on_data = proc do |chunk, downloaded_bytes|
|
|
77
99
|
if downloaded_bytes > content_length
|
|
@@ -83,14 +105,15 @@ module SuttyMigration
|
|
|
83
105
|
progress_bar&.progress = downloaded_bytes
|
|
84
106
|
end
|
|
85
107
|
end
|
|
86
|
-
rescue Faraday::Error => e
|
|
87
|
-
::Jekyll.logger.warn 'Error:', e.message
|
|
88
108
|
end
|
|
89
|
-
|
|
90
|
-
|
|
109
|
+
rescue Faraday::Error => e
|
|
110
|
+
::Jekyll.logger.warn 'Error:', e.message
|
|
111
|
+
FileUtils.rm_f(File.dirname(dest))
|
|
91
112
|
end
|
|
92
113
|
|
|
93
|
-
|
|
114
|
+
progress_bar&.stop
|
|
115
|
+
|
|
116
|
+
File.exist?(dest)
|
|
94
117
|
end
|
|
95
118
|
|
|
96
119
|
# Local path following Sutty's conventions. Instead of producing
|
|
@@ -104,32 +127,31 @@ module SuttyMigration
|
|
|
104
127
|
return unless digest
|
|
105
128
|
|
|
106
129
|
@public_path ||= File.join('public', digest[..27], dest.split('/').last).tap do
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
130
|
+
# If there's a file with the same digest, we replace the
|
|
131
|
+
# public_path so we deduplicate them.
|
|
132
|
+
if (other = wordpress.attachments[digest]) == self
|
|
133
|
+
next if File.exist? _1
|
|
134
|
+
|
|
135
|
+
FileUtils.mkdir_p(File.dirname(_1))
|
|
136
|
+
FileUtils.ln(dest, _1)
|
|
137
|
+
else
|
|
138
|
+
_1.replace other.public_path
|
|
116
139
|
end
|
|
117
|
-
|
|
118
|
-
FileUtils.mkdir_p(File.dirname(_1))
|
|
119
|
-
FileUtils.ln(dest, _1)
|
|
120
140
|
end
|
|
121
141
|
end
|
|
122
142
|
|
|
123
|
-
# Download and produce a digest
|
|
124
|
-
#
|
|
143
|
+
# Download and produce a digest if the file can be downloaded.
|
|
144
|
+
# Index by digest too.
|
|
125
145
|
#
|
|
126
146
|
# @return [String]
|
|
127
147
|
def digest
|
|
128
148
|
@digest ||=
|
|
129
|
-
|
|
149
|
+
if download
|
|
130
150
|
require 'digest'
|
|
131
151
|
|
|
132
|
-
Digest::SHA2.file(dest).hexdigest
|
|
152
|
+
Digest::SHA2.file(dest).hexdigest.tap do |digest|
|
|
153
|
+
wordpress.attachments[digest] ||= self
|
|
154
|
+
end
|
|
133
155
|
end
|
|
134
156
|
end
|
|
135
157
|
end
|