-2

I'm debugging carrierwave. It's fun. I think the problem comes down to this:

Inside carrierwave_direct, there's this code:

if record.new_record? && record.send("has_#{attribute}_upload?") && record.key !~ record.send(attribute).key_regexp

The #key_regexp code is this:

  /\A#{store_dir}\/[a-f\d\-]+\/.+\.(?i)#{extension_regexp}(?-i)\z/

which evaluates to this when I am debugging:

record.send(attribute).key_regexp
/\Astories\/\/main_images\/[a-f\d\-]+\/.+\.(?i)(jpg|jpeg|gif|png)(?-i)\z/

My store_dir is this:

"stories/#{model.id}/main_images"

and I think there's a blank "/" section because model.id is nil.

The record.key is this:

"/stories/main_imagescache/20160319-1549-2202-5826/blueapronimage.jpg"

Why doesn't it currently match?

It looks like the regex has an extra slash and I'm not sure what this section is:

[a-f\d\-]+\/.+\.(?i)(jpg|jpeg|gif|png)(?-i)

Is it any number of letters between a-f, digit, or "-", then a "/", then any number of letters or numbers, then a "."....

What is the (?i)?

What are capture groups?

What is the (?-i)?

I think this is the problem:

record.key !~ /\Astories/
true

It should be false.

Jwan622
  • 8,910
  • 11
  • 56
  • 125
  • 2
    Put your RegEx in [RegExr](http://regexr.com/) or [Regex101](https://regex101.com/). You can then hover over parts and it will tell you exactly what it does. They both also have an *Explain* section, explaining the RegEx for you. – Kaspar Lee Mar 19 '16 at 20:30

1 Answers1

0

Look at this, it explains it:

[a-f\d\-]+            # Matches a-f, any digit, - (hyphen)
                      # ^ One or more times (+)
\/                    # Matches a /
.+                    # Matches any character one or more times
\.                    # Matches a . (dot)
(?i)                  # States that anything after this is case-insesitive
                      # ^ Inline Flag
(jpg|jpeg|gif|png)    # Selects file extension
                      # ^ (either jpg, jpeg, gif, or png)
(?-i)                 # Removes Inline Flag

That should explain what each one does.

Also check out this Live Demo. Your example does seem to match, maybe another part of your code is incorrect...

Kaspar Lee
  • 4,918
  • 4
  • 27
  • 52