ustore.rb 5.5 KB

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