summaryrefslogtreecommitdiff
path: root/config/mpv/scripts/subs2srs/platform/win.lua
blob: e40dd7a79b597ac3ddabda6098d1fff4aa64b7ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
--[[
Copyright: Ren Tatsumoto and contributors
License: GNU GPL, version 3 or later; http://www.gnu.org/licenses/gpl.html

Platform-specific functions for Windows.
]]

local mp = require('mp')
local h = require('helpers')
local utils = require('mp.utils')
local base64 = require('utils.base64')
local self = { windows = true, healthy = true, clip_util = "cmd", }
local tmp_files = {}

mp.register_event('shutdown', function()
    for _, file in ipairs(tmp_files) do
        os.remove(file)
    end
end)

self.tmp_dir = function()
    return os.getenv('TEMP')
end

self.copy_to_clipboard = function(text)
    local args = {
        "powershell", "-NoLogo", "-NoProfile", "-WindowStyle", "Hidden", "-Command",
        string.format(
                "Set-Clipboard ([Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('%s')))",
                base64.enc(text)
        )
    }
    return h.subprocess_detached(
            args,
            function()
            end
    )
end

self.gen_random_tmp_file_path = function()
    return utils.join_path(self.tmp_dir(), string.format('curl_tmp_%d.txt', math.random(10 ^ 9)))
end

self.gen_unique_tmp_file_path = function()
    local curl_tmpfile_path = self.gen_random_tmp_file_path()
    while h.file_exists(curl_tmpfile_path) do
        curl_tmpfile_path = self.gen_random_tmp_file_path()
    end
    return curl_tmpfile_path
end

self.curl_request = function(url, request_json, completion_fn)
    local curl_tmpfile_path = self.gen_unique_tmp_file_path()
    local handle = io.open(curl_tmpfile_path, "w")
    handle:write(request_json)
    handle:close()
    table.insert(tmp_files, curl_tmpfile_path)
    local args = {
        'curl',
        '-s',
        url,
        '-H',
        'Content-Type: application/json; charset=UTF-8',
        '-X',
        'POST',
        '--data-binary',
        table.concat { '@', curl_tmpfile_path }
    }
    return h.subprocess(args, completion_fn)
end

return self