ustore.rb 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #!/usr/bin/ruby
  2. #
  3. # This file is part of centurio.work/ing/commands.
  4. #
  5. # centurio.work/ing/commands is free software: you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or (at your
  8. # option) any later version.
  9. #
  10. # centurio.work/ing/commands is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  13. # Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along with
  16. # centurio.work/ing/commands (file COPYING in the main directory). If not, see
  17. # <http://www.gnu.org/licenses/>.
  18. require 'rubygems'
  19. require 'json'
  20. require 'riddl/server'
  21. require 'riddl/client'
  22. require 'fileutils'
  23. require 'nokogiri'
  24. class GetFolders < Riddl::Implementation
  25. def response
  26. Dir.chdir( __dir__ + '/../storage')
  27. i = 1
  28. while i < @r.length do
  29. Dir.chdir(@r[i])
  30. i +=1
  31. end
  32. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(Dir.glob('*/').sort_by{|x| x.downcase}))
  33. end
  34. end
  35. class CreateFolder < Riddl::Implementation
  36. def response
  37. path = File.join(File.dirname(__dir__),'storage/', @r.drop(1).join("/"))
  38. FileUtils.mkdir_p(path)
  39. end
  40. end
  41. class DeleteFolder < Riddl::Implementation
  42. def response
  43. path = File.join(File.dirname(__dir__),'storage/', @r.drop(1).join("/"))
  44. FileUtils.rm_rf(path)
  45. end
  46. end
  47. class GetAllFolders < Riddl::Implementation
  48. def response
  49. Dir.chdir( __dir__ + '/../storage')
  50. i = 1
  51. while i < @r.length do
  52. Dir.chdir(@r[i])
  53. i +=1
  54. end
  55. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(Dir.glob('**/*/').sort_by{|x| x.downcase}))
  56. end
  57. end
  58. class GetImages < Riddl::Implementation
  59. def response
  60. Dir.chdir( __dir__ + '/../storage')
  61. i = 1
  62. while i < @r.length do
  63. Dir.chdir(@r[i])
  64. i +=1
  65. end
  66. ret = [];
  67. ret2 = [];
  68. Dir.glob('*').select{ |e|
  69. if File.symlink?(e)
  70. ret2.append(e)
  71. elsif( File.file?(e))
  72. ret.append(e)
  73. end
  74. }
  75. ret = ret.sort_by{|x| x.downcase}
  76. ret2 = ret2.sort_by{|x| x.downcase}
  77. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret + ret2))
  78. end
  79. end
  80. class UploadData < Riddl::Implementation
  81. def response
  82. i = 0
  83. while i < @p.length do
  84. item = @p[i]
  85. if(item != nil && item.name == "files[]")
  86. #puts item.inspect
  87. #get filename from additional info as .filname is not alwys right?
  88. readFilename = item.additional.inspect.to_s[/filename=\\"(.*?)\\"/m, 1]
  89. path = File.join(File.dirname(__dir__),'storage/', @r.drop(1).join("/"))
  90. FileUtils.mkdir_p(path)
  91. readFile = File.read(item.value.inspect.to_s[/Tempfile:(.*?)>/m, 1])
  92. File.open(File.join(path, readFilename), 'wb') do |file|
  93. file.write(readFile.to_s)
  94. end
  95. end
  96. i +=1
  97. end
  98. end
  99. end
  100. class CreateSymlink < Riddl::Implementation
  101. def response
  102. datalink = File.join(File.dirname(__dir__),'storage/', @r.drop(1).join("/"))
  103. datalink2 = datalink
  104. data = JSON.parse(@p[0].value.read)
  105. #replace last oldfilename with new one
  106. datalink2 = datalink.reverse.sub(data["oldfilename"].reverse, data["filename"].reverse).reverse
  107. #puts datalink
  108. #puts datalink2
  109. File.symlink(datalink, datalink2)
  110. end
  111. end
  112. class DeleteData < Riddl::Implementation
  113. def response
  114. datalink = File.join(File.dirname(__dir__),'storage/', @r.drop(1).join("/"))
  115. puts datalink
  116. File.delete(datalink) if File.exist?(datalink)
  117. File.delete(datalink) if File.symlink?(datalink)
  118. end
  119. end
  120. class NewExternalFolder < Riddl::Implementation
  121. def response
  122. parsed = JSON.parse(@p[0].value.read)
  123. found = false;
  124. path = File.join(File.dirname(__dir__),'server/ustore_Folders.txt')
  125. text = File::readlines(path)
  126. text.each do |line|
  127. if line.chomp == parsed["folder"]
  128. found = true
  129. end
  130. end
  131. if !found
  132. File.open(path, 'a') do |file|
  133. file.puts parsed["folder"]
  134. end
  135. end
  136. end
  137. end
  138. class GetAllExternalFolders < Riddl::Implementation
  139. def response
  140. path = File.join(File.dirname(__dir__),'server/ustore_Folders.txt')
  141. text = File::readlines(path)
  142. text.each(&:chomp)
  143. puts text
  144. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(text))
  145. end
  146. end
  147. server = Riddl::Server.new(File.join(__dir__,'/ustore.xml'), :host => 'localhost') do |opts|
  148. accessible_description true
  149. cross_site_xhr true
  150. on resource do
  151. on resource 'folders' do
  152. run GetFolders if get
  153. run CreateFolder if post
  154. run DeleteFolder if delete
  155. on resource '.*' do
  156. run GetFolders if get
  157. run CreateFolder if post
  158. run DeleteFolder if delete
  159. end
  160. end
  161. on resource 'allfolders' do
  162. run GetAllFolders if get
  163. on resource '.*' do
  164. run GetAllFolders if get
  165. end
  166. end
  167. on resource 'images' do
  168. run GetImages if get
  169. on resource '.*' do
  170. run GetImages if get
  171. end
  172. end
  173. on resource 'data' do
  174. run UploadData if post
  175. run DeleteData if delete
  176. run CreateSymlink if post 'list'
  177. on resource '.*' do
  178. run UploadData if post
  179. run CreateSymlink if post 'list'
  180. run DeleteData if delete
  181. end
  182. end
  183. on resource 'externalFolder' do
  184. run NewExternalFolder if post
  185. run GetAllExternalFolders if get
  186. end
  187. end
  188. end.loop!