From 5dfb79c57acb69e360dd6ae52eadd2f829c68c01 Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Fri, 11 Oct 2024 13:35:41 +0300 Subject: [PATCH] Too large replays are now attached as .txt files --- im.inc.php | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++- plan.inc.php | 30 +++++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/im.inc.php b/im.inc.php index 13cfd05..676c485 100644 --- a/im.inc.php +++ b/im.inc.php @@ -8,6 +8,7 @@ interface IMChan { function post(string $message, array $fields = array()) : array; function updatePost(string $thread_id, array $fields = array()); + function postFile(string $title, string $mime_type, string $file_path, array $fields = array()) : array; function postPNG(string $title, string $png_file, array $fields = array()) : array; function getPermalink(string $id) : string; } @@ -24,6 +25,11 @@ class NullMessenger implements IMChan return array('ok' => true); } + function postFile(string $title, string $mime_type, string $file_path, array $fields = array()) : array + { + return array('ok' => true); + } + function postPNG(string $title, string $png_file, array $fields = array()) : array { return array('ok' => true); @@ -60,6 +66,11 @@ class MattermostChan implements IMChan return mm_update($this->server, $this->token, $id, $fields); } + function postFile(string $title, string $mime_type, string $file_path, array $fields = array()) : array + { + return mm_post_file($this->server, $this->token, $this->chan, $title, $mime_type, $file_path, $fields); + } + function postPNG(string $title, string $png_file, array $fields = array()) : array { return mm_post_png($this->server, $this->token, $this->chan, $title, $png_file, $fields); @@ -122,7 +133,49 @@ function mm_update(string $server, string $token, string $id, array $fields) return json_decode($result, true); } -function mm_post_png(string $server, string $token, string $chan, string $title, string $png_file, $msg_fields = array()) +function mm_post_file( + string $server, string $token, string $chan, + string $title, string $mime_type, string $file_path, + $msg_fields = array() +) +{ + $headers = array( + 'Content-Type: multipart/form-data', + 'Authorization: Bearer ' . $token + ); + + $file = new \CurlFile(realpath($file_path), $mime_type, basename($file_path)); + + $fields = array(); + $fields['channel_id'] = $chan; + $fields['files'] = $file; + + $ch = curl_init("$server/api/v4/files"); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + $result = curl_exec($ch); + $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + if($status != 201) + throw new Exception("File failed with status: " . $status .", result: " . var_export($result, true)); + + $result = json_decode($result, true); + $file_id = $result['file_infos'][0]['id']; + + $msg_fields['file_ids'] = array($file_id); + + return mm_post($server, $token, $chan, $title, $msg_fields); +} + +function mm_post_png( + string $server, string $token, string $chan, + string $title, string $png_file, + $msg_fields = array() +) { $headers = array( 'Content-Type: multipart/form-data', diff --git a/plan.inc.php b/plan.inc.php index 091fe18..84645b9 100644 --- a/plan.inc.php +++ b/plan.inc.php @@ -580,6 +580,30 @@ class Plan return $this->session->conf->im->post($msg, $args); } + function postFileData(string $title, string $mime_type, string $file_data, string $file_extension, array $args = array()) : bool + { + $args['root_id'] = $this->im_thread_id; + + if($file_data) + { + $tmp_file = tempnam(sys_get_temp_dir(), 'tmp_upload') . $file_extension; + taskman\ensure_write($tmp_file, $file_data); + try + { + $this->session->conf->im->postFile($title, $mime_type, $tmp_file, $args); + return true; + } + catch(Exception $e) + {} + finally + { + taskman\ensure_rm($tmp_file); + } + } + + return false; + } + function _analyzeExtStatusItemAsync(Task $task, array $item) : Amp\Promise { return Amp\call(function() use($task, $item) { @@ -652,7 +676,11 @@ class Plan function _postReplayToMessenger(Task $task, $repl_txt) { $repl_txt = _try_lz4_replay($repl_txt); - $this->post("Last Replay: \n```\n$repl_txt\n```\n *{$task->device}*"); + + if(strlen($repl_txt) > 3000) + $this->postFileData("Replay File: *{$task->device}*", 'text/plain', $repl_txt, '.txt'); + else + $this->post("Last Replay: \n```\n$repl_txt\n```\n *{$task->device}*"); } function _reportErrorFromLogcatToMessenger(Task $task, $limit)