cybertrain 0.2.0 → 0.3.0

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.
@@ -0,0 +1,1323 @@
1
+ # `cybertrain generate attachment MODEL FIELD [--any]`: a file attached to a
2
+ # model (`cover` on `Post`), as code the application owns (docs/design.md
3
+ # D15, D16). The framework supplies the parts that are easy to get wrong
4
+ # (the multipart parser, Cybertrain::UploadedFile, Cybertrain::BlobStore,
5
+ # Controller#send_blob); this writes the rest:
6
+ #
7
+ # db/migrate/*_add_cover_to_posts.rb five metadata columns
8
+ # test/attachment_post_cover.rb, test/support/app_test.rb
9
+ #
10
+ # and one-time edits, each guarded by a marker so that running the
11
+ # generator again changes nothing:
12
+ #
13
+ # app/models/post.rb constants, callbacks and cover_* methods
14
+ # app/controllers/posts_controller.rb `def cover` (the download, loaded by
15
+ # `before_action :set_post`), `cover_file=`
16
+ # in create and update
17
+ # config/routes.rb get "/posts/:id/cover"
18
+ # app/views/posts/_form.html.erb, show.html.erb (scaffold shape only)
19
+ # .gitignore /storage/uploads/
20
+ #
21
+ # The bytes are never in the database (SQLite binds text, and stops at a
22
+ # NUL): BlobStore writes them under storage/uploads/ and the model keeps a
23
+ # random key plus the server-detected type, size and SHA-256. Without
24
+ # --any only PNG, JPEG, GIF and WebP are accepted, and
25
+ # Controller#send_blob serves those inline and anything else as a download.
26
+ #
27
+ # `generate scaffold post title:string cover:attachment` ends in the same
28
+ # edits (Scaffold.generate calls install_code); it writes the columns in
29
+ # the create_table migration itself, so there is no second migration.
30
+ #
31
+ # Loaded by cybertrain/cli.rb together with scaffold.rb (whose Scaffold
32
+ # and InvalidArgument it uses) and authentication.rb (indent and
33
+ # insert_after_line).
34
+ require "cybertrain/generator/inflector"
35
+ require "cybertrain/ident"
36
+ require "cybertrain/cli/templates"
37
+ require "cybertrain/cli/scaffold"
38
+ require "cybertrain/cli/authentication"
39
+
40
+ module Cybertrain
41
+ module CLI
42
+ # The names one attachment is generated from: model `post`, field
43
+ # `cover` give Post / posts / PostsController, COVER_*, cover_key ...
44
+ class AttachmentSpec
45
+ attr_reader :singular, :plural, :class_name, :plural_class, :field, :any
46
+
47
+ # `model` is snake_case or CamelCase, singular or plural ("post",
48
+ # "Post" and "posts" all name Post).
49
+ def initialize(model, field, any)
50
+ @singular = Inflector.singularize(Inflector.underscore(model))
51
+ @plural = Inflector.pluralize(@singular)
52
+ @class_name = Inflector.camelize(@singular)
53
+ @plural_class = Inflector.camelize(@plural)
54
+ @field = field
55
+ @any = any
56
+ end
57
+
58
+ def upper
59
+ @field.upcase
60
+ end
61
+
62
+ def human_field
63
+ Templates.humanize(@field)
64
+ end
65
+
66
+ # `text` (a template of the generated files) with its <<placeholders>>
67
+ # filled in. The templates are quoted heredocs so that the interpolation
68
+ # of the generated code needs no escaping.
69
+ def expand(text)
70
+ out = text.gsub("<<Model>>", @class_name)
71
+ out = out.gsub("<<model>>", @singular)
72
+ out = out.gsub("<<plural>>", @plural)
73
+ out = out.gsub("<<Plural>>", @plural_class)
74
+ out = out.gsub("<<FIELD>>", upper)
75
+ out = out.gsub("<<Field>>", human_field)
76
+ out = out.gsub("<<field>>", @field)
77
+ out
78
+ end
79
+ end
80
+
81
+ module Attachment
82
+ MARK = "cybertrain:attachment"
83
+ BACKUP_NOTE = "note: uploaded files live in storage/uploads/; back it up (and mount it as a volume) together with the SQLite file"
84
+ IMAGE_TYPES_LITERAL = "[\"image/png\", \"image/jpeg\", \"image/gif\", \"image/webp\"]"
85
+
86
+ # Returns the paths it created. Raises InvalidArgument, before anything
87
+ # is written, when the field name is unusable or `root` is not an
88
+ # application with that model, its controller and a routes file.
89
+ def self.generate(root, model, field, any)
90
+ spec = AttachmentSpec.new(model, field, any)
91
+ check_names!(spec)
92
+
93
+ routes_path = File.join(root, "config/routes.rb")
94
+ raise InvalidArgument, "#{routes_path} not found: run this inside a cybertrain app" unless File.exist?(routes_path)
95
+ raise InvalidArgument, "#{routes_path} has no `#{Scaffold::DRAW_LINE}` line" if File.read(routes_path).index(Scaffold::DRAW_LINE).nil?
96
+
97
+ model_path = File.join(root, model_file(spec))
98
+ raise InvalidArgument, "#{model_path} not found: generate the model first (cybertrain generate scaffold #{spec.singular} ...)" unless File.exist?(model_path)
99
+ raise InvalidArgument, "#{model_path} has no `class #{spec.class_name}` line" if line_start(File.read(model_path), "class #{spec.class_name}", "class") < 0
100
+
101
+ controller_path = File.join(root, controller_file(spec))
102
+ raise InvalidArgument, "#{controller_path} not found: generate the controller first (cybertrain generate scaffold #{spec.singular} ...)" unless File.exist?(controller_path)
103
+ raise InvalidArgument, "#{controller_path} has no `#{controller_line(spec)}` line" if line_start(File.read(controller_path), controller_line(spec), "start") < 0
104
+
105
+ # Only the owner's table counts (products may have a cover when posts
106
+ # do), and a column this generator added (the model has its marker)
107
+ # is a re-run after `db migrate`, which reports identical.
108
+ schema_path = File.join(root, "db/schema.rb")
109
+ if File.exist?(schema_path) && schema_column?(File.read(schema_path), spec.plural, "#{field}_key") &&
110
+ File.read(model_path).index(model_marker(spec)).nil?
111
+ raise InvalidArgument, "#{schema_path} already has a \"#{field}_key\" column in \"#{spec.plural}\": #{spec.class_name} already has a #{field}, or pick another field name"
112
+ end
113
+ check_download_policy!(root, spec, File.read(controller_path))
114
+
115
+ created = Array.new(0) { "" }
116
+ record(created, root, migration_path(root, spec), migration(spec))
117
+ # The views first: the test written by install_code asserts on the
118
+ # show page only when it renders the file.
119
+ add_form_field(root, spec)
120
+ add_show_markup(root, spec)
121
+ install_code(root, spec, created)
122
+ created
123
+ end
124
+
125
+ # Everything but the migration and the views: the model block, the
126
+ # download action, the create/update assignments, the route, the
127
+ # .gitignore line and the test. Shared with the scaffold, which writes
128
+ # the columns and views itself. Appends the paths it created.
129
+ def self.install_code(root, spec, created)
130
+ add_model_block(root, spec)
131
+ add_action(root, spec)
132
+ add_public_download(root, spec)
133
+ add_assignments(root, spec)
134
+ add_route(root, spec)
135
+ add_gitignore(root)
136
+ record(created, root, "test/support/app_test.rb", Authentication.app_test_support)
137
+ record(created, root, test_file(spec), attachment_test(root, spec))
138
+ nil
139
+ end
140
+
141
+ # Check before writing anything, including when invoked by scaffold.
142
+ # Authentication alone cannot replace a show-only authorization check.
143
+ # Only the shared record callback is extended automatically; any other
144
+ # policy must explicitly cover both show and the download already.
145
+ def self.check_download_policy!(root, spec, source)
146
+ coverage = set_record_coverage(source, spec, spec.field)
147
+ new_action = line_start(source, "def #{spec.field}", "exact") < 0
148
+ source = add_to_only_list(source, spec, spec.field) if coverage == "add" && new_action
149
+ safe = line_start(source, controller_line(spec), "exact") >= 0 &&
150
+ set_record_coverage(source, spec, "show") == "yes" &&
151
+ set_record_coverage(source, spec, spec.field) == "yes" &&
152
+ !show_checks_more?(source, spec)
153
+
154
+ # Read the actual ancestry rather than inferring policy from the child.
155
+ parent = "ApplicationController"
156
+ seen = Array.new(0) { "" }
157
+ while safe && parent != "Cybertrain::Controller"
158
+ path = File.join(root, "app/controllers/#{Inflector.underscore(parent)}.rb")
159
+ if seen.include?(parent) || !File.file?(path)
160
+ safe = false
161
+ else
162
+ seen << parent
163
+ inherited = File.read(path)
164
+ head = "class #{parent} < "
165
+ at = line_start(inherited, head, "start")
166
+ safe = at >= 0 && !callback_checks_more?(inherited, spec)
167
+ parent = inherited[at, line_end(inherited, at) - at].to_s.strip.delete_prefix(head) if safe
168
+ end
169
+ end
170
+ return nil if safe
171
+
172
+ raise InvalidArgument, "#{controller_file(spec)}: cannot preserve show's authorization for :#{spec.field}; use the same explicit before_action policy for :show and :#{spec.field}, move checks out of def show, and load both through before_action :set_#{spec.singular} (including inherited policies); no attachment files were written"
173
+ end
174
+
175
+ def self.record(created, root, path, content)
176
+ created << path if Templates.write(root, path, content) == "create"
177
+ nil
178
+ end
179
+
180
+ # ---- names --------------------------------------------------------
181
+
182
+ def self.model_file(spec)
183
+ "app/models/#{spec.singular}.rb"
184
+ end
185
+
186
+ def self.controller_file(spec)
187
+ "app/controllers/#{spec.plural}_controller.rb"
188
+ end
189
+
190
+ def self.controller_line(spec)
191
+ "class #{spec.plural_class}Controller < ApplicationController"
192
+ end
193
+
194
+ def self.test_file(spec)
195
+ "test/attachment_#{spec.singular}_#{spec.field}.rb"
196
+ end
197
+
198
+ def self.route_line(spec)
199
+ "get \"/#{spec.plural}/:id/#{spec.field}\", to: \"#{spec.plural}##{spec.field}\", as: \"#{spec.singular}_#{spec.field}\""
200
+ end
201
+
202
+ # Whether db/schema.rb's `create_table "<table>"` block has a column
203
+ # `column` (the schema dumper writes `s.create_table "posts" do |t|`,
204
+ # one `t.<type> "<name>", ...` line per column and `end`).
205
+ def self.schema_column?(source, table, column)
206
+ head = "create_table \"#{table}\""
207
+ inside = false
208
+ found = false
209
+ source.each_line do |raw|
210
+ stripped = raw.strip
211
+ if stripped.start_with?(head) || stripped.start_with?("s.#{head}")
212
+ inside = true
213
+ elsif inside && stripped == "end"
214
+ inside = false
215
+ elsif inside && !stripped.index("\"#{column}\"").nil?
216
+ found = true
217
+ end
218
+ end
219
+ found
220
+ end
221
+
222
+ # The marker comment that opens the model block of this attachment.
223
+ def self.model_marker(spec)
224
+ "# #{MARK} #{spec.field} "
225
+ end
226
+
227
+ # The field must be a lowercase identifier, and neither it nor the
228
+ # five columns made from it may be a name the model generator would
229
+ # rename (Ident), nor a method the controller already has (the
230
+ # download action is `def <field>`; Templates.attachment_action_clash?).
231
+ def self.check_names!(spec)
232
+ field = spec.field
233
+ raise InvalidArgument, "bad model name '#{spec.singular}'" unless Templates.identifier?(spec.singular)
234
+ raise InvalidArgument, "bad field name '#{field}' (lowercase letters, digits and _, starting with a letter)" unless field_name?(field)
235
+ if Templates.attachment_action_clash?(field, spec.singular)
236
+ raise InvalidArgument, "'#{field}' would redefine a method of #{spec.plural_class}Controller (the download action is `def #{field}`); pick another field name"
237
+ end
238
+
239
+ Scaffold.refuse_unusable!(field, "attachment")
240
+ Templates.attachment_column_names(field).each { |col| Scaffold.refuse_unusable!(col, "attachment column") }
241
+ nil
242
+ end
243
+
244
+ # /\A[a-z][a-z0-9_]*\z/
245
+ def self.field_name?(name)
246
+ return false if name.empty?
247
+
248
+ i = 0
249
+ while i < name.bytesize
250
+ b = name.getbyte(i)
251
+ lower = b >= 97 && b <= 122
252
+ digit_or_underscore = (b >= 48 && b <= 57) || b == 95
253
+ return false unless lower || (i > 0 && digit_or_underscore)
254
+
255
+ i += 1
256
+ end
257
+ true
258
+ end
259
+
260
+ # ---- the migration ------------------------------------------------
261
+
262
+ # db/migrate/<version>_add_cover_to_posts.rb, or the existing one.
263
+ def self.migration_path(root, spec)
264
+ suffix = "_add_#{spec.field}_to_#{spec.plural}.rb"
265
+ existing = Dir.glob(File.join(root, "db/migrate/*#{suffix}")).sort
266
+ return "db/migrate/#{File.basename(existing[0])}" unless existing.empty?
267
+
268
+ "db/migrate/#{Scaffold.next_version(root, Scaffold.timestamp)}#{suffix}"
269
+ end
270
+
271
+ # `default:` is SQL literal text (migration.rb), hence "''" and "0".
272
+ # Not null with "" / 0 as "nothing attached" keeps the model's readers
273
+ # a plain String and Integer under Spinel (NOTES rules 7/11).
274
+ def self.migration(spec)
275
+ buf = +""
276
+ buf << "class Add#{Inflector.camelize(spec.field)}To#{spec.plural_class} < Cybertrain::Migration::Base\n"
277
+ buf << " def change\n"
278
+ Templates::ATTACHMENT_COLUMNS.each do |col|
279
+ buf << " add_column \"#{spec.plural}\", \"#{spec.field}_#{col[0]}\", :#{col[1]}, null: false, default: \"#{col[2]}\"\n"
280
+ end
281
+ buf << " end\n"
282
+ buf << "end\n"
283
+ buf
284
+ end
285
+
286
+ # ---- edits ----------------------------------------------------------
287
+
288
+ # Inserts the attachment block right after the model's class line, once
289
+ # (the block's first comment line is the marker).
290
+ def self.add_model_block(root, spec)
291
+ path = model_file(spec)
292
+ full = File.join(root, path)
293
+ source = File.read(full)
294
+ unless source.index(model_marker(spec)).nil?
295
+ puts "identical #{path} (attachment #{spec.field} already added)"
296
+ return false
297
+ end
298
+
299
+ at = line_end(source, line_start(source, "class #{spec.class_name}", "class"))
300
+ if at < 0
301
+ puts "skip #{path} (no `class #{spec.class_name}` line): add this inside the class:"
302
+ puts Authentication.indent(model_block(spec))
303
+ return false
304
+ end
305
+
306
+ File.write(full, insert_at(source, at, Authentication.indent(model_block(spec)) + "\n"))
307
+ puts "insert #{path} (attachment #{spec.field})"
308
+ true
309
+ end
310
+
311
+ # Inserts the download action right after the controller's class line,
312
+ # once (`def cover` marks it). The record comes from the controller's
313
+ # own `before_action :set_post`, with :cover added to its only: list,
314
+ # so the download is scoped exactly like show: an app that limits
315
+ # set_post to the signed-in user's posts limits the file too. The
316
+ # preflight requires this shared lookup and shared authorization.
317
+ def self.add_action(root, spec)
318
+ path = controller_file(spec)
319
+ full = File.join(root, path)
320
+ source = File.read(full)
321
+ unless line_start(source, "def #{spec.field}", "exact") < 0
322
+ puts "identical #{path} (def #{spec.field} already added)"
323
+ return false
324
+ end
325
+
326
+ coverage = set_record_coverage(source, spec, spec.field)
327
+ scoped = coverage != "no"
328
+ at = line_end(source, line_start(source, controller_line(spec), "start"))
329
+ if at < 0
330
+ puts "skip #{path} (no `#{controller_line(spec)}` line): add this to the controller:"
331
+ puts Authentication.indent(action_block(spec, false))
332
+ return false
333
+ end
334
+
335
+ source = add_to_only_list(source, spec, spec.field) if coverage == "add"
336
+ block = Authentication.indent(action_block(spec, scoped))
337
+ # Below the before_action line when there is one, so that it stays
338
+ # the first thing in the class. The block ends in a blank line; it
339
+ # moves to the front, since that line is usually followed by one.
340
+ before = set_record_line(source, spec)
341
+ if before >= 0
342
+ at = line_end(source, before)
343
+ block = "\n" + block[0, block.size - 1].to_s
344
+ else
345
+ at = line_end(source, line_start(source, controller_line(spec), "start"))
346
+ end
347
+ File.write(full, insert_at(source, at, block))
348
+ puts "insert #{path} (def #{spec.field})"
349
+ puts "insert #{path} (:#{spec.field} in before_action :set_#{spec.singular})" if coverage == "add"
350
+ unless scoped
351
+ puts "warning: #{path} has no `before_action :set_#{spec.singular}` for :#{spec.field}, so def #{spec.field} looks the #{spec.singular} up with an unscoped #{spec.class_name}.find(params[:id]); if your actions limit which records a user may see, limit this lookup the same way"
352
+ end
353
+ true
354
+ end
355
+
356
+ # The image is shown on the show page, so when `allow_unauthenticated_access`
357
+ # makes show public (step 13 of the tutorial: `only: [:index, :show]`),
358
+ # the download joins it; otherwise a visitor sees a broken image. Where
359
+ # show needs a signed-in user the download does too, and nothing changes.
360
+ # Both find the record through `before_action :set_post`; the preflight
361
+ # has checked additional and inherited policies as well.
362
+ def self.add_public_download(root, spec)
363
+ path = controller_file(spec)
364
+ full = File.join(root, path)
365
+ source = File.read(full)
366
+ return false unless download_access(source, spec) == "add"
367
+
368
+ unless set_record_coverage(source, spec, spec.field) == "yes" && set_record_coverage(source, spec, "show") == "yes"
369
+ puts "skip #{path} (:show is public, but def #{spec.field} does not load the #{spec.singular} through before_action :set_#{spec.singular} as show does): add :#{spec.field} to allow_unauthenticated_access yourself if visitors may download it"
370
+ return false
371
+ end
372
+ if show_checks_more?(source, spec)
373
+ puts "skip #{path} (:show is public, but show may check more than set_#{spec.singular}: a before_action of its own, code in def show, or a line this cannot read): add :#{spec.field} to allow_unauthenticated_access yourself once def #{spec.field} checks the same"
374
+ return false
375
+ end
376
+
377
+ File.write(full, add_to_only_list_at(source, public_access_line(source), spec.field))
378
+ puts "insert #{path} (:#{spec.field} in allow_unauthenticated_access, since :show is public)"
379
+ true
380
+ end
381
+
382
+ # Unshared callbacks, skips or mixins cannot establish a shared policy.
383
+ # Restrict callback declarations to literal, single-line symbol lists;
384
+ # conditional/dynamic declarations and blocks need application review.
385
+ def self.callback_checks_more?(source, spec)
386
+ # The authentication generator's helper registers a skip when called;
387
+ # its declaration is not itself a skip. Calls are checked below.
388
+ source = source.sub(" def self.allow_unauthenticated_access(only: [], except: [])\n skip_before_action(:require_authentication, only: only, except: except)\n end\n", "")
389
+ source.each_line do |raw|
390
+ stripped = raw.strip
391
+ return true if stripped.match?(/\A(?:include|extend|prepend)\b/)
392
+ next if stripped.empty? || stripped.start_with?("#")
393
+ next unless stripped.match?(/\b(?:before_action|skip_before_action|allow_unauthenticated_access)\b/)
394
+
395
+ return true unless stripped.match?(/\A(?:before_action\s+:[a-z_]\w*|skip_before_action\s+:[a-z_]\w*|allow_unauthenticated_access)(?:\s*,?\s*(?:only|except):\s*\[(?:\s*:[a-z_]\w*\s*,?)*\s*\])*\s*\z/)
396
+ # Ruby uses the last duplicate keyword; list_coverage reads the first.
397
+ ["only:", "except:"].each do |key|
398
+ at = stripped.index(key)
399
+ return true if !at.nil? && !stripped.index(key, at + key.size).nil?
400
+ end
401
+
402
+ show = list_coverage(stripped, "show") == "yes"
403
+ download = list_coverage(stripped, spec.field) == "yes"
404
+ if stripped.start_with?("before_action")
405
+ return true if show && !download
406
+ else
407
+ return true if download && !show
408
+ end
409
+ end
410
+ false
411
+ end
412
+
413
+ # The scaffold's show is empty. Inline checks cannot safely be copied
414
+ # into a different action; the application must share an explicit policy.
415
+ def self.show_checks_more?(source, spec)
416
+ return true if callback_checks_more?(source, spec)
417
+
418
+ at = line_start(source, "def show", "exact")
419
+ return true if at < 0
420
+
421
+ body = false
422
+ done = false
423
+ source[line_end(source, at), source.size - line_end(source, at)].to_s.each_line do |raw|
424
+ stripped = raw.strip
425
+ next if done || stripped.empty? || stripped.start_with?("#")
426
+
427
+ if stripped == "end"
428
+ done = true
429
+ else
430
+ body = true
431
+ done = true
432
+ end
433
+ end
434
+ body
435
+ end
436
+
437
+ # Who may download: "public" when allow_unauthenticated_access already
438
+ # covers the action (no list, an only: list with it, an except: list
439
+ # without it), "add" when its one-line only: list makes show public but
440
+ # does not list the action, "private" otherwise (no such line, or show
441
+ # needs a signed-in user).
442
+ def self.download_access(source, spec)
443
+ at = public_access_line(source)
444
+ return "private" if at < 0
445
+
446
+ stripped = source[at, line_end(source, at) - at].to_s.strip
447
+ field = list_coverage(stripped, spec.field)
448
+ return "public" if field == "yes"
449
+ return "add" if field == "add" && list_coverage(stripped, "show") == "yes"
450
+
451
+ "private"
452
+ end
453
+
454
+ # Offset of the line `allow_unauthenticated_access` (alone, or followed
455
+ # by its arguments), or -1.
456
+ def self.public_access_line(source)
457
+ head = "allow_unauthenticated_access"
458
+ offset = 0
459
+ hit = -1
460
+ source.each_line do |raw|
461
+ stripped = raw.strip
462
+ if hit < 0 && stripped.start_with?(head)
463
+ rest = stripped[head.size, stripped.size - head.size].to_s
464
+ hit = offset if rest == "" || rest.start_with?(" ") || rest.start_with?("(")
465
+ end
466
+ offset += raw.size
467
+ end
468
+ hit
469
+ end
470
+
471
+ # `@post.cover_file = params.nested(:post).upload(:cover)` after the
472
+ # line that builds the record in create, and in update once @post is
473
+ # loaded: after `@post = Post.find(params[:id])` when update has that
474
+ # line, else right after `def update` when `before_action :set_post`
475
+ # covers update. permit stays as it is: it returns Strings only, so a
476
+ # file can only arrive through this explicit assignment. An action the
477
+ # scaffold shape does not have gets the line printed instead.
478
+ #
479
+ # `nested`, not `require`: a form whose only field is the file sends no
480
+ # `post[...]` key when no file is chosen (the parser drops the empty
481
+ # input), and that edit keeps the stored file rather than being a 400.
482
+ # For the same reason a `<model>_params` that permits nothing (a model
483
+ # whose only fields are files) is switched to nested.
484
+ def self.add_assignments(root, spec)
485
+ path = controller_file(spec)
486
+ full = File.join(root, path)
487
+ line = assignment_line(spec)
488
+ s = spec.singular
489
+ source = File.read(full)
490
+ build = "@#{s} = #{spec.class_name}.new(#{s}_params)"
491
+ updated = insert_line_after(source, line_start(source, build, "exact"), 0, line)
492
+ if updated == ""
493
+ puts "skip #{path} (no `#{build}` line): add `#{line}` to create"
494
+ elsif updated == source
495
+ puts "identical #{path} (#{spec.field}_file= in create)"
496
+ else
497
+ puts "insert #{path} (#{spec.field}_file= in create)"
498
+ end
499
+ source = updated unless updated == ""
500
+
501
+ updated = ""
502
+ def_at = line_start(source, "def update", "exact")
503
+ find = "@#{s} = #{spec.class_name}.find(params[:id])"
504
+ if def_at >= 0
505
+ find_at = line_start_between(source, find, line_end(source, def_at), method_end(source, def_at))
506
+ if find_at >= 0
507
+ updated = insert_line_after(source, find_at, 0, line)
508
+ elsif set_record_coverage(source, spec, "update") == "yes"
509
+ updated = insert_line_after(source, def_at, 2, line)
510
+ end
511
+ end
512
+ if updated == ""
513
+ puts "skip #{path} (update does not load @#{s} with `#{find}` nor through `before_action :set_#{s}`): add `#{line}` to update once @#{s} is loaded"
514
+ elsif updated == source
515
+ puts "identical #{path} (#{spec.field}_file= in update)"
516
+ else
517
+ puts "insert #{path} (#{spec.field}_file= in update)"
518
+ end
519
+ source = updated unless updated == ""
520
+
521
+ strict = "params.require(:#{s}).permit()"
522
+ unless source.index(strict).nil?
523
+ source = source.sub(strict, "params.nested(:#{s}).permit()")
524
+ puts "insert #{path} (#{s}_params reads params.nested(:#{s}): the form may send only files)"
525
+ end
526
+
527
+ File.write(full, source)
528
+ true
529
+ end
530
+
531
+ # Inserts the download route after the draw line, once.
532
+ def self.add_route(root, spec)
533
+ routes_path = File.join(root, "config/routes.rb")
534
+ line = route_line(spec)
535
+ source = File.read(routes_path)
536
+ unless source.index(line).nil?
537
+ puts "identical route #{spec.singular}_#{spec.field}"
538
+ return false
539
+ end
540
+
541
+ File.write(routes_path, Authentication.insert_after_line(source, Scaffold::DRAW_LINE, Authentication.indent(line + "\n")))
542
+ puts "route #{spec.singular}_#{spec.field} (GET /#{spec.plural}/:id/#{spec.field})"
543
+ true
544
+ end
545
+
546
+ # Appends /storage/uploads/ to .gitignore, once. The templates for a
547
+ # new app have it already.
548
+ def self.add_gitignore(root)
549
+ path = File.join(root, ".gitignore")
550
+ unless File.exist?(path)
551
+ puts "skip .gitignore (not found): ignore /storage/uploads/"
552
+ return false
553
+ end
554
+
555
+ source = File.read(path)
556
+ present = false
557
+ source.each_line { |l| present = true if l.strip == "/storage/uploads/" }
558
+ if present
559
+ puts "identical .gitignore (/storage/uploads/ already ignored)"
560
+ return false
561
+ end
562
+
563
+ source += "\n" unless source.empty? || source.end_with?("\n")
564
+ File.write(path, source + "/storage/uploads/\n")
565
+ puts "insert .gitignore (/storage/uploads/)"
566
+ true
567
+ end
568
+
569
+ # The scaffold's form gets multipart: true and the file input; any
570
+ # other shape of form is told what to add.
571
+ def self.add_form_field(root, spec)
572
+ path = "app/views/#{spec.plural}/_form.html.erb"
573
+ full = File.join(root, path)
574
+ s = spec.singular
575
+ unless File.exist?(full)
576
+ puts "skip #{path} (not found): use form_with(model: #{s}, multipart: true) and #{file_input(spec)}"
577
+ return false
578
+ end
579
+
580
+ source = File.read(full)
581
+ if name_present?(source, "f.file_field :#{spec.field}")
582
+ puts "identical #{path} (file field already added)"
583
+ return false
584
+ end
585
+
586
+ updated = source
587
+ plain = "<%= form_with(model: #{s}) do |f| %>"
588
+ updated = updated.sub(plain, "<%= form_with(model: #{s}, multipart: true) do |f| %>") unless updated.index(plain).nil?
589
+ actions = line_start(updated, "<div class=\"actions\">", "exact")
590
+ if updated.index("multipart: true").nil? || actions < 0
591
+ puts "skip #{path} (not the scaffold's form): use form_with(model: #{s}, multipart: true) and #{file_input(spec)}"
592
+ return false
593
+ end
594
+
595
+ File.write(full, insert_at(updated, actions, form_block(spec)))
596
+ puts "insert #{path} (#{spec.field} file field, multipart: true)"
597
+ true
598
+ end
599
+
600
+ # The cover image (or, with --any, a download link) after the <h1> of
601
+ # the scaffold's show page.
602
+ def self.add_show_markup(root, spec)
603
+ path = "app/views/#{spec.plural}/show.html.erb"
604
+ full = File.join(root, path)
605
+ markup = show_markup(spec)
606
+ unless File.exist?(full)
607
+ puts "skip #{path} (not found): show the file with #{markup}"
608
+ return false
609
+ end
610
+
611
+ source = File.read(full)
612
+ if name_present?(source, "@#{spec.singular}.#{spec.field}_url")
613
+ puts "identical #{path} (#{spec.field} already shown)"
614
+ return false
615
+ end
616
+
617
+ at = line_end(source, line_start(source, "<h1>", "start"))
618
+ if at < 0
619
+ puts "skip #{path} (no <h1>): show the file with #{markup}"
620
+ return false
621
+ end
622
+
623
+ File.write(full, insert_at(source, at, markup + "\n"))
624
+ puts "insert #{path} (#{spec.field})"
625
+ true
626
+ end
627
+
628
+ # ---- line helpers ---------------------------------------------------
629
+
630
+ # Offset where the first line matching `text` starts, or -1. `mode`
631
+ # "exact": the stripped line equals text; "start": it starts with
632
+ # text; "class": a `class Name` line (the name ends at a space,
633
+ # `<`, `;` or the end of the line, so `class Post` is not `class PostTag`).
634
+ def self.line_start(source, text, mode)
635
+ offset = 0
636
+ source.each_line do |raw|
637
+ stripped = raw.strip
638
+ hit = false
639
+ if mode == "exact"
640
+ hit = stripped == text
641
+ elsif mode == "start"
642
+ hit = stripped.start_with?(text)
643
+ else
644
+ rest = stripped.start_with?(text) ? stripped[text.size, stripped.size - text.size].to_s : "x"
645
+ hit = rest == "" || rest.start_with?(" ") || rest.start_with?(";")
646
+ end
647
+ return offset if hit
648
+
649
+ offset += raw.size
650
+ end
651
+ -1
652
+ end
653
+
654
+ # Offset just after the line that starts at `start` (its newline
655
+ # included), or -1 when start is -1.
656
+ def self.line_end(source, start)
657
+ return -1 if start < 0
658
+
659
+ eol = source.index("\n", start)
660
+ eol.nil? ? source.size : eol + 1
661
+ end
662
+
663
+ # `source` with `block` inserted at `offset`; a source whose last
664
+ # line has no newline gets one first.
665
+ def self.insert_at(source, offset, block)
666
+ head = source[0, offset].to_s
667
+ head += "\n" unless head.empty? || head.end_with?("\n")
668
+ out = +""
669
+ out << head << block << source[offset, source.size - offset].to_s
670
+ out << "\n" unless out.end_with?("\n")
671
+ out
672
+ end
673
+
674
+ # `source` with `text` (indented `extra` columns deeper than the anchor)
675
+ # inserted after the anchor line, the line that starts at offset `at`.
676
+ # Returns `source` itself when the method around the anchor already
677
+ # has a line `text` anywhere (a second attachment's line may sit
678
+ # between the anchor and this one), and "" when `at` is -1.
679
+ def self.insert_line_after(source, at, extra, text)
680
+ return "" if at < 0
681
+ return source if line_start_between(source, text, method_start(source, at), method_end(source, at)) >= 0
682
+
683
+ eol = line_end(source, at)
684
+ anchor_line = source[at, eol - at].to_s
685
+ pad = +""
686
+ (anchor_line.size - anchor_line.lstrip.size + extra).times { pad << " " }
687
+ insert_at(source, eol, "#{pad}#{text}\n")
688
+ end
689
+
690
+ # Whether a line starts with `def ` or is a visibility keyword: where
691
+ # one method's body ends.
692
+ def self.method_boundary?(stripped)
693
+ stripped.start_with?("def ") || stripped == "private" || stripped == "protected" || stripped == "public"
694
+ end
695
+
696
+ # Offset of the `def` line of the method around offset `at` (the last
697
+ # `def` line starting at or before it), or 0.
698
+ def self.method_start(source, at)
699
+ found = 0
700
+ offset = 0
701
+ source.each_line do |raw|
702
+ found = offset if offset <= at && raw.strip.start_with?("def ")
703
+ offset += raw.size
704
+ end
705
+ found
706
+ end
707
+
708
+ # Offset of the first method boundary after the line at `at`, or the
709
+ # end of `source`.
710
+ def self.method_end(source, at)
711
+ after = line_end(source, at)
712
+ found = source.size
713
+ offset = 0
714
+ source.each_line do |raw|
715
+ found = offset if offset >= after && found == source.size && method_boundary?(raw.strip)
716
+ offset += raw.size
717
+ end
718
+ found
719
+ end
720
+
721
+ # Offset of the first line in [from, to) whose stripped text is
722
+ # `text`, or -1.
723
+ def self.line_start_between(source, text, from, to)
724
+ offset = 0
725
+ hit = -1
726
+ source.each_line do |raw|
727
+ hit = offset if hit < 0 && offset >= from && offset < to && raw.strip == text
728
+ offset += raw.size
729
+ end
730
+ hit
731
+ end
732
+
733
+ # Whether `piece` occurs in `source` as a whole name, not followed by
734
+ # a letter, digit or _: `f.file_field :cover` is not in a form that
735
+ # only has `f.file_field :cover_image`.
736
+ def self.name_present?(source, piece)
737
+ at = source.index(piece)
738
+ until at.nil?
739
+ after = at + piece.size
740
+ return true if after >= source.size || !name_char?(source[after, 1].to_s)
741
+
742
+ at = source.index(piece, at + 1)
743
+ end
744
+ false
745
+ end
746
+
747
+ def self.name_char?(ch)
748
+ (ch >= "a" && ch <= "z") || (ch >= "A" && ch <= "Z") || (ch >= "0" && ch <= "9") || ch == "_"
749
+ end
750
+
751
+ # How the controller's `before_action :set_post` covers `action`:
752
+ # "yes" when it runs for it (no only:, or only: lists it, or an except:
753
+ # that does not), "add" when it has a one-line `only: [...]` list
754
+ # without it, "no" when there is no such line or its only: is not a
755
+ # list this can read.
756
+ def self.set_record_coverage(source, spec, action)
757
+ at = set_record_line(source, spec)
758
+ return "no" if at < 0
759
+
760
+ list_coverage(source[at, line_end(source, at) - at].to_s.strip, action)
761
+ end
762
+
763
+ # How a callback line (`before_action :set_post, only: [...]`,
764
+ # `allow_unauthenticated_access except: [...]`) covers `action`: "yes",
765
+ # "add" (a one-line only: list without it) or "no", as
766
+ # set_record_coverage describes.
767
+ # except: wins over only: (Callback#applies? checks both), and a list
768
+ # this cannot read answers "no".
769
+ def self.list_coverage(stripped, action)
770
+ token = ":#{action}"
771
+ except_at = stripped.index("except:")
772
+ only_at = stripped.index("only:")
773
+ unless except_at.nil?
774
+ return "no" if bracket_open(stripped, except_at + 7) < 0
775
+ return "no" if list_has?(stripped, except_at + 7, token)
776
+ end
777
+ return "yes" if only_at.nil?
778
+ return "no" if bracket_open(stripped, only_at + 5) < 0
779
+
780
+ list_has?(stripped, only_at + 5, token) ? "yes" : "add"
781
+ end
782
+
783
+ # Offset of the line `before_action :set_post` (alone or followed by
784
+ # a comma, so not `:set_post_tag`), or -1.
785
+ def self.set_record_line(source, spec)
786
+ head = "before_action :set_#{spec.singular}"
787
+ offset = 0
788
+ hit = -1
789
+ source.each_line do |raw|
790
+ stripped = raw.strip
791
+ if hit < 0 && stripped.start_with?(head)
792
+ rest = stripped[head.size, stripped.size - head.size].to_s
793
+ hit = offset if rest == "" || rest.start_with?(",")
794
+ end
795
+ offset += raw.size
796
+ end
797
+ hit
798
+ end
799
+
800
+ # Offset of the `[` that follows offset `from` in `line` (after
801
+ # spaces) when a `]` closes it on the same line, or -1.
802
+ def self.bracket_open(line, from)
803
+ i = from
804
+ i += 1 while i < line.size && line[i, 1] == " "
805
+ return -1 unless line[i, 1] == "["
806
+ return -1 if line.index("]", i).nil?
807
+
808
+ i
809
+ end
810
+
811
+ # Whether the bracketed list after offset `from` in `line` has the
812
+ # item `token` (":show"); false when there is no such list.
813
+ def self.list_has?(line, from, token)
814
+ open = bracket_open(line, from)
815
+ return false if open < 0
816
+
817
+ close = line.index("]", open).to_i
818
+ found = false
819
+ line[open + 1, close - open - 1].to_s.split(",").each { |item| found = true if item.strip == token }
820
+ found
821
+ end
822
+
823
+ # `source` with `:action` appended to the only: list of `before_action
824
+ # :set_post` (set_record_coverage answered "add").
825
+ def self.add_to_only_list(source, spec, action)
826
+ add_to_only_list_at(source, set_record_line(source, spec), action)
827
+ end
828
+
829
+ # `source` with `:action` appended to the only: list of the line at
830
+ # offset `at`.
831
+ def self.add_to_only_list_at(source, at, action)
832
+ eol = line_end(source, at)
833
+ line = source[at, eol - at].to_s
834
+ open = line.index("[", line.index("only:").to_i).to_i
835
+ close = line.index("]", open).to_i
836
+ inner = line[open + 1, close - open - 1].to_s
837
+ sep = inner.strip.empty? ? "" : ", "
838
+ updated = line[0, close].to_s + "#{sep}:#{action}" + line[close, line.size - close].to_s
839
+ source[0, at].to_s + updated + source[eol, source.size - eol].to_s
840
+ end
841
+
842
+ # ---- the generated code (quoted templates, filled by AttachmentSpec) ----
843
+
844
+ def self.assignment_line(spec)
845
+ "@#{spec.singular}.#{spec.field}_file = params.nested(:#{spec.singular}).upload(:#{spec.field})"
846
+ end
847
+
848
+ def self.file_input(spec)
849
+ accept = spec.any ? "" : ", accept: \"#{Templates::ATTACHMENT_ACCEPT}\""
850
+ "<%= f.file_field :#{spec.field}#{accept} %>"
851
+ end
852
+
853
+ def self.form_block(spec)
854
+ " <div class=\"field\">\n <%= f.label :#{spec.field} %>\n #{file_input(spec)}\n </div>\n\n"
855
+ end
856
+
857
+ def self.show_markup(spec)
858
+ s = spec.singular
859
+ f = spec.field
860
+ if spec.any
861
+ "<% if @#{s}.#{f}_attached? %><p><a href=\"<%= @#{s}.#{f}_url %>\"><%= @#{s}.#{f}_filename %></a></p><% end %>"
862
+ else
863
+ "<% if @#{s}.#{f}_attached? %><p><img src=\"<%= @#{s}.#{f}_url %>\" alt=\"<%= @#{s}.#{f}_filename %>\"></p><% end %>"
864
+ end
865
+ end
866
+
867
+ # The block inserted (indented) after the model's class line. The hook
868
+ # methods are public because the callback blocks get the record, not
869
+ # an implicit self (as in the authentication generator's User).
870
+ def self.model_block(spec)
871
+ types = spec.any ? "Array.new(0) { \"\" }" : IMAGE_TYPES_LITERAL
872
+ text = MODEL_BLOCK.sub("<<types>>", types)
873
+ text = text.sub("<<type_check>>", spec.any ? "" : TYPE_CHECK_LINE)
874
+ spec.expand(text)
875
+ end
876
+
877
+ MODEL_BLOCK = <<~'RUBY'
878
+ # cybertrain:attachment <<field>> -- written by `cybertrain generate attachment`; yours to change.
879
+ # The bytes live in storage/uploads (Cybertrain::BlobStore), never in the
880
+ # database: the five <<field>>_* columns hold their key and metadata. The type
881
+ # stored is the one the server detected from the bytes, never the client's.
882
+ # <<FIELD>>_MAX_BYTES must stay at or below config.max_upload_bytes (5 MiB), or a
883
+ # larger file is a 400 from the parser instead of the 422 below.
884
+ <<FIELD>>_TYPES = <<types>>
885
+ <<FIELD>>_MAX_BYTES = 2_097_152
886
+
887
+ before_validation { |<<model>>| <<model>>.validate_<<field>>_upload }
888
+ before_save { |<<model>>| <<model>>.store_<<field>>_upload }
889
+ after_save { |<<model>>| <<model>>.delete_replaced_<<field>> }
890
+ after_destroy { |<<model>>| <<model>>.purge_<<field>> }
891
+
892
+ # Takes the Cybertrain::UploadedFile of a form (params.nested(:<<model>>).upload(:<<field>>)),
893
+ # or nil. Nil or an empty file keeps the current one.
894
+ def <<field>>_file=(upload)
895
+ @<<field>>_upload = upload.nil? ? Cybertrain::UploadedFile.blank : upload
896
+ end
897
+
898
+ def <<field>>_attached?
899
+ !<<field>>_key.to_s.empty?
900
+ end
901
+
902
+ # Where the file is served from (the download action of the
903
+ # controller); "" when none is attached. ?v= changes with the content,
904
+ # so a replaced file is not served from a browser's cache.
905
+ def <<field>>_url
906
+ <<field>>_attached? ? "/<<plural>>/#{id}/<<field>>?v=#{<<field>>_checksum.to_s[0, 12]}" : ""
907
+ end
908
+
909
+ # Public so the callbacks above (which receive the record) can call them.
910
+
911
+ def validate_<<field>>_upload
912
+ pending = <<field>>_pending_upload
913
+ return nil if pending.empty?
914
+
915
+ errors.add(:<<field>>, "is too large (maximum is 2 MB)") if pending.byte_size > <<FIELD>>_MAX_BYTES
916
+ <<type_check>> nil
917
+ end
918
+
919
+ # Writes the new file to disk under a random key and points the columns
920
+ # at it. The old key is kept for delete_replaced_<<field>>. A save that fails
921
+ # after this point (a database error) leaves the new blob orphaned.
922
+ def store_<<field>>_upload
923
+ pending = <<field>>_pending_upload
924
+ return nil if pending.empty?
925
+
926
+ key = Cybertrain::BlobStore.new_key
927
+ Cybertrain::BlobStore.write(key, pending.body_bytes)
928
+ @replaced_<<field>>_key = <<field>>_key.to_s
929
+ self.<<field>>_key = key
930
+ self.<<field>>_filename = pending.original_filename
931
+ self.<<field>>_content_type = pending.detected_content_type
932
+ self.<<field>>_byte_size = pending.byte_size
933
+ self.<<field>>_checksum = pending.sha256_hex
934
+ @<<field>>_upload = Cybertrain::UploadedFile.blank
935
+ nil
936
+ end
937
+
938
+ def delete_replaced_<<field>>
939
+ old = @replaced_<<field>>_key
940
+ old = "" if old.nil?
941
+ Cybertrain::BlobStore.delete(old) unless old.empty?
942
+ @replaced_<<field>>_key = ""
943
+ nil
944
+ end
945
+
946
+ def purge_<<field>>
947
+ Cybertrain::BlobStore.delete(<<field>>_key.to_s)
948
+ nil
949
+ end
950
+
951
+ def <<field>>_pending_upload
952
+ upload = @<<field>>_upload
953
+ upload.nil? ? Cybertrain::UploadedFile.blank : upload
954
+ end
955
+ RUBY
956
+
957
+ TYPE_CHECK_LINE = " errors.add(:<<field>>, \"must be a PNG, JPEG, GIF or WebP image\") unless <<FIELD>>_TYPES.empty? || <<FIELD>>_TYPES.include?(pending.detected_content_type)\n"
958
+
959
+ # Inserted (indented) after the controller's class line. `scoped`: the
960
+ # record comes from `before_action :set_<model>`; otherwise the action
961
+ # looks it up itself.
962
+ def self.action_block(spec, scoped)
963
+ text = ACTION_BLOCK.sub("<<lookup_note>>", scoped ? LOOKUP_SCOPED_NOTE : LOOKUP_FIND_NOTE)
964
+ text = text.sub("<<lookup>>", scoped ? "" : " @<<model>> = <<Model>>.find(params[:id])\n")
965
+ spec.expand(text)
966
+ end
967
+
968
+ LOOKUP_SCOPED_NOTE = <<~'RUBY'
969
+ # GET /<<plural>>/1/<<field>> -- @<<model>> comes from before_action :set_<<model>>, so
970
+ # the download is scoped exactly like show (and authentication applies).
971
+ RUBY
972
+
973
+ LOOKUP_FIND_NOTE = <<~'RUBY'
974
+ # GET /<<plural>>/1/<<field>> -- runs after this controller's before_actions
975
+ # (authentication applies), but the lookup below is unscoped: if your other
976
+ # actions limit which records a user may see, limit this one the same way.
977
+ RUBY
978
+
979
+ ACTION_BLOCK = <<~'RUBY'
980
+ <<lookup_note>># send_blob is the one way to serve uploads: it sets the type from what the
981
+ # server detected, nosniff, a locked-down CSP and Content-Disposition
982
+ # (inline for images only, a download otherwise).
983
+ def <<field>>
984
+ <<lookup>> if @<<model>>.<<field>>_attached? && Cybertrain::BlobStore.exist?(@<<model>>.<<field>>_key)
985
+ send_blob(Cybertrain::BlobStore.read(@<<model>>.<<field>>_key), filename: @<<model>>.<<field>>_filename, content_type: @<<model>>.<<field>>_content_type)
986
+ else
987
+ head 404
988
+ end
989
+ end
990
+
991
+ RUBY
992
+
993
+ # ---- the generated test ---------------------------------------------
994
+
995
+ # The app test for this attachment: written as test/attachment_post_cover.rb.
996
+ # It signs in first when the app has authentication (app/models/user.rb;
997
+ # a later `generate authentication` adds the sign-in to it instead),
998
+ # fills the form fields the model validates for presence, and checks
999
+ # the security-relevant behaviour (type, filename, CSRF, cleanup).
1000
+ def self.attachment_test(root, spec)
1001
+ auth = File.exist?(File.join(root, "app/models/user.rb"))
1002
+ text = TEST_HEAD
1003
+ text += spec.any ? TEST_PNG_ROUNDTRIP + TEST_ANY_TYPE : TEST_PNG_ROUNDTRIP + TEST_REJECT_TYPES
1004
+ text += TEST_COMMON
1005
+ fields = required_fields(root, spec)
1006
+ text += TEST_KEEP
1007
+ if auth
1008
+ open_download = download_access(File.read(File.join(root, controller_file(spec))), spec) == "public"
1009
+ text += open_download ? TEST_PUBLIC_DOWNLOAD : TEST_AUTH_DOWNLOAD
1010
+ end
1011
+ text += TEST_TAIL
1012
+ text = text.sub(" <<reset_auth>>\n", auth ? Authentication::TEST_RESET_LINES : "")
1013
+ text = text.sub(" <<sign_in>>\n", auth ? Authentication::TEST_SIGN_IN_LINES : "")
1014
+ text = text.sub("<<fields_lines>>", field_lines(root, spec, fields))
1015
+ text = text.sub("<<show_check>>", show_check(root, spec))
1016
+ spec.expand(text)
1017
+ end
1018
+
1019
+ # The attributes the model validates for presence or a minimum length
1020
+ # (`validates :title, presence: true`), which a valid record needs in
1021
+ # the form.
1022
+ def self.required_fields(root, spec)
1023
+ names = Array.new(0) { "" }
1024
+ path = File.join(root, model_file(spec))
1025
+ return names unless File.exist?(path)
1026
+
1027
+ File.read(path).each_line do |raw|
1028
+ line = raw.strip
1029
+ next unless line.start_with?("validates :") && (!line.index("presence: true").nil? || !line.index("minimum:").nil?)
1030
+
1031
+ name = +""
1032
+ line[11, line.size - 11].to_s.each_char do |ch|
1033
+ break unless (ch >= "a" && ch <= "z") || (ch >= "0" && ch <= "9") || ch == "_"
1034
+
1035
+ name << ch
1036
+ end
1037
+ names << name unless name.empty? || names.include?(name)
1038
+ end
1039
+ names
1040
+ end
1041
+
1042
+ def self.field_lines(root, spec, names)
1043
+ lines = +""
1044
+ names.each { |name| lines << " fields[\"#{spec.singular}[#{name}]\"] = \"#{field_value(root, spec, name)}\"\n" }
1045
+ lines
1046
+ end
1047
+
1048
+ # "A post", stretched to the field's `length: { minimum: N }` and cut
1049
+ # to its `maximum: N` (the blog's body needs 10 characters), so the
1050
+ # test's record is valid.
1051
+ def self.field_value(root, spec, name)
1052
+ value = "A #{spec.singular.tr("_", " ")}"
1053
+ min = length_bound(root, spec, name, "minimum:")
1054
+ max = length_bound(root, spec, name, "maximum:")
1055
+ value = value.ljust(min, ".") if min > value.size
1056
+ value = value[0, max].to_s if max >= 0 && value.size > max
1057
+ value
1058
+ end
1059
+
1060
+ # The number after `key` (`minimum:`) on a `validates :name, ...` line
1061
+ # of the model, or -1.
1062
+ def self.length_bound(root, spec, name, key)
1063
+ bound = -1
1064
+ path = File.join(root, model_file(spec))
1065
+ return bound unless File.exist?(path)
1066
+
1067
+ File.read(path).each_line do |raw|
1068
+ line = raw.strip
1069
+ next unless line.start_with?("validates :#{name},") || line == "validates :#{name}"
1070
+
1071
+ at = line.index(key)
1072
+ next if at.nil?
1073
+
1074
+ digits = +""
1075
+ line[at + key.size, line.size - at - key.size].to_s.strip.each_char do |ch|
1076
+ break unless ch >= "0" && ch <= "9"
1077
+
1078
+ digits << ch
1079
+ end
1080
+ bound = digits.to_i unless digits.empty?
1081
+ end
1082
+ bound
1083
+ end
1084
+
1085
+ # The show-page assertion, only when the show page renders the file
1086
+ # (the scaffold shape; another page may not).
1087
+ def self.show_check(root, spec)
1088
+ path = File.join(root, "app/views/#{spec.plural}/show.html.erb")
1089
+ return "" unless File.exist?(path) && name_present?(File.read(path), "@#{spec.singular}.#{spec.field}_url")
1090
+
1091
+ " res = client.get(\"/#{spec.plural}/\#{record.id}\")\n assert_includes res.body, \"/#{spec.plural}/\#{record.id}/#{spec.field}?v=\"\n"
1092
+ end
1093
+
1094
+ TEST_HEAD = <<~'RUBY'
1095
+ # Uploads of <<Model>>#<<field>> through the full stack: the multipart parser, CSRF,
1096
+ # method override, the model's callbacks and the download action. Written
1097
+ # by `cybertrain generate attachment <<model>> <<field>>`. Run with
1098
+ # `cybertrain spin test`; its snapshot comes from the compiled binary (the
1099
+ # database is FFI): `script/regen-snapshot`-style, see README "Testing an app".
1100
+ require_relative "support/app_test"
1101
+
1102
+ # Uploaded files land here, not in storage/uploads.
1103
+ Cybertrain::BlobStore.root = "tmp/test_uploads"
1104
+
1105
+ # A PNG signature, then NUL, 0xFF and CR LF bytes: the server sniffs the
1106
+ # signature only, the rest shows that every byte survives the round trip.
1107
+ PNG_BYTES = Cybertrain::Test.binary([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0xFF, 0x0D, 0x0A, 0x00, 0x01])
1108
+ HTML_BYTES = "<html><script>alert(document.cookie)</script></html>"
1109
+ SVG_BYTES = '<svg xmlns="http://www.w3.org/2000/svg" onload="alert(1)"></svg>'
1110
+
1111
+ # The text fields of the form besides the file, and the CSRF token. Add the
1112
+ # other attributes your <<Model>> requires.
1113
+ def <<model>>_fields(token)
1114
+ fields = { "authenticity_token" => token }
1115
+ <<fields_lines>> fields
1116
+ end
1117
+
1118
+ def new_client
1119
+ <<Model>>.all.delete_all
1120
+ <<reset_auth>>
1121
+ Dir.glob("tmp/test_uploads/*/*").each { |path| File.delete(path) }
1122
+ client = Cybertrain::Test::Client.new(APP)
1123
+ <<sign_in>>
1124
+ client
1125
+ end
1126
+
1127
+ # POST /<<plural>> with one file in <<model>>[<<field>>], as the new form sends it.
1128
+ def create_<<model>>(client, filename, type, data)
1129
+ client.get("/<<plural>>/new")
1130
+ token = AppTest.form_token(client)
1131
+ field = "<<model>>[<<field>>]"
1132
+ upload = Cybertrain::Test::Upload.new(field, filename, type, data)
1133
+ client.post_multipart("/<<plural>>", <<model>>_fields(token), [upload])
1134
+ end
1135
+
1136
+ RUBY
1137
+
1138
+ TEST_PNG_ROUNDTRIP = <<~'RUBY'
1139
+ test "a PNG upload is stored on disk and served back with safe headers" do
1140
+ client = new_client
1141
+ res = create_<<model>>(client, "<<field>>.png", "image/png", PNG_BYTES)
1142
+ assert_response res, :see_other
1143
+ assert_equal 1, <<Model>>.count
1144
+ record = <<Model>>.first
1145
+ assert record.<<field>>_attached?
1146
+ assert_equal "<<field>>.png", record.<<field>>_filename
1147
+ assert_equal "image/png", record.<<field>>_content_type
1148
+ assert_equal PNG_BYTES.bytesize, record.<<field>>_byte_size
1149
+ assert Cybertrain::BlobStore.exist?(record.<<field>>_key)
1150
+ res = client.get("/<<plural>>/#{record.id}/<<field>>")
1151
+ assert_response res, :ok
1152
+ served = res.body
1153
+ assert_equal PNG_BYTES.bytesize, served.bytesize
1154
+ name = "copy.png"
1155
+ data = served.b
1156
+ copy = Cybertrain::UploadedFile.new(name, "", data)
1157
+ assert_equal record.<<field>>_checksum, copy.sha256_hex
1158
+ assert res.header("content-type").to_s.start_with?("image/png"), "content-type"
1159
+ assert_equal "nosniff", res.header("x-content-type-options")
1160
+ assert res.header("content-disposition").to_s.start_with?("inline"), "content-disposition"
1161
+ assert_equal "default-src 'none'; sandbox", res.header("content-security-policy")
1162
+ <<show_check>>end
1163
+
1164
+ RUBY
1165
+
1166
+ TEST_REJECT_TYPES = <<~'RUBY'
1167
+ test "HTML and SVG bytes are refused even when named .png and declared image/png" do
1168
+ client = new_client
1169
+ res = create_<<model>>(client, "x.png", "image/png", HTML_BYTES)
1170
+ assert_response res, :unprocessable_entity
1171
+ assert_includes res.body, "<<Field>> must be a PNG, JPEG, GIF or WebP image"
1172
+ res = create_<<model>>(client, "logo.png", "image/png", SVG_BYTES)
1173
+ assert_response res, :unprocessable_entity
1174
+ assert_equal 0, <<Model>>.count
1175
+ assert_equal 0, Dir.glob("tmp/test_uploads/*/*").size
1176
+ end
1177
+
1178
+ RUBY
1179
+
1180
+ TEST_ANY_TYPE = <<~'RUBY'
1181
+ test "any file is accepted, stored as a download and never served inline" do
1182
+ client = new_client
1183
+ res = create_<<model>>(client, "page.html", "text/html", HTML_BYTES)
1184
+ assert_response res, :see_other
1185
+ record = <<Model>>.first
1186
+ assert_equal "application/octet-stream", record.<<field>>_content_type
1187
+ res = client.get("/<<plural>>/#{record.id}/<<field>>")
1188
+ assert_response res, :ok
1189
+ assert res.header("content-type").to_s.start_with?("application/octet-stream"), "content-type"
1190
+ assert res.header("content-disposition").to_s.start_with?("attachment"), "content-disposition"
1191
+ assert_equal "nosniff", res.header("x-content-type-options")
1192
+ assert_equal "default-src 'none'; sandbox", res.header("content-security-policy")
1193
+ end
1194
+
1195
+ RUBY
1196
+
1197
+ TEST_COMMON = <<~'RUBY'
1198
+ test "a file over the model's limit is a 422 with a message" do
1199
+ client = new_client
1200
+ big = +""
1201
+ big << PNG_BYTES
1202
+ big << ("a" * (<<Model>>::<<FIELD>>_MAX_BYTES + 1))
1203
+ res = create_<<model>>(client, "big.png", "image/png", big)
1204
+ assert_response res, :unprocessable_entity
1205
+ assert_includes res.body, "<<Field>> is too large (maximum is 2 MB)"
1206
+ assert_equal 0, <<Model>>.count
1207
+ end
1208
+
1209
+ test "the client's filename is a display name: it never reaches the disk path" do
1210
+ client = new_client
1211
+ res = create_<<model>>(client, "../../evil.png", "image/png", PNG_BYTES)
1212
+ assert_response res, :see_other
1213
+ record = <<Model>>.first
1214
+ assert_equal "evil.png", record.<<field>>_filename
1215
+ key = record.<<field>>_key
1216
+ assert_equal 32, key.bytesize
1217
+ assert Cybertrain::BlobStore.valid_key?(key)
1218
+ path = Cybertrain::BlobStore.path_for(key)
1219
+ refute path.include?("evil"), path
1220
+ assert path.start_with?("tmp/test_uploads/"), path
1221
+ res = client.get("/<<plural>>/#{record.id}/<<field>>")
1222
+ assert_includes res.header("content-disposition").to_s, "filename=\"evil.png\""
1223
+ end
1224
+
1225
+ test "replacing the file deletes the old blob; destroying the record deletes the current one" do
1226
+ client = new_client
1227
+ create_<<model>>(client, "one.png", "image/png", PNG_BYTES)
1228
+ record = <<Model>>.first
1229
+ old_key = record.<<field>>_key
1230
+ client.get("/<<plural>>/#{record.id}/edit")
1231
+ token = AppTest.form_token(client)
1232
+ field = "<<model>>[<<field>>]"
1233
+ upload = Cybertrain::Test::Upload.new(field, "two.png", "image/png", PNG_BYTES)
1234
+ res = client.patch_multipart("/<<plural>>/#{record.id}", <<model>>_fields(token), [upload])
1235
+ assert_response res, :see_other
1236
+ record = <<Model>>.find(record.id)
1237
+ new_key = record.<<field>>_key
1238
+ refute new_key == old_key, "the key changes with the file"
1239
+ assert_equal "two.png", record.<<field>>_filename
1240
+ refute Cybertrain::BlobStore.exist?(old_key), "the replaced blob is deleted"
1241
+ assert Cybertrain::BlobStore.exist?(new_key)
1242
+ client.get("/<<plural>>/#{record.id}")
1243
+ res = client.post("/<<plural>>/#{record.id}", { "_method" => "delete", "authenticity_token" => AppTest.form_token(client) })
1244
+ assert_response res, :see_other
1245
+ assert_equal 0, <<Model>>.count
1246
+ refute Cybertrain::BlobStore.exist?(new_key), "the destroyed record's blob is deleted"
1247
+ end
1248
+
1249
+ test "a record whose blob is gone answers 404" do
1250
+ client = new_client
1251
+ create_<<model>>(client, "<<field>>.png", "image/png", PNG_BYTES)
1252
+ record = <<Model>>.first
1253
+ File.delete(Cybertrain::BlobStore.path_for(record.<<field>>_key))
1254
+ res = client.get("/<<plural>>/#{record.id}/<<field>>")
1255
+ assert_response res, :not_found
1256
+ end
1257
+
1258
+ test "a multipart POST without the CSRF token is refused" do
1259
+ client = new_client
1260
+ client.get("/<<plural>>/new")
1261
+ field = "<<model>>[<<field>>]"
1262
+ upload = Cybertrain::Test::Upload.new(field, "<<field>>.png", "image/png", PNG_BYTES)
1263
+ fields = <<model>>_fields("unused")
1264
+ fields.delete("authenticity_token")
1265
+ res = client.post_multipart("/<<plural>>", fields, [upload])
1266
+ assert_response res, :forbidden
1267
+ assert_equal 0, <<Model>>.count
1268
+ end
1269
+
1270
+ RUBY
1271
+
1272
+ TEST_KEEP = <<~'RUBY'
1273
+ # What a browser sends for a file input left empty: filename="" and no
1274
+ # bytes. The parser drops it, so a form whose only field is the file
1275
+ # arrives without any <<model>>[...] key at all.
1276
+ test "an update without a file keeps the stored one" do
1277
+ client = new_client
1278
+ create_<<model>>(client, "<<field>>.png", "image/png", PNG_BYTES)
1279
+ record = <<Model>>.first
1280
+ key = record.<<field>>_key
1281
+ client.get("/<<plural>>/#{record.id}/edit")
1282
+ field = "<<model>>[<<field>>]"
1283
+ empty = Cybertrain::Test::Upload.new(field, "", "application/octet-stream", "")
1284
+ res = client.patch_multipart("/<<plural>>/#{record.id}", <<model>>_fields(AppTest.form_token(client)), [empty])
1285
+ assert_response res, :see_other
1286
+ record = <<Model>>.find(record.id)
1287
+ assert_equal key, record.<<field>>_key
1288
+ assert Cybertrain::BlobStore.exist?(key)
1289
+ end
1290
+
1291
+ RUBY
1292
+
1293
+ TEST_AUTH_DOWNLOAD = <<~'RUBY'
1294
+ test "the download needs a signed-in user, like every other action of the controller" do
1295
+ client = new_client
1296
+ create_<<model>>(client, "<<field>>.png", "image/png", PNG_BYTES)
1297
+ record = <<Model>>.first
1298
+ anonymous = Cybertrain::Test::Client.new(APP)
1299
+ res = anonymous.get("/<<plural>>/#{record.id}/<<field>>")
1300
+ assert_redirected_to res, "/session/new"
1301
+ end
1302
+
1303
+ RUBY
1304
+
1305
+ TEST_PUBLIC_DOWNLOAD = <<~'RUBY'
1306
+ test "the download is public like show: a visitor gets the file" do
1307
+ client = new_client
1308
+ create_<<model>>(client, "<<field>>.png", "image/png", PNG_BYTES)
1309
+ record = <<Model>>.first
1310
+ anonymous = Cybertrain::Test::Client.new(APP)
1311
+ res = anonymous.get("/<<plural>>/#{record.id}/<<field>>")
1312
+ assert_response res, :ok
1313
+ assert_equal PNG_BYTES.bytesize, res.body.bytesize
1314
+ end
1315
+
1316
+ RUBY
1317
+
1318
+ TEST_TAIL = <<~'RUBY'
1319
+ Cybertrain::Test.run!
1320
+ RUBY
1321
+ end
1322
+ end
1323
+ end