ustore.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. path = File.join(File.dirname(__dir__),'storage/', @r.drop(1).join("/"))
  88. FileUtils.mkdir_p(path)
  89. readFile = File.read(item.value.inspect.to_s[/Tempfile:(.*?)>/m, 1])
  90. File.open(File.join(path, item.filename), 'wb') do |file|
  91. file.write(readFile.to_s)
  92. end
  93. end
  94. i +=1
  95. end
  96. end
  97. end
  98. class CreateSymlink < Riddl::Implementation
  99. def response
  100. datalink = File.join(File.dirname(__dir__),'storage/', @r.drop(1).join("/"))
  101. datalink2 = datalink
  102. data = JSON.parse(@p[0].value.read)
  103. #replace last oldfilename with new one
  104. datalink2 = datalink.reverse.sub(data["oldfilename"].reverse, data["filename"].reverse).reverse
  105. #puts datalink
  106. #puts datalink2
  107. File.symlink(datalink, datalink2)
  108. end
  109. end
  110. class DeleteData < Riddl::Implementation
  111. def response
  112. datalink = File.join(File.dirname(__dir__),'storage/', @r.drop(1).join("/"))
  113. puts datalink
  114. File.delete(datalink) if File.exist?(datalink)
  115. File.delete(datalink) if File.symlink?(datalink)
  116. end
  117. end
  118. class NewExternalFolder < Riddl::Implementation
  119. def response
  120. parsed = JSON.parse(@p[0].value.read)
  121. found = false;
  122. path = File.join(File.dirname(__dir__),'server/ustore_Folders.txt')
  123. text = File::readlines(path)
  124. text.each do |line|
  125. if line.chomp == parsed["folder"]
  126. found = true
  127. end
  128. end
  129. if !found
  130. File.open(path, 'a') do |file|
  131. file.puts parsed["folder"]
  132. end
  133. end
  134. end
  135. end
  136. class GetAllExternalFolders < Riddl::Implementation
  137. def response
  138. path = File.join(File.dirname(__dir__),'server/ustore_Folders.txt')
  139. text = File::readlines(path)
  140. text.each(&:chomp)
  141. puts text
  142. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(text))
  143. end
  144. end
  145. server = Riddl::Server.new(File.join(__dir__,'/ustore.xml'), :host => 'localhost') do |opts|
  146. accessible_description true
  147. cross_site_xhr true
  148. on resource do
  149. on resource 'folders' do
  150. run GetFolders if get
  151. run CreateFolder if post
  152. run DeleteFolder if delete
  153. on resource '.*' do
  154. run GetFolders if get
  155. run CreateFolder if post
  156. run DeleteFolder if delete
  157. end
  158. end
  159. on resource 'allfolders' do
  160. run GetAllFolders if get
  161. on resource '.*' do
  162. run GetAllFolders if get
  163. end
  164. end
  165. on resource 'images' do
  166. run GetImages if get
  167. on resource '.*' do
  168. run GetImages if get
  169. end
  170. end
  171. on resource 'data' do
  172. run UploadData if post
  173. run DeleteData if delete
  174. run CreateSymlink if post 'list'
  175. on resource '.*' do
  176. run UploadData if post
  177. run CreateSymlink if post 'list'
  178. run DeleteData if delete
  179. end
  180. end
  181. on resource 'externalFolder' do
  182. run NewExternalFolder if post
  183. run GetAllExternalFolders if get
  184. end
  185. end
  186. end.loop!