Riaggiunta dei file caricati mancanti al database

I’ve got a site where it seems that a bunch of uploads have been removed from the uploads table in the database, but still exist in the filesystem. This leaves links to these broken. I’ve fixed a few by re-uploading files into a new topic to create the record in the DB (at the time I thought that the files were missing too, but @angus graciously pointed out why I wasn’t finding them–One day I’ll ask: why do we have both sha1 and base62 names for all of these assets?) and then re-baking the posts that include those uploads, like this:

def rebake_posts_with_uploads(topic_id, post_number)
  p = Post.find_by(topic_id: topic_id, post_number: post_number)
  exit unless p
  re = /upload:\/\/(.+?)\)/
  shas= p.raw.scan(re)

  shas.each do |sha|
    posts = Post.where("raw like '%#{sha[0]}%'")
    next unless posts
    puts "Found #{posts.count - 1} #{sha[0]}"
    posts.each do |post|
      next if post.id == p.id
      puts "rebake #{post.id}--#{BASE_URL}/t/-/#{post.topic_id}/#{post.post_number}"
      post.rebake!
    end
  end
end

My new plan, since it seems that the files exist in the file system but not in the database, is to do something like this:

for (all files in /shared/uploads/default/original/1x) do |file|
  unless file is in uploads table
     create upload record 
     for each post that includes that upload record
       rebake

Does that seem right? I’m looking at uploads.rake and don’t see anything that seems to do this already. This is sort of the opposite of

but instead of FileUtils.rm(file_path) I’d instead do an Upload.create, I think.

If this seems really stupid or there is a much better solution, I’d love to hear it before I go down this little rabbit hole.

Thanks.

I don’t know how this happened. I was hoping to pin the blame on a custom plugin, but I’m afraid that’s not the case. It may be related to another discussion, in which someone said:

3 Mi Piace

Yeah our history here with uploads is quite spotty, and it is our fault… @sam can you recommend someone to give a quick bit of advice?

2 Mi Piace

This can work, you just have to be careful to test on local … also look at 2x / 3x directories there are uploads everywhere.

3 Mi Piace

I’m trying something like this:

def add_missing_files_to_uploads
  public_directory = Rails.root.join("public").to_s
  db = RailsMultisite::ConnectionManagement.current_db
  uploads_directory = File.join(public_directory, 'uploads', db).to_s
  # uploads and optimized images
  missing = 0
  matched = 0
  Dir.glob("#{uploads_directory}/**/*.pdf").each do |file_path|
    sha1 = Upload.generate_digest(file_path)
    url = file_path.split(public_directory, 2)[1]
    if (Upload.where(sha1: sha1).empty? &&
        Upload.where(url: url).empty?)
      puts "MISSING #{file_path}" if DEBUG
      missing += 1
    else
      matched += 1
    end
  end
  puts "MISS: #{missing}. Match #{matched}"
end

Does that look sort of close? My first test seems to have failed, but I might have screwed something up.

Ciao @pfaffman. Stavo leggendo questo argomento poiché sembra una soluzione promettente per il mio problema qui. Sei riuscito a ottenere un buon risultato alla fine?

Scusa, non riesco a ricordarlo.

A me e a Sam sembra che funzionerà.

Ho appena risolto un problema del simulatore creando un post, inserendovi un link a tutte le immagini e poi rielaborando tutti i post che contengono transparent.gif.

Questa sembra una soluzione un po’ più elegante.

Direi di fare un backup solo del database e di provarci. Sono in vacanza, ma se hai un budget posso vedere cosa posso fare.

Anche io ho un cliente importante su ams3; non li ho ancora spostati su AWS, ma penso che accadrà presto. Ho un amico che lavorava per DigitalOcean e mi ha consigliato ams3 perché era il loro data center migliore ed era in gran parte sottoutilizzato (questo è stato molto tempo fa). Non è andato come speravo.

1 Mi Piace

Mi chiedo se questo sia il metodo migliore al giorno d’oggi?

Sto cercando di recuperare i caricamenti che si trovano sul filesystem ma non nel database dopo una migrazione S3 fallita (con il vecchio rake:s3_migrate).

Non c’è davvero un buon metodo. :crying_cat:

Ma, sì. Quella è ancora l’idea. Non credo che nulla sia cambiato in Discourse da allora. Di solito, ne farei un paio a mano per assicurarmi che faccia ciò che è previsto. Inoltre, fai prima un backup, e magari metti il sito in modalità di sola lettura in modo che se devi ripristinare il backup non dovrai buttare via nessun post fatto mentre stavi armeggiando con le cose.

Senza sapere parecchio di più di quanto sia facile comunicare in un forum non posso dire se quello sia davvero il modo migliore o se ci sia qualcosa di più semplice. Potresti essere in grado di fare semplicemente gsub sui percorsi degli URL, per esempio. Se hai un budget puoi contattarmi o chiedere in Marketplace.

1 Mi Piace

Ok, l’ho risolto con Claude e molti complimenti. Condivido quello che ho fatto per aiutare chiunque abbia un problema simile o identico.

Non sono sicuro che questo sia il metodo più intelligente e ottimale da usare, ma è quello che ha funzionato per me.

Per favore, fate attenzione e tenete presente che non sono un esperto, ma un principiante che impara sempre.

Il problema (da S3 a filesystem locale)

Dopo la migrazione da AWS S3 a filesystem locale, molte immagini venivano visualizzate come transparent.png. I file erano sempre su disco, ma Discourse non riusciva a risolverli.

La causa principale era una catena interrotta:

  1. Post con URL brevi upload:// (SHA1 codificato in base62).
  2. Database uploads che mappa SHA1 → percorso file locale.
  3. Filesystem che memorizza i file denominati con il loro hash SHA1,

La migrazione ha spostato i file su disco correttamente, ma non esistevano record DB uploads. Senza un record corrispondente, Discourse ripiega su transparent.png.

La soluzione (creare record e ribake)

Creare record di caricamento mancanti dai file orfani:

dir = Rails.root.join("public", "uploads", "default", "original")
created = 0

Dir.glob(dir.join("**", "*")).select { |f| File.file?(f) }.each do |path|
  sha = File.basename(path, File.extname(path))
  next if Upload.find_by(sha1: sha)

  ext = File.extname(path).delete(".")
  relative = path.sub("#{Rails.root}/public", "")

  u = Upload.new
  u.sha1 = sha
  u.url = relative
  u.original_filename = File.basename(path)
  u.filesize = File.size(path)
  u.extension = ext
  u.user_id = -1
  u.save!(validate: false)

  created += 1
  puts "Created upload #{u.id}: #{sha}"
end

puts "Total created: #{created}"

Rifare il bake dei post che fanno riferimento ai caricamenti ripristinati:

fixed_posts = 0

Upload.where(user_id: -1).find_each do |u|
  short = u.short_url
  next unless short

  Post.where("raw LIKE '%upload://%'").find_each do |p|
    urls = p.raw.scan(/upload:\/\/\[^\s\]\)]+/)
    urls.each do |url|
      decoded = Upload.sha1_from_short_url(url)
      if decoded == u.sha1
        p.rebake!
        fixed_posts += 1
        puts "Rebaked post #{p.id}"
        break
      end
    end
  end
end

puts "Total rebaked: #{fixed_posts}"

Rigenerare gli ottimizzati mancanti:

Dopo aver corretto i file originali, dobbiamo popolare i file ottimizzati (1X, 2X, ecc.).

rake uploads:regenerate_missing_optimized

Rollback sicuro (giusto per ogni evenienza)

Tutti i record creati utilizzano user_id: -1. Per annullare:

Upload.where(user_id: -1).delete_all

delete_all salta i callback quindi i file del filesystem non vengono toccati.

In precedenza ho usato destroy_all per errore e questo ha innescato i callback che hanno spostato i file nella cartella di rimozione (tombstone).

Ne ho recuperato uno individuale che ho usato per testare e ho riformulato il mio approccio.

3 Mi Piace