Exemple #1
0
        public override void Init()
        {
            base.Init();

            FileComponent   file   = Owner.Get <FileComponent>();
            ClientComponent client = Entity.Get <ClientComponent>();

            try
            {
                // TODO: Support folders!
                //
                string folder = "";                // client.Root;
                string path   = !string.IsNullOrEmpty(folder) ? Path.Combine(client.Download, folder) : client.Download;
                DirectoryUtil.Create(path);

                file.Path = Path.Combine(path, file.Name);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                file.Path = Path.Combine(client.Download, file.Name);
            }

            ServerComponent server = Entity.Get <ServerComponent>();

            if (server.Enabled && Inactive)
            {
                Start();
            }
        }
Exemple #2
0
 public BrowseComponent(FileComponent file) : base(file)
 {
 }
Exemple #3
0
        private void DownloadState()
        {
            try
            {
                // Command
                FileComponent          file    = Owner.Get <FileComponent>();
                DownloadRequestCommand command = new DownloadRequestCommand(Entity, Connection);
                HttpCode code = command.Execute(Id, file, ClientId);

                if (code == HttpCode.Ok)
                {
                    byte[] data = command.Data;

                    if (data != null)
                    {
                        // Data
                        JsonChunk jsonChunk = command.Chunk;
                        Debug.Assert(data.Length == jsonChunk.Size);

                        while (true)
                        {
                            try
                            {
                                FileUtil.Write(file.Path, data, jsonChunk.Offset);
                                break;
                            }
                            catch (IOException ex)
                            {
                                Console.WriteLine(ex.Message);
                                Status = DemonStatus.Warning;
                                Thread.Sleep(DemonTimeout.File);
                            }
                        }

                        TransferComponent transfer = Owner.Get <TransferComponent>();
                        transfer.Size += jsonChunk.Size;

                        if (transfer.Done)
                        {
                            State  = DemonState.Shutdown;
                            Status = DemonStatus.Success;
                        }
                    }
                    else
                    {
                        if (Id == null)
                        {
                            FileUtil.Create(file.Path, file.Size);

                            if (code == HttpCode.Ok)
                            {
                                // Data
                                Id = command.Id;
                                if (!Paused && !Cancelled)
                                {
                                    Status = DemonStatus.Success;
                                }
                            }
                        }
                        else
                        {
                            // TODO: Implement infinite time-out?
                            //
                            Sleep(DemonTimeout.Seconds);
                        }
                    }
                }
                else
                {
                    State = DemonState.Shutdown;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                State  = DemonState.Shutdown;
                Status = DemonStatus.Error;
            }
        }