Skip to content

Commit

Permalink
Also store content-type for icon
Browse files Browse the repository at this point in the history
  • Loading branch information
joto committed Sep 13, 2020
1 parent df67783 commit b10074c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
22 changes: 17 additions & 5 deletions sources/projects/get_icons.rb
Expand Up @@ -63,11 +63,23 @@ def fetch(uri_str, limit = 10)
projects = database.execute("SELECT id, icon_url FROM projects WHERE status='OK' AND (icon_url LIKE 'http://%' OR icon_url LIKE 'https://%') ORDER BY id")

projects.each do |id, url|
puts " #{id} #{url}"
response = fetch(url)
if response.code == '200'
blob = SQLite3::Blob.new(response.body)
database.execute("UPDATE projects SET icon = ? WHERE id = ?", [ blob, id ])
begin
response = fetch(url)
if response.code == '200'
content_type = response['content-type'].force_encoding('UTF-8')
content_type.sub!(/ *;.*/, '')
if content_type =~ %r{^image/}
puts " #{id} #{url} #{content_type}"
image = SQLite3::Blob.new(response.body)
database.execute("UPDATE projects SET icon_type = ?, icon = ? WHERE id = ?", [ content_type, image, id ])
else
puts " #{id} #{url} ERROR content-type=#{content_type}"
end
else
puts " #{id} #{url} ERROR code=#{response.code}"
end
rescue
puts " #{id} #{url} ERROR"
end
end

Expand Down
1 change: 1 addition & 0 deletions sources/projects/pre.sql
Expand Up @@ -28,6 +28,7 @@ CREATE TABLE projects (
contact_name TEXT,
contact_email TEXT,
keywords TEXT,
icon_type TEXT,
icon BLOB,
key_entries INTEGER DEFAULT 0,
tag_entries INTEGER DEFAULT 0,
Expand Down
7 changes: 4 additions & 3 deletions web/lib/api/v4/project.rb
Expand Up @@ -67,10 +67,11 @@ class Taginfo < Sinatra::Base
:ui => '/projects/id_editor'
}) do
project_id = params[:project]
content_type :png
@db.select('SELECT icon FROM projects.projects').
res = @db.select('SELECT icon_type, icon FROM projects.projects').
condition('id = ?', project_id).
get_first_value()
execute()[0]
content_type res['icon_type']
res['icon']
end

end

0 comments on commit b10074c

Please sign in to comment.