Skip to content

Commit

Permalink
Implementation of ZIP and selection only
Browse files Browse the repository at this point in the history
  • Loading branch information
alexschreyer committed Mar 8, 2014
1 parent 250be3c commit f44c90e
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 78 deletions.
203 changes: 138 additions & 65 deletions as_sketchfab/as_sketchfab_uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
Website: http://www.alexschreyer.net/projects/sketchfab-uploader-plugin-for-sketchup/
Name : Sketchfab Uploader
Version: 1.7
Date : 2/18/2014
Version: 1.8
Date : TBD
Description : This plugin uploads the currently open model to Sketchfab.com
Expand Down Expand Up @@ -47,35 +47,79 @@
- Added new upload method
- Implemented multipart upload via new API
- Added option to open model after uploading
1.8:
- Uploads ZIPped models now
- Gives option to only upload selection if something has been selected.
- SketchUp material names are now preserved on upload.
Issues:
- For versions before SU 2014: the post_url function does not accept returned data.
Licenses:
multipart-post
==============
https://github.com/nicksieger/multipart-post
Credits:
Copyright (c) 2007-2013 Nick Sieger [email protected]
Clément (zqsd) for the DAE ZIP solution.
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the 'Software'), to deal in the Software without
restriction, including without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Licenses:
multipart-post
==============
https://github.com/nicksieger/multipart-post
Copyright (c) 2007-2013 Nick Sieger [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the 'Software'), to deal in the Software without
restriction, including without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7-zip:
======
7-Zip Command line version
~~~~~~~~~~~~~~~~~~~~~~~~~~
License for use and distribution
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7-Zip Copyright (C) 1999-2010 Igor Pavlov.
7za.exe is distributed under the GNU LGPL license
Notes:
You can use 7-Zip on any computer, including a computer in a commercial
organization. You don't need to register or pay for 7-Zip.
GNU LGPL information
--------------------
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You can receive a copy of the GNU Lesser General Public License from
http://www.gnu.org/
=end

Expand Down Expand Up @@ -106,40 +150,49 @@ module AS_SketchfabUploader
@zip_name = File.join(@user_dir,'temp_export.zip')

# Exporter options - doesn't work with KMZ export, though
@options_hash = { :triangulated_faces => true,
@options_hash = { :triangulated_faces => true,
:doublesided_faces => false,
:edges => true,
:materials_by_layer => false,
:author_attribution => true,
:texture_maps => true,
:selectionset_only => false,
:preserve_instancing => true }


# ========================
:preserve_instancing => true }

# clean old files if any
# Add the library path so Ruby can find it
$: << File.dirname(__FILE__)+'/lib'
require 'fileutils'
FileUtils.rm(@filename) if File.exists?(@filename)
FileUtils.rm_r(@asset_dir) if File.exists?(@asset_dir)
FileUtils.rm(@zip_name) if File.exists?(@zip_name)

# Load libraries for 2013 and 2014
require 'zip'


# ========================


def self.show_dialog_2013
# This uses a json approach to upload (for < SU 2014)

# Need to load the old Fileutils here
require 'fileutils-186'

# Allow for only selection upload if something is selected - reset var first
@options_hash[:selectionset_only] = false
if (Sketchup.active_model.selection.length > 0) then
res = UI.messagebox "Upload only selected geometry?", MB_YESNO
@options_hash[:selectionset_only] = true if (res == 6)
end

# Export model as KMZ
# Export model as DAE
if Sketchup.active_model.export @filename, @options_hash then


require 'zip'
# Create ZIP file
Zip.create(@zip_name, @filename, @asset_dir)

# Open file as binary and encode it as Base64
contents = open(@zip_name, "rb") {|io| io.read }
encdata = [contents].pack('m')

# Then delete the temporary files
# Then delete the temporary files - keep for debugging
# File.delete @zip_name

# Set up and show Webdialog
Expand Down Expand Up @@ -179,11 +232,18 @@ def self.show_dialog_2013
end

# Assemble JSON string
json = '{"contents":"' + encdata.split(/[\r\n]+/).join('\r\n') + '","filename":"temp.zip","title":"' + mytitle + '","description":"' + description + '","tags":"' + tags + '","token":"' + p + '","source":"sketchup-exporter"' + privString + '}'
json = '{"contents":"' + encdata.split(/[\r\n]+/).join('\r\n') + '","filename":"model.zip","title":"' + mytitle + '","description":"' + description + '","tags":"' + tags + '","token":"' + p + '","source":"sketchup-exporter"' + privString + '}'

# Submit data to Sketchfab - need to use old API with JSON
d.post_url("https://api.sketchfab.com/model", json)

# Then delete the temporary files
File.delete @zip_name if File.exists?(@zip_name)
File.delete @filename if File.exists?(@filename)
# FileUtils.rm(@zip_name) if File.exists?(@zip_name)
# FileUtils.rm(@filename) if File.exists?(@filename)
FileUtils.rm_r(@asset_dir) if File.exists?(@asset_dir)

defaults = Sketchup.write_default "Sketchfab", "api_token", p
d.execute_script('submitted()')

Expand All @@ -204,7 +264,7 @@ def self.show_dialog_2013
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<body>
<img src="~ + logo + %Q~" style="width:100%;" />
<img src="#{logo}" style="width:100%;" />
<p id="text">This dialog uploads the currently open model to Sketchfab.com. All fields marked with a * are mandatory.
You can get your API token from the <a href='http://sketchfab.com' title='http://sketchfab.com' target='_blank'>Sketchfab website</a> after registering there.</p>
<form id="SketchfabSubmit" name="SketchfabSubmit" action="">
Expand Down Expand Up @@ -287,12 +347,19 @@ def self.show_dialog_2013
def self.show_dialog_2014
# This uses the Ruby NET StdLibs instead of json


# Load Net and multipart post libraries
# Load Net and multipart post libraries for 2014
require 'net/http'
require 'json'
require 'net/http/post/multipart'
# Can load the new Fileutils here
require 'fileutils'

# Allow for only selection upload if something is selected - reset var first
@options_hash[:selectionset_only] = false
if (Sketchup.active_model.selection.length > 0) then
res = UI.messagebox "Upload only selected geometry?", MB_YESNO
@options_hash[:selectionset_only] = true if (res == 6)
end

# Set up and show Webdialog
dlg = UI::WebDialog.new('Sketchfab Uploader', false,'SketchfabUploader', 450, 520, 150, 150, true)
Expand Down Expand Up @@ -336,16 +403,17 @@ def self.show_dialog_2014

# Export model as KMZ and process
if Sketchup.active_model.export @filename, @options_hash then

# Create ZIP file
Zip.create(@zip_name, @filename, @asset_dir)

# Some feedback while we wait
d.execute_script('submitted()')

# Open file for multipart upload
encdata = UploadIO.new(File.new(@filename), "application/zip", "model.kmz")
upfile = File.new(@zip_name)
encdata = UploadIO.new(upfile, "application/zip", "model.zip")

# Then delete the temporary file
# File.delete @filename

# Submission URL
url = URI.parse('http://api.sketchfab.com/v1/models')

Expand All @@ -364,7 +432,7 @@ def self.show_dialog_2014
res = Net::HTTP.start(url.host, url.port) do |http|
http.request(req)
end
json = JSON.parse(res.body.gsub(/"/,"\""))
json = JSON.parse(res.body.gsub(/"/,"\""))

@success = json['success']
if @success then
Expand All @@ -374,7 +442,7 @@ def self.show_dialog_2014

d.close
# Give option to open uploaded model
result = UI.messagebox 'Open Sketchfab model in default browser?', MB_YESNO
result = UI.messagebox 'Open Sketchfab model in your browser?', MB_YESNO
UI.openURL "https://sketchfab.com/show/#{@model_id}" if result == 6

else
Expand All @@ -383,6 +451,14 @@ def self.show_dialog_2014
UI.messagebox "Sketchfab upload failed. Error: " + json['error']

end

# Then delete the temporary files
upfile.close
File.delete @zip_name if File.exists?(@zip_name)
File.delete @filename if File.exists?(@filename)
# FileUtils.rm(@zip_name) if File.exists?(@zip_name)
# FileUtils.rm(@filename) if File.exists?(@filename)
FileUtils.rm_r(@asset_dir) if File.exists?(@asset_dir)

