@@ -19,6 +19,7 @@ defmodule Philomena.DevSeeds do
19
19
alias Philomena.Posts
20
20
alias Philomena.Tags
21
21
import Ecto.Query
22
+ require Logger
22
23
23
24
def seed ( ) do
24
25
{ :ok , _ } = Application . ensure_all_started ( :plug )
@@ -28,11 +29,6 @@ defmodule Philomena.DevSeeds do
28
29
|> File . read! ( )
29
30
|> Jason . decode! ( )
30
31
31
- images =
32
- "priv/repo/seeds/dev/images.json"
33
- |> File . read! ( )
34
- |> Jason . decode! ( )
35
-
36
32
# pages =
37
33
# "priv/repo/seeds/dev/pages.json"
38
34
# |> File.read!()
@@ -63,39 +59,7 @@ defmodule Philomena.DevSeeds do
63
59
64
60
IO . puts ( "---- Generating images" )
65
61
66
- for image_def <- images do
67
- file = Briefly . create! ( )
68
- now = DateTime . utc_now ( ) |> DateTime . to_unix ( :microsecond )
69
-
70
- IO . puts ( "Fetching #{ image_def [ "url" ] } ..." )
71
- { :ok , % { body: body } } = PhilomenaProxy.Http . get ( image_def [ "url" ] )
72
-
73
- File . write! ( file , body )
74
-
75
- upload = % Plug.Upload {
76
- path: file ,
77
- content_type: "application/octet-stream" ,
78
- filename: "fixtures-#{ now } "
79
- }
80
-
81
- IO . puts ( "Inserting ..." )
82
-
83
- Images . create_image (
84
- pleb_attrs ,
85
- Map . merge ( image_def , % { "image" => upload } )
86
- )
87
- |> case do
88
- { :ok , % { image: image } } ->
89
- Images . approve_image ( image )
90
- Images . reindex_image ( image )
91
- Tags . reindex_tags ( image . added_tags )
92
-
93
- IO . puts ( "Created image ##{ image . id } " )
94
-
95
- { :error , :image , changeset , _so_far } ->
96
- IO . inspect ( changeset . errors )
97
- end
98
- end
62
+ generate_images ( pleb_attrs )
99
63
100
64
IO . puts ( "---- Generating comments for image #1" )
101
65
@@ -157,6 +121,48 @@ defmodule Philomena.DevSeeds do
157
121
Logger . configure ( level: :debug )
158
122
end
159
123
124
+ defp generate_images ( pleb_attrs ) do
125
+ images =
126
+ "priv/repo/seeds/dev/images.json"
127
+ |> File . read! ( )
128
+ |> Jason . decode! ( )
129
+
130
+ ingest_image = fn image_def ->
131
+ file = Briefly . create! ( )
132
+ now = DateTime . utc_now ( ) |> DateTime . to_unix ( :microsecond )
133
+
134
+ IO . puts ( "Fetching #{ image_def [ "url" ] } ..." )
135
+ { :ok , % { body: body } } = PhilomenaProxy.Http . get ( image_def [ "url" ] )
136
+
137
+ File . write! ( file , body )
138
+
139
+ upload = % Plug.Upload {
140
+ path: file ,
141
+ content_type: "application/octet-stream" ,
142
+ filename: "fixtures-#{ now } "
143
+ }
144
+
145
+ IO . puts ( "Inserting ..." )
146
+
147
+ Images . create_image ( pleb_attrs , Map . merge ( image_def , % { "image" => upload } ) )
148
+ |> case do
149
+ { :ok , % { image: image } } ->
150
+ Images . approve_image ( image )
151
+ Images . reindex_image ( image )
152
+ Tags . reindex_tags ( image . added_tags )
153
+
154
+ IO . puts ( "Created image ##{ image . id } " )
155
+
156
+ { :error , :image , changeset , _so_far } ->
157
+ IO . inspect ( changeset . errors )
158
+ end
159
+ end
160
+
161
+ images
162
+ |> Task . async_stream ( ingest_image , max_concurrency: 20 , ordered: false )
163
+ |> Stream . run ( )
164
+ end
165
+
160
166
defp default_ip ( ) do
161
167
{ :ok , ip } = EctoNetwork.INET . cast ( { 203 , 0 , 113 , 0 } )
162
168
ip
0 commit comments