Exemplo n.º 1
0
        async Task <bool> OnGetScriptSource(MessageId msg_id, string script_id, CancellationToken token)
        {
            if (!SourceId.TryParse(script_id, out var id))
            {
                return(false);
            }

            var src_file = (await LoadStore(msg_id, token)).GetFileById(id);

            try {
                var    uri    = new Uri(src_file.Url);
                string source = $"// Unable to find document {src_file.SourceUri}";

                using (var data = await src_file.GetSourceAsync(checkHash: false, token: token)) {
                    if (data.Length == 0)
                    {
                        return(false);
                    }

                    using (var reader = new StreamReader(data))
                        source = await reader.ReadToEndAsync();
                }
                SendResponse(msg_id, Result.OkFromObject(new { scriptSource = source }), token);
            } catch (Exception e) {
                var o = new {
                    scriptSource = $"// Unable to read document ({e.Message})\n" +
                                   $"Local path: {src_file?.SourceUri}\n" +
                                   $"SourceLink path: {src_file?.SourceLinkUri}\n"
                };

                SendResponse(msg_id, Result.OkFromObject(o), token);
            }
            return(true);
        }
Exemplo n.º 2
0
    internal async Task <bool> OnGetBreakableLines(MessageId msg_id, string script_id, CancellationToken token)
    {
        if (!SourceId.TryParse(script_id, out SourceId id))
        {
            return(false);
        }

        SourceFile src_file = (await LoadStore(msg_id, token)).GetFileById(id);

        await SendEvent(msg_id, "", JObject.FromObject(new { lines = src_file.BreakableLines.ToArray(), from = script_id }), token);

        return(true);
    }
Exemplo n.º 3
0
        async Task <bool> OnGetScriptSource(MessageId msg_id, string script_id, CancellationToken token)
        {
            if (!SourceId.TryParse(script_id, out var id))
            {
                return(false);
            }

            var src_file = GetContext(msg_id).Store.GetFileById(id);
            var res      = new StringWriter();

            try {
                var    uri    = new Uri(src_file.Url);
                string source = $"// Unable to find document {src_file.SourceUri}";

                if (uri.IsFile && File.Exists(uri.LocalPath))
                {
                    using (var f = new StreamReader(File.Open(uri.LocalPath, FileMode.Open))) {
                        await res.WriteAsync(await f.ReadToEndAsync());
                    }

                    source = res.ToString();
                }
                else if (src_file.SourceUri.IsFile && File.Exists(src_file.SourceUri.LocalPath))
                {
                    using (var f = new StreamReader(File.Open(src_file.SourceUri.LocalPath, FileMode.Open))) {
                        await res.WriteAsync(await f.ReadToEndAsync());
                    }

                    source = res.ToString();
                }
                else if (src_file.SourceLinkUri != null)
                {
                    var doc = await new WebClient().DownloadStringTaskAsync(src_file.SourceLinkUri);
                    await res.WriteAsync(doc);

                    source = res.ToString();
                }

                SendResponse(msg_id, Result.OkFromObject(new { scriptSource = source }), token);
            } catch (Exception e) {
                var o = new {
                    scriptSource = $"// Unable to read document ({e.Message})\n" +
                                   $"Local path: {src_file?.SourceUri}\n" +
                                   $"SourceLink path: {src_file?.SourceLinkUri}\n"
                };

                SendResponse(msg_id, Result.OkFromObject(o), token);
            }
            return(true);
        }
Exemplo n.º 4
0
    internal override async Task <bool> OnGetScriptSource(MessageId msg_id, string script_id, CancellationToken token)
    {
        if (!SourceId.TryParse(script_id, out SourceId id))
        {
            return(false);
        }

        SourceFile src_file = (await LoadStore(msg_id, token)).GetFileById(id);

        try
        {
            var    uri    = new Uri(src_file.Url);
            string source = $"// Unable to find document {src_file.SourceUri}";

            using (Stream data = await src_file.GetSourceAsync(checkHash: false, token: token))
            {
                if (data.Length == 0)
                {
                    return(false);
                }

                using (var reader = new StreamReader(data))
                    source = await reader.ReadToEndAsync(token);
            }
            await SendEvent(msg_id, "", JObject.FromObject(new { source, from = script_id }), token);
        }
        catch (Exception e)
        {
            var o = JObject.FromObject(new
            {
                source = $"// Unable to read document ({e.Message})\n" +
                         $"Local path: {src_file?.SourceUri}\n" +
                         $"SourceLink path: {src_file?.SourceLinkUri}\n",
                from = script_id
            });

            await SendEvent(msg_id, "", o, token);
        }
        return(true);
    }