# Save token for the next time
defaults = Sketchup.write_default "as_Sketchfab", "api_token", p
Expand Down Expand Up @@ -413,7 +489,7 @@ def self.show_dialog_2014
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<body>
<img src="~ + logo + %Q~" style="width:100%;" />
<img src="#{logo}" style="width:100%;" />
<p id="text">This dialog uploads the currently open model to Sketchfab.com. All fields marked with a * are mandatory.
You can get your API token from the <a href='http://sketchfab.com' title='http://sketchfab.com' target='_blank'>Sketchfab website</a> after registering there.</p>
<form id="SketchfabSubmit" name="SketchfabSubmit" action="">
Expand Down Expand Up @@ -484,22 +560,19 @@ def self.show_dialog_2014
# ========================


end # module


# ========================


# Create menu items
unless file_loaded?(__FILE__)

# Pick based on version
if Sketchup.version.to_f < 14 then
UI.menu("File").add_item("Upload to Sketchfab") {AS_SketchfabUploader::show_dialog_2013}
else
UI.menu("File").add_item("Upload to Sketchfab") {AS_SketchfabUploader::show_dialog_2014}
end

file_loaded(__FILE__)
# Create menu items
unless file_loaded?(__FILE__)

# Pick based on version
if Sketchup.version.to_f < 14 then
UI.menu("File").add_item("Upload to Sketchfab") {AS_SketchfabUploader::show_dialog_2013}
else
UI.menu("File").add_item("Upload to Sketchfab") {AS_SketchfabUploader::show_dialog_2014}
end

file_loaded(__FILE__)

end
end


end # module
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,6 @@ def cp_r(src, dest, options = {})
fu_check_options options, OPT_TABLE['cp_r']
fu_output_message "cp -r#{options[:preserve] ? 'p' : ''}#{options[:remove_destination] ? ' --remove-destination' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
return if options[:noop]
options = options.dup
options[:dereference_root] = true unless options.key?(:dereference_root)
fu_each_src_dest(src, dest) do |s, d|
copy_entry s, d, options[:preserve], options[:dereference_root], options[:remove_destination]
Expand Down Expand Up @@ -658,10 +657,10 @@ def rm_rf(list, options = {})
# removing directories. This requires the current process is the
# owner of the removing whole directory tree, or is the super user (root).
#
# WARNING: You must ensure that *ALL* parent directories cannot be
# moved by other untrusted users. For example, parent directories
# should not be owned by untrusted users, and should not be world
# writable except when the sticky bit set.
# WARNING: You must ensure that *ALL* parent directories are not
# world writable. Otherwise this method does not work.
# Only exception is temporary directory like /tmp and /var/tmp,
# whose permission is 1777.
#
# WARNING: Only the owner of the removing directory tree, or Unix super
# user (root) should invoke this method. Otherwise this method does not
Expand Down Expand Up @@ -704,11 +703,6 @@ def remove_entry_secure(path, force = false)
end
f.chown euid, -1
f.chmod 0700
unless fu_stat_identical_entry?(st, File.lstat(fullpath))
# TOC-to-TOU attack?
File.unlink fullpath
return
end
}
# ---- tree root is frozen ----
root = Entry_.new(path)
Expand Down Expand Up @@ -990,7 +984,6 @@ def fu_get_uid(user) #:nodoc:

def fu_get_gid(group) #:nodoc:
return nil unless group
group = group.to_s
if /\A\d+\z/ =~ group
then group.to_i
else Etc.getgrnam(group).gid
Expand Down Expand Up @@ -1027,7 +1020,7 @@ def touch(list, options = {})
created = nocreate = options[:nocreate]
t = options[:mtime]
if options[:verbose]
fu_output_message "touch #{nocreate ? '-c ' : ''}#{t ? t.strftime('-t %Y%m%d%H%M.%S ') : ''}#{list.join ' '}"
fu_output_message "touch #{nocreate ? ' -c' : ''}#{t ? t.strftime(' -t %Y%m%d%H%M.%S') : ''}#{list.join ' '}"
end
return if options[:noop]
list.each do |path|
Expand Down
3 changes: 2 additions & 1 deletion as_sketchfab/lib/zip.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Zip

USING_WINDOWS = (RUBY_PLATFORM =~/mswin/)
# Have to check for old and newer Windows name
USING_WINDOWS = ( RUBY_PLATFORM =~/mswin/ or RUBY_PLATFORM =~/mingw/ )
if USING_WINDOWS
SevenZip = File.join(File.dirname(__FILE__), '..', 'bin', '7za.exe')
end
Expand Down

0 comments on commit f44c90e

Please sign in to comment.