Пример #1
0
        private static MemoryStream GetThumbnailFromProcess(Process p,
                                                            ref int width,
                                                            ref int height)
        {
            var lastPosition = 0L;

            using (var thumb = StreamManager.GetStream())
            {
                using (var pump = new StreamPump(
                           p.StandardOutput.BaseStream, thumb, 4096))
                {
                    pump.Pump(null);
                    while (!p.WaitForExit(20000))
                    {
                        if (lastPosition != thumb.Position)
                        {
                            lastPosition = thumb.Position;
                            continue;
                        }

                        p.Kill();
                        throw new ArgumentException("ffmpeg timed out");
                    }

                    if (p.ExitCode != 0)
                    {
                        throw new ArgumentException("ffmpeg does not understand the stream");
                    }
                    if (!pump.Wait(2000))
                    {
                        throw new ArgumentException("stream reading timed out");
                    }
                    if (thumb.Length == 0)
                    {
                        throw new ArgumentException("ffmpeg did not produce a result");
                    }

                    using (var img = Image.FromStream(thumb))
                    {
                        using (var scaled = ThumbnailMaker.ResizeImage(img, width, height,
                                                                       ThumbnailMakerBorder.Bordered))
                        {
                            width  = scaled.Width;
                            height = scaled.Height;
                            var rv = new MemoryStream();
                            try
                            {
                                scaled.Save(rv, ImageFormat.Jpeg);
                                return(rv);
                            }
                            catch (Exception)
                            {
                                rv.Dispose();
                                throw;
                            }
                        }
                    }
                }
            }
        }
Пример #2
0
    public ChannelReader <string> WatchStream(string streamName, CancellationToken token)
    {
        // TODO:
        // Allow client to stop watching a stream, or is that automatic if they cancel on the client (double check this)

        var stream = _streamManager.GetStream(streamName, token);

        return(stream);
    }
Пример #3
0
        private void ReadNext()
        {
            Method = null;
            Headers.Clear();
            hasHeaders = false;
            Body       = null;
            bodyBytes  = 0;
            readStream = StreamManager.GetStream();

            ++requestCount;
            State = HttpStates.ReadBegin;

            Read();
        }
        public bool Get([FromBody] LoginData data)
        {
            TwitterStream ts = streamManager.GetStream(data.Stream);

            if (ts != null)
            {
                bool res = streamManager.ChangeUserCredentials(data.Handle, data.Token, data.Stream);
                if (res)
                {
                    ts.Restart();
                    return(true);
                }
            }
            return(false);
        }
Пример #5
0
        public async Task <Stream> StoreStreamAsync(string address)
        {
            var stream = StreamManager.GetStream();
            var zip    = new ZipArchive(stream, ZipArchiveMode.Update, true);

            using (var zipSave = new ZipStoreService(zip))
            {
                var defaultStore = AppEngine.GetRequiredService <IStoreService>();
                var keeper       = ComicKeeper.FromDefault(address, zipSave, defaultStore);
                await keeper.StoreAsync();
            }
            await stream.FlushAsync();

            stream.Seek(0, SeekOrigin.Begin);
            return(stream);
        }
Пример #6
0
        private void ReadCallback(IAsyncResult result)
        {
            if (state == HttpStates.Closed)
            {
                return;
            }

            State = HttpStates.Reading;

            try {
                var read = stream.EndRead(result);
                if (read < 0)
                {
                    throw new HttpException("Client did not send anything");
                }
                DebugFormat("{0} - Read {1} bytes", this, read);
                readStream.Write(buffer, 0, read);
                lastActivity = DateTime.Now;
            }
            catch (Exception) {
                if (!IsATimeout)
                {
                    WarnFormat("{0} - Failed to read data", this);
                    Close();
                }
                return;
            }

            try {
                if (!hasHeaders)
                {
                    readStream.Seek(0, SeekOrigin.Begin);
                    var reader = new StreamReader(readStream);
                    for (var line = reader.ReadLine();
                         line != null;
                         line = reader.ReadLine())
                    {
                        line = line.Trim();
                        if (string.IsNullOrEmpty(line))
                        {
                            hasHeaders = true;
                            readStream = StreamManager.GetStream();
                            if (Headers.ContainsKey("content-length") &&
                                uint.TryParse(Headers["content-length"], out bodyBytes))
                            {
                                if (bodyBytes > 1 << 20)
                                {
                                    throw new IOException("Body too long");
                                }
                                var ascii = Encoding.ASCII.GetBytes(reader.ReadToEnd());
                                readStream.Write(ascii, 0, ascii.Length);
                                DebugFormat("Must read body bytes {0}", bodyBytes);
                            }
                            break;
                        }
                        if (Method == null)
                        {
                            var parts = line.Split(new[] { ' ' }, 3);
                            Method = parts[0].Trim().ToUpperInvariant();
                            Path   = parts[1].Trim();
                            DebugFormat("{0} - {1} request for {2}", this, Method, Path);
                        }
                        else
                        {
                            var parts = line.Split(new[] { ':' }, 2);
                            Headers[parts[0]] = Uri.UnescapeDataString(parts[1]).Trim();
                        }
                    }
                }
                if (bodyBytes != 0 && bodyBytes > readStream.Length)
                {
                    DebugFormat(
                        "{0} - Bytes to go {1}", this, bodyBytes - readStream.Length);
                    Read();
                    return;
                }
                using (readStream) {
                    Body = Encoding.UTF8.GetString(readStream.ToArray());
                    Debug(Body);
                    Debug(Headers);
                }
                SetupResponse();
            }
            catch (Exception ex) {
                Warn($"{this} - Failed to process request", ex);
                response = error500.HandleRequest(this);
                SendResponse();
            }
        }
Пример #7
0
 public static MemoryStream StartEncoding()
 {
     return(StreamManager.GetStream());
 }
Пример #8
0
 public static MemoryStream BorrowStream()
 {
     return(StreamManager.GetStream());
 }
Пример #9
0
        internal void MaybeStoreFile(BaseFile file)
        {
            if (connection == null)
            {
                return;
            }
            if (!file.GetType().Attributes.HasFlag(TypeAttributes.Serializable))
            {
                return;
            }
            try {
                using (var s = StreamManager.GetStream()) {
                    using (var c = StreamManager.GetStream()) {
                        var ctx = new StreamingContext(
                            StreamingContextStates.Persistence,
                            null
                            );
                        var formatter = new BinaryFormatter(null, ctx)
                        {
                            TypeFormat     = FormatterTypeStyle.TypesWhenNeeded,
                            AssemblyFormat = FormatterAssemblyStyle.Simple
                        };
                        formatter.Serialize(s, file);
                        Cover cover = null;
                        try {
                            cover = file.MaybeGetCover();
                            if (cover != null)
                            {
                                formatter.Serialize(c, cover);
                            }
                        }
                        catch (NotSupportedException) {
                            // Ignore and store null.
                        }

                        lock (connection) {
                            using (var trans = connection.BeginTransaction()) {
                                insertKey.Value  = file.Item.FullName;
                                insertSize.Value = file.Item.Length;
                                insertTime.Value = file.Item.LastWriteTimeUtc.Ticks;
                                insertData.Value = s.ToArray();

                                insertCover.Value = null;
                                if (cover != null)
                                {
                                    insertCover.Value = c.ToArray();
                                }
                                try {
                                    insert.Transaction = trans;
                                    insert.ExecuteNonQuery();
                                    trans.Commit();
                                }
                                catch (DbException ex) {
                                    Error("Failed to put file cover into store", ex);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex) {
                Error("Failed to serialize an object of type " + file.GetType(), ex);
                throw;
            }
        }
        public void ToggleSticky(long tweetId, string streamName)
        {
            TwitterStream ts = streamManager.GetStream(streamName);

            if (ts != null)
            {
                ts._tweetRepo.ToggleSticky(tweetId);
                ts.UpdateTweet(tweetId);
                TweetUpdate(ts._tweetRepo.Find(t => t.Id == tweetId && t.Event.Name == streamName).SingleOrDefault(), streamName);
            }
        }