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