mirror of
https://github.com/neocities/neocities.git
synced 2025-07-25 11:58:28 +02:00
code to delete thumbnails and screenshots
This commit is contained in:
parent
b0e1a47e19
commit
0a636b2397
3 changed files with 44 additions and 8 deletions
|
@ -52,6 +52,9 @@ class Site < Sequel::Model
|
||||||
LOSSLESS_IMAGE_REGEX = /png|bmp|gif/
|
LOSSLESS_IMAGE_REGEX = /png|bmp|gif/
|
||||||
LOSSY_IMAGE_REGEX = /jpg|jpeg/
|
LOSSY_IMAGE_REGEX = /jpg|jpeg/
|
||||||
HTML_REGEX = /htm|html/
|
HTML_REGEX = /htm|html/
|
||||||
|
|
||||||
|
SCREENSHOT_RESOLUTIONS = ['235x141', '105x63', '270x162']
|
||||||
|
THUMBNAIL_RESOLUTIONS = ['105x63']
|
||||||
|
|
||||||
many_to_one :server
|
many_to_one :server
|
||||||
|
|
||||||
|
@ -180,7 +183,7 @@ class Site < Sequel::Model
|
||||||
FileUtils.mv uploaded.path, file_path(filename)
|
FileUtils.mv uploaded.path, file_path(filename)
|
||||||
File.chmod(0640, file_path(filename))
|
File.chmod(0640, file_path(filename))
|
||||||
|
|
||||||
ext = File.extname(filename).gsub('.', '')
|
ext = File.extname(filename).gsub(/^./, '')
|
||||||
|
|
||||||
if ext.match HTML_REGEX
|
if ext.match HTML_REGEX
|
||||||
ScreenshotWorker.perform_async values[:username], filename
|
ScreenshotWorker.perform_async values[:username], filename
|
||||||
|
@ -218,8 +221,13 @@ class Site < Sequel::Model
|
||||||
begin
|
begin
|
||||||
FileUtils.rm file_path(filename)
|
FileUtils.rm file_path(filename)
|
||||||
rescue Errno::ENOENT
|
rescue Errno::ENOENT
|
||||||
return false
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
ext = File.extname(filename).gsub(/^./, '')
|
||||||
|
|
||||||
|
screenshots_delete(filename) if ext.match HTML_REGEX
|
||||||
|
thumbnails_delete(filename) if ext.match IMAGE_REGEX
|
||||||
|
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -369,6 +377,28 @@ class Site < Sequel::Model
|
||||||
values[:title] || values[:username]
|
values[:title] || values[:username]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def screenshots_delete(filename)
|
||||||
|
SCREENSHOT_RESOLUTIONS.each do |res|
|
||||||
|
begin
|
||||||
|
FileUtils.rm screenshot_path(filename, res)
|
||||||
|
rescue Errno::ENOENT
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def thumbnails_delete(filename)
|
||||||
|
THUMBNAIL_RESOLUTIONS.each do |res|
|
||||||
|
begin
|
||||||
|
FileUtils.rm thumbnail_path(filename, res)
|
||||||
|
rescue Errno::ENOENT
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def screenshot_path(filename, resolution)
|
||||||
|
File.join(SCREENSHOTS_ROOT, values[:username], "#{filename}.#{resolution}.jpg")
|
||||||
|
end
|
||||||
|
|
||||||
def screenshot_exists?(filename, resolution)
|
def screenshot_exists?(filename, resolution)
|
||||||
File.exist? File.join(SCREENSHOTS_ROOT, values[:username], "#{filename}.#{resolution}.jpg")
|
File.exist? File.join(SCREENSHOTS_ROOT, values[:username], "#{filename}.#{resolution}.jpg")
|
||||||
end
|
end
|
||||||
|
@ -377,9 +407,17 @@ class Site < Sequel::Model
|
||||||
"#{SCREENSHOTS_URL_ROOT}/#{values[:username]}/#{filename}.#{resolution}.jpg"
|
"#{SCREENSHOTS_URL_ROOT}/#{values[:username]}/#{filename}.#{resolution}.jpg"
|
||||||
end
|
end
|
||||||
|
|
||||||
def thumbnail_exists?(filename, resolution)
|
def thumbnail_path(filename, resolution)
|
||||||
ext = File.extname(filename).gsub('.', '').match(LOSSY_IMAGE_REGEX) ? 'jpg' : 'png'
|
ext = File.extname(filename).gsub('.', '').match(LOSSY_IMAGE_REGEX) ? 'jpg' : 'png'
|
||||||
File.exist?(File.join(THUMBNAILS_ROOT, values[:username], "#{filename}.#{resolution}.#{ext}"))
|
File.join THUMBNAILS_ROOT, values[:username], "#{filename}.#{resolution}.#{ext}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def thumbnail_exists?(filename, resolution)
|
||||||
|
File.exist? thumbnail_path(filename, resolution)
|
||||||
|
end
|
||||||
|
|
||||||
|
def thumbnail_delete(filename, resolution)
|
||||||
|
File.rm thumbnail_path(filename, resolution)
|
||||||
end
|
end
|
||||||
|
|
||||||
def thumbnail_url(filename, resolution)
|
def thumbnail_url(filename, resolution)
|
||||||
|
|
|
@ -27,7 +27,6 @@ module Phantomjs
|
||||||
end
|
end
|
||||||
|
|
||||||
class ScreenshotWorker
|
class ScreenshotWorker
|
||||||
REQUIRED_RESOLUTIONS = ['235x141', '105x63', '270x162']
|
|
||||||
SCREENSHOTS_PATH = File.join DIR_ROOT, 'public', 'site_screenshots'
|
SCREENSHOTS_PATH = File.join DIR_ROOT, 'public', 'site_screenshots'
|
||||||
include Sidekiq::Worker
|
include Sidekiq::Worker
|
||||||
sidekiq_options queue: :screenshots, retry: 3, backtrace: true
|
sidekiq_options queue: :screenshots, retry: 3, backtrace: true
|
||||||
|
@ -82,7 +81,7 @@ class ScreenshotWorker
|
||||||
user_screenshots_path = File.join SCREENSHOTS_PATH, username
|
user_screenshots_path = File.join SCREENSHOTS_PATH, username
|
||||||
FileUtils.mkdir_p user_screenshots_path
|
FileUtils.mkdir_p user_screenshots_path
|
||||||
|
|
||||||
REQUIRED_RESOLUTIONS.each do |res|
|
Site::SCREENSHOT_RESOLUTIONS.each do |res|
|
||||||
img.scale(*res.split('x').collect {|r| r.to_i}).write(File.join(user_screenshots_path, "#{filename}.#{res}.jpg")) {
|
img.scale(*res.split('x').collect {|r| r.to_i}).write(File.join(user_screenshots_path, "#{filename}.#{res}.jpg")) {
|
||||||
self.quality = 90
|
self.quality = 90
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
require 'RMagick'
|
require 'RMagick'
|
||||||
|
|
||||||
class ThumbnailWorker
|
class ThumbnailWorker
|
||||||
REQUIRED_RESOLUTIONS = ['105x63']
|
|
||||||
THUMBNAILS_PATH = File.join DIR_ROOT, 'public', 'site_thumbnails'
|
THUMBNAILS_PATH = File.join DIR_ROOT, 'public', 'site_thumbnails'
|
||||||
include Sidekiq::Worker
|
include Sidekiq::Worker
|
||||||
sidekiq_options queue: :thumbnails, retry: 3, backtrace: true
|
sidekiq_options queue: :thumbnails, retry: 3, backtrace: true
|
||||||
|
@ -14,7 +13,7 @@ class ThumbnailWorker
|
||||||
user_thumbnails_path = File.join THUMBNAILS_PATH, username
|
user_thumbnails_path = File.join THUMBNAILS_PATH, username
|
||||||
FileUtils.mkdir_p user_thumbnails_path
|
FileUtils.mkdir_p user_thumbnails_path
|
||||||
|
|
||||||
REQUIRED_RESOLUTIONS.each do |res|
|
Site::THUMBNAIL_RESOLUTIONS.each do |res|
|
||||||
resimg = img.resize_to_fit(*res.split('x').collect {|r| r.to_i})
|
resimg = img.resize_to_fit(*res.split('x').collect {|r| r.to_i})
|
||||||
format = File.extname(filename).gsub('.', '')
|
format = File.extname(filename).gsub('.', '')
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue