From a2137e1f8b9cd90f2317a6be9bef32d3875a987d Mon Sep 17 00:00:00 2001 From: Wiktor Przybylski Date: Sun, 5 Mar 2023 23:56:28 +0100 Subject: [PATCH] lol update --- Gemfile.lock | 55 +++++++++++++++++++++ assets/dead.svg | 1 + assets/glider.svg | 1 + assets/hswro.svg | 51 +++++++++++++++++++ assets/money.svg | 1 + assets/person.svg | 4 ++ assets/warning.svg | 32 ++++++++++++ main.rb | 119 +++++++++++++++++++++++++++++++++++++++++---- 8 files changed, 254 insertions(+), 10 deletions(-) create mode 100644 assets/dead.svg create mode 100644 assets/glider.svg create mode 100755 assets/hswro.svg create mode 100644 assets/money.svg create mode 100644 assets/person.svg create mode 100644 assets/warning.svg diff --git a/Gemfile.lock b/Gemfile.lock index 3d5400b..8450371 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -11,14 +11,42 @@ GEM specs: addressable (2.7.0) public_suffix (>= 2.0.2, < 5.0) + celluloid (0.18.0) + timers (~> 4) + celluloid-io (0.17.3) + celluloid (>= 0.17.2) + nio4r (>= 1.1) + timers (>= 4.1.1) chunky_png (1.3.11) color (1.8) css_parser (1.7.1) addressable + daemons (1.4.1) + domain_name (0.5.20190701) + unf (>= 0.0.5, < 1.0.0) + eventmachine (1.2.7) excon (0.73.0) + ffi (1.15.5) + ffi-compiler (1.0.1) + ffi (>= 1.0.0) + rake + http (5.0.1) + addressable (~> 2.3) + http-cookie (~> 1.0) + http-form_data (~> 2.2) + llhttp-ffi (~> 0.3.0) + http-cookie (1.0.4) + domain_name (~> 0.5) + http-form_data (2.3.0) + http_parser.rb (0.8.0) json (2.3.0) + llhttp-ffi (0.3.1) + ffi-compiler (~> 1.0) + rake (~> 13.0) + matrix (0.4.2) mustermann (1.1.1) ruby2_keywords (~> 0.0.1) + nio4r (2.5.8) pdf-core (0.7.0) prawn (2.2.2) pdf-core (~> 0.7.0) @@ -27,9 +55,18 @@ GEM css_parser (~> 1.6) prawn (>= 0.11.1, < 3) public_suffix (4.0.5) + puma (5.6.4) + nio4r (~> 2.0) rack (2.2.2) rack-protection (2.0.8.1) rack + rake (13.0.6) + reel (0.6.1) + celluloid (>= 0.15.1) + celluloid-io (>= 0.15.0) + http (>= 0.6.0.pre) + http_parser.rb (>= 0.6.0) + websocket-driver (>= 0.5.1) rmagick (4.1.2) rqrcode (1.1.2) chunky_png (~> 1.0) @@ -41,8 +78,20 @@ GEM rack (~> 2.0) rack-protection (= 2.0.8.1) tilt (~> 2.0) + thin (1.8.1) + daemons (~> 1.0, >= 1.0.9) + eventmachine (~> 1.0, >= 1.0.4) + rack (>= 1, < 3) tilt (2.0.10) + timers (4.3.3) ttfunk (1.6.2.1) + unf (0.1.4) + unf_ext + unf_ext (0.0.8.1) + webrick (1.7.0) + websocket-driver (0.7.5) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.5) PLATFORMS ruby @@ -50,12 +99,18 @@ PLATFORMS DEPENDENCIES color excon + http json + matrix prawn prawn-qrcode! prawn-svg + puma + reel rmagick sinatra + thin + webrick BUNDLED WITH 1.17.2 diff --git a/assets/dead.svg b/assets/dead.svg new file mode 100644 index 0000000..d5b858e --- /dev/null +++ b/assets/dead.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/assets/glider.svg b/assets/glider.svg new file mode 100644 index 0000000..4e6c879 --- /dev/null +++ b/assets/glider.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/assets/hswro.svg b/assets/hswro.svg new file mode 100755 index 0000000..4738929 --- /dev/null +++ b/assets/hswro.svg @@ -0,0 +1,51 @@ + +image/svg+xml + + + + + + diff --git a/assets/money.svg b/assets/money.svg new file mode 100644 index 0000000..6dcd317 --- /dev/null +++ b/assets/money.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/assets/person.svg b/assets/person.svg new file mode 100644 index 0000000..4096627 --- /dev/null +++ b/assets/person.svg @@ -0,0 +1,4 @@ + + + + diff --git a/assets/warning.svg b/assets/warning.svg new file mode 100644 index 0000000..ffc5471 --- /dev/null +++ b/assets/warning.svg @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + diff --git a/main.rb b/main.rb index 84cf200..6acb81f 100644 --- a/main.rb +++ b/main.rb @@ -37,7 +37,7 @@ module Excon end end -BACKEND_URL = 'http://spejstore:8000/api/1/' +BACKEND_URL = 'http://192.168.75.155:8000/api/1/' CODE_PREFIX = "HTTP://I/" def api(uri) @@ -70,8 +70,12 @@ end DYMO_LABEL_SIZE = [89, 36] ZEBRA_LABEL_SIZE = [50, 30] -def render_label(item_or_label_id, size: ZEBRA_LABEL_SIZE) - item = api("items/#{item_or_label_id}") +def render_label(dict_or_api, is_qr, item_or_label_id, size: ZEBRA_LABEL_SIZE) + if dict_or_api + item = item_or_label_id + else + item = api("items/#{item_or_label_id}") + end pdf = Prawn::Document.new(page_size: size.map { |x| mm2pt(x) }, margin: [2, 2, 2, 6].map { |x| mm2pt(x) }) do @@ -89,9 +93,12 @@ def render_label(item_or_label_id, size: ZEBRA_LABEL_SIZE) # Right side bounding_box([bounds.right - qr_size, bounds.top], width: qr_size) do - print_qr_code CODE_PREFIX + item['short_id'], stroke: false, - foreground_color: '000000', - extent: bounds.width, margin: 0, pos: bounds.top_left + + if is_qr + print_qr_code CODE_PREFIX + item['short_id'], stroke: false, + foreground_color: '000000', + extent: bounds.width, margin: 0, pos: bounds.top_left + end owner_text = item["owner"] ? "owner: #{item['owner']}\n\n" : "" metadata_text = owner_text # todo: creation date? @@ -111,17 +118,43 @@ def render_label(item_or_label_id, size: ZEBRA_LABEL_SIZE) pdf.render end +def render_hack(data, size: ZEBRA_LABEL_SIZE) + pdf = Prawn::Document.new(page_size: size.map { |x| mm2pt(x) }, + margin: [2, 2, 2, 6].map { |x| mm2pt(x) }) do + font_families.update("DejaVuSans" => { + normal: "fonts/DejaVuSans.ttf", + italic: "fonts/DejaVuSans-Oblique.ttf", + bold: "fonts/DejaVuSans-Bold.ttf", + bold_italic: "fonts/DejaVuSans-BoldOblique.ttf" + }) + font 'DejaVuSans' + + image_size = [bounds.height / 2, 27].max + svg IO.read(data['image']), width: image_size, position: :right + bounding_box(bounds.top_left, width: bounds.width - image_size) do + text_box data['text'], + size: 40, align: :center, valign: :center, width: bounds.width-10, + inline_format: true, overflow: :shrink_to_fit, disable_wrap_by_char: true + end + + + + end + + pdf.render +end + set :bind, '0.0.0.0' get '/api/1/preview/:id.pdf' do headers["Content-Type"] = "application/pdf; charset=utf8" - render_label params["id"] + render_label(false,true,params["id"]) end get '/api/1/preview/:id.png' do headers["Content-Type"] = "image/png" img = Magick::ImageList.new() - img = img.from_blob(render_label(params["id"])){ self.density = 200 }.first + img = img.from_blob(render_label(false,true,params["id"])){ self.density = 200 }.first img.format = 'png' img.background_color = 'white' img.to_blob @@ -129,7 +162,73 @@ end post '/api/1/print/:id' do temp = Tempfile.new('labelmaker') - temp.write(render_label(params["id"])) + temp.write(render_label(false,true, params["id"])) temp.close - system("lpr -P DYMO_LabelWriter_450 #{temp.path}") + system("lpr -P Zebra #{temp.path}") +end + +get '/api/1/testprint' do + test = {"owner" => "grzegorz_brzęczyszczykiewicz", "short_id" => "wszczebrzeszyniechrzaszczbrzmiwtrzcinie", "name" => "pchnąć w tę łódź jeża lub ośm skrzyń fig"} + temp = Tempfile.new('labelmaker') + temp.write(render_label(true,true,test)) + temp.close + system("lpr -P Zebra #{temp.path}") +end + +get '/api/1/testshow' do + test = {"owner" => "grzegorz_brzęczyszczykiewicz", "short_id" => "wszczebrzeszyniechrzaszczbrzmiwtrzcinie", "name" => "pchnąć w tę łódź jeża lub ośm skrzyń fig"} + headers["Content-Type"] = "application/pdf; charset=utf8" + render_label(true, true, test) +end + +templates = { + "prywatne" => { "text" => "Ten przedmiot jest własnością prywatną", "image" => "assets/person.svg"}, + "hsowe" => { "text" => "Ten przedmiot należy do HSWro", "image" => "assets/hswro.svg"}, + "hackuj" => { "text" => "Hackuj ile dusza zapragnie", "image" => "assets/glider.svg"}, + "zepsute" => { "text" => "Ten przedmiot jest zepsuty", "image" => "assets/dead.svg"}, + "eksploatuje" => { "text" => "Ten przedmiot eksploatuje materiały", "image" => "assets/money.svg"}, + "niehackuj" => { "text" => "Nie hackuj tego przedmiotu", "image" => "assets/glider.svg"}, + +# meme + "bhp" => { "text" => "Gdy ci smutno, gdy ci źle, użyj pasty BHP", "image" => "assets/hswro.svg"}, + +} + +get '/api/1/templates/preview/:id' do + headers["Content-Type"] = "application/pdf; charset=utf8" + render_hack(templates[params["id"]]) +end + +get '/api/1/templates/print/:id' do + temp = Tempfile.new('labelmaker') + temp.write(render_hack(templates[params["id"]])) + temp.close + system("lpr -P Zebra #{temp.path}") +end + +get '/api/1/nametag/preview/:id' do + headers["Content-Type"] = "application/pdf; charset=utf8" + render_hack({"text" => params['id'], "image" => "assets/hswro.svg"}) +end + +get '/api/1/nametag/print/:id' do + temp = Tempfile.new('labelmaker') + temp.write(render_hack({"text" => params['id'], "image" => "assets/hswro.svg"})) + temp.close + system("lpr -P Zebra #{temp.path}") +end + +get '/api/1/customitem/preview/:owner/:name' do + test = {"owner" => params["owner"], "short_id" => "test", "name" => params["name"]} + headers["Content-Type"] = "application/pdf; charset=utf8" + render_label(true,false,test) +end + + +get '/api/1/customitem/print/:owner/:name' do + test = {"owner" => params["owner"], "short_id" => "test", "name" => params["name"]} + temp = Tempfile.new('labelmaker') + temp.write(render_label(true,false,test)) + temp.close + system("lpr -P Zebra #{temp.path}") end