| [29] | 1 | //"php/content.php" |
|---|
| 2 | class Remotex { |
|---|
| 3 | constructor( filename ){ |
|---|
| 4 | this.filename = filename |
|---|
| [36] | 5 | // this.num = num |
|---|
| 6 | // this.part = part |
|---|
| [29] | 7 | } |
|---|
| 8 | |
|---|
| [41] | 9 | make_url_params0( arrayx ){ |
|---|
| [29] | 10 | let params = new URLSearchParams(); |
|---|
| 11 | arrayx.reduce(function(acc, element, index, array) { |
|---|
| 12 | acc.append(element[0], element[1]); |
|---|
| 13 | return acc; |
|---|
| 14 | }, params) |
|---|
| [41] | 15 | return params.toString(); |
|---|
| [29] | 16 | } |
|---|
| 17 | |
|---|
| [41] | 18 | make_url_params( arrayx , num ){ |
|---|
| 19 | let params = new URLSearchParams(); |
|---|
| 20 | for(let i=0; i<num; i++ ){ |
|---|
| 21 | params.append(arrayx[i][0], arrayx[i][1]) |
|---|
| 22 | } |
|---|
| 23 | const pa = params.toString() |
|---|
| 24 | return pa |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| [29] | 27 | async get_filelist_from_remote( num, part, func ) { |
|---|
| 28 | let param_array = [['cmd', 'get_filelistx'], ['num', num ], ['part', part ]] |
|---|
| [41] | 29 | await this.fetchx( this.filename , param_array, 3, func ) |
|---|
| [29] | 30 | } |
|---|
| 31 | |
|---|
| 32 | async update_filelist( num, part, func ) { |
|---|
| 33 | let param_array = [['cmd', 'update_filelistx'], ['num', num ], ['part', part ]] |
|---|
| [41] | 34 | await this.fetchx( this.filename , param_array, 3, func ) |
|---|
| [29] | 35 | } |
|---|
| 36 | |
|---|
| 37 | async get_content(num, part, path , func) { |
|---|
| 38 | let param_array = [['cmd', 'get_content'], ['num', num ], ['part', part ], ['path', path]] |
|---|
| [41] | 39 | await this.fetchx( this.filename , param_array, 4, func ) |
|---|
| [29] | 40 | } |
|---|
| 41 | |
|---|
| [36] | 42 | async get_output_url(num, func) { |
|---|
| 43 | let param_array = [['cmd', 'get_output_url'], ['num', num ] ] |
|---|
| [41] | 44 | await this.fetchx( this.filename , param_array, 2, func ) |
|---|
| [36] | 45 | } |
|---|
| 46 | |
|---|
| [41] | 47 | async fetchx( filename, param_array, num, func) { |
|---|
| 48 | let params = this.make_url_params( param_array , num ) |
|---|
| [29] | 49 | fetch(`${filename}?${params}`) |
|---|
| [41] | 50 | .then((response) => { return response.text() }) |
|---|
| 51 | .then((text) => { func(text) }) |
|---|
| 52 | .catch((error) => console.log("error=" + error)); |
|---|
| [29] | 53 | } |
|---|
| 54 | |
|---|
| [36] | 55 | upload_to_host(part) { |
|---|
| 56 | if( Globalx[part].key_indicate_file === true ){ |
|---|
| 57 | let textarea = $( Globalx[part].textarea_sel ) |
|---|
| [41] | 58 | this.upload_content(Globalx.num, part, Globalx[part].key, textarea.val()) |
|---|
| [36] | 59 | } |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| [41] | 62 | async build( cmd, num, part, path, content, func = null ) { |
|---|
| 63 | await this.upload_file( cmd, num, part, path, content, func = null ) |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | async upload_file(cmd, num, part, path, content, func = null) { |
|---|
| 67 | let form = document.createElement('form') |
|---|
| 68 | let req_cmd = document.createElement('input') |
|---|
| 69 | let req_num = document.createElement('input') |
|---|
| 70 | let req_part = document.createElement('input') |
|---|
| 71 | let req_path = document.createElement('input') |
|---|
| 72 | let req_content = document.createElement('input') |
|---|
| 73 | |
|---|
| 74 | document.body.appendChild(form) |
|---|
| 75 | |
|---|
| 76 | req_cmd.type = 'hidden' |
|---|
| 77 | req_cmd.name = 'cmd' |
|---|
| 78 | req_cmd.value = cmd |
|---|
| 79 | |
|---|
| 80 | req_num.type = 'hidden' |
|---|
| 81 | req_num.name = 'num' |
|---|
| 82 | req_num.value = num + '' |
|---|
| 83 | |
|---|
| 84 | req_part.type = 'hidden' |
|---|
| 85 | req_part.name = 'part' |
|---|
| 86 | req_part.value = part |
|---|
| 87 | |
|---|
| 88 | req_path.type = 'hidden' |
|---|
| 89 | req_path.name = 'path' |
|---|
| 90 | req_path.value = path |
|---|
| 91 | |
|---|
| 92 | req_content.type = 'hidden' |
|---|
| 93 | req_content.name = 'mytext' |
|---|
| 94 | req_content.value = content |
|---|
| 95 | |
|---|
| 96 | form.appendChild(req_cmd) |
|---|
| 97 | form.appendChild(req_num) |
|---|
| 98 | form.appendChild(req_part) |
|---|
| 99 | form.appendChild(req_path) |
|---|
| 100 | form.appendChild(req_content) |
|---|
| 101 | |
|---|
| 102 | // FormDataオブジェクトを作成する |
|---|
| 103 | var form_data = new FormData(form); |
|---|
| 104 | console.log("remotex.js filename=" + this.filename ) |
|---|
| 105 | fetch( this.filename , { |
|---|
| 106 | method: 'POST', |
|---|
| 107 | mode: 'same-origin', /* 'no-cors' 'cors' 'same-origin' */ |
|---|
| 108 | body: form_data |
|---|
| 109 | }) |
|---|
| 110 | .then((response) => { |
|---|
| 111 | return response.text() |
|---|
| 112 | }) |
|---|
| 113 | .then((data) => {console.log( "data=" + data) |
|---|
| 114 | if( func !== null ){ |
|---|
| 115 | func(data) |
|---|
| 116 | } |
|---|
| 117 | }) |
|---|
| 118 | .catch((error) => {console.log("error=" + error)}) |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| [36] | 121 | upload_content(num, part, path, content) { |
|---|
| 122 | let form = document.createElement('form'); |
|---|
| 123 | let req_content = document.createElement('input'); |
|---|
| 124 | let req_path = document.createElement('input'); |
|---|
| 125 | let req_cmd = document.createElement('input'); |
|---|
| 126 | let req_num = document.createElement('input'); |
|---|
| [41] | 127 | let req_part = document.createElement('input'); |
|---|
| [36] | 128 | |
|---|
| 129 | document.body.appendChild(form); |
|---|
| 130 | req_content.type = 'hidden'; |
|---|
| 131 | req_content.name = 'mytext'; |
|---|
| 132 | req_content.value = content; |
|---|
| 133 | |
|---|
| 134 | req_path.type = 'hidden'; |
|---|
| 135 | req_path.name = 'path'; |
|---|
| 136 | req_path.value = path; |
|---|
| 137 | |
|---|
| 138 | req_cmd.type = 'hidden'; |
|---|
| 139 | req_cmd.name = 'cmd'; |
|---|
| 140 | req_cmd.value = 'upload_content'; |
|---|
| 141 | |
|---|
| 142 | req_num.type = 'hidden'; |
|---|
| 143 | req_num.name = 'num'; |
|---|
| 144 | req_num.value = num + ''; |
|---|
| 145 | |
|---|
| [41] | 146 | req_part.type = 'hidden'; |
|---|
| 147 | req_part.name = 'part'; |
|---|
| 148 | req_part.value = part; |
|---|
| 149 | |
|---|
| [36] | 150 | form.appendChild(req_content); |
|---|
| 151 | form.appendChild(req_path); |
|---|
| 152 | form.appendChild(req_cmd); |
|---|
| 153 | form.appendChild(req_num); |
|---|
| [41] | 154 | form.appendChild(req_part); |
|---|
| [36] | 155 | |
|---|
| 156 | // FormDataオブジェクトを作成する |
|---|
| 157 | var form_data = new FormData(form); |
|---|
| [41] | 158 | console.log("remotex.js filename=" + this.filename ) |
|---|
| 159 | fetch( this.filename , { |
|---|
| [36] | 160 | method: 'POST', |
|---|
| 161 | mode: 'same-origin', /* 'no-cors' 'cors' 'same-origin' */ |
|---|
| 162 | body: form_data |
|---|
| 163 | }) |
|---|
| [41] | 164 | .then((response) => { return response.text()}) |
|---|
| 165 | .then((data) => {console.log( "data=" + data)}) |
|---|
| 166 | .catch((error) => {console.log("error=" + error)}); |
|---|
| [36] | 167 | } |
|---|
| 168 | |
|---|
| [29] | 169 | handleDownload(e) { |
|---|
| [36] | 170 | let dl = document.getElementById(`${e.data.part}-down-download`) |
|---|
| [29] | 171 | |
|---|
| 172 | if( Globalx[ e.data.part ].key_indicate_file === true ){ |
|---|
| 173 | this.get_content( e.data.num, e.data.part , Globalx[ e.data.part ].key , (content) => { |
|---|
| 174 | let blob = new Blob([ content ], { "type" : "text/plain" }); |
|---|
| 175 | |
|---|
| 176 | if (window.navigator.msSaveBlob) { |
|---|
| 177 | window.navigator.msSaveBlob(blob, Globalx[ e.data.part ].item_name); |
|---|
| 178 | |
|---|
| 179 | // msSaveOrOpenBlobの場合はファイルを保存せずに開ける |
|---|
| 180 | window.navigator.msSaveOrOpenBlob(blob, Globalx[ e.data.part ].item_name); |
|---|
| 181 | } else { |
|---|
| 182 | dl.download = Globalx[ e.data.part ].item_name |
|---|
| 183 | dl.href = window.URL.createObjectURL(blob); |
|---|
| 184 | } |
|---|
| 185 | }) |
|---|
| 186 | } |
|---|
| 187 | else{ |
|---|
| 188 | e.preventDefault() |
|---|
| 189 | dl.href = "" |
|---|
| 190 | dl.download = "" |
|---|
| 191 | } |
|---|
| 192 | } |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | function handleDownload(e) { |
|---|
| 196 | Globalx.remotex.handleDownload(e) |
|---|
| 197 | } |
|---|