Esempio n. 1
0
        public override void Get(HttpResponse response, string relativePath, UrlEncodedMessage message) {
            if (string.IsNullOrEmpty(message["action"])) {
                response.StatusCode = 200;
                response.Write("No Action Specified."); 
            }

            switch (message["action"]) {
                case "add":
                    if (!string.IsNullOrEmpty(message["location"])) {
                        try {
                            var uri = new Uri(message["location"]);
                            if (Peek(uri)) {
                                var filename = "UploadedFile.bin".GenerateTemporaryFilename();
                                var rf = new RemoteFile(uri, filename);
                                rf.Get();
                                if (File.Exists(filename)) {
                                    HandleFile(filename).ContinueWith(antecedent => {
                                        if (antecedent.IsFaulted) {
                                            var e = antecedent.Exception.InnerException;
                                            HandleException(e);
                                            response.StatusCode = 500;
                                            response.Close();
                                        } else {
                                            response.StatusCode = antecedent.Result;
                                            response.Close();
                                        }
                                    }).Wait();
                                    return;
                                }
                            }
                        } catch {
                        }
                    }
                    break;

                case "validate":
                    Validate().ContinueWith(antecedent => {
                        if (antecedent.IsFaulted) {
                            var e = antecedent.Exception.InnerException;
                            HandleException(e);
                            response.StatusCode = 500;
                            response.Close();
                        } else {
                            response.StatusCode = antecedent.Result;
                            response.Close();
                        }
                    }).Wait();
                    return;
                    break;
                case "makewebpi":
                    var txt = RegenerateWebPI();
                    response.ContentType = "application/xml";
                    response.StatusCode = 200;
                    response.Write(txt);
                    // response.Close();
                    return;
                case "test":
                    var txt2 = "Hello World";
                    // response.ContentType = "application/text";
                    response.StatusCode = 200;
                    response.Write(txt2);
                    // response.Close();
                    return;
            }

            response.StatusCode = 500;
            response.Close();
        }
Esempio n. 2
0
        public Task <IEnumerable <Package> > QueryPackages(string query, PkgFilter pkgFilter, CollectionFilter collectionFilter, string location)
        {
            string localPath = null;

            // there are three possibilities:
            // 1. The parameter is a file on disk, or a remote file.
            try {
                // some kind of local file?
                if (File.Exists(query))
                {
                    return(PackagesFromLocalFile(query.EnsureFileIsLocal(), Path.GetDirectoryName(query.GetFullPath()), pkgFilter, collectionFilter));
                }

                var uri = new Uri(query);
                if (uri.IsWebUri())
                {
                    Task.Factory.StartNew(() => {
                        var lp         = uri.AbsolutePath.MakeSafeFileName().GenerateTemporaryFilename();
                        var remoteFile = new RemoteFile(uri, lp, itemUri => {
                            Event <DownloadCompleted> .Raise(uri.AbsolutePath, lp); // got the file!
                            localPath = lp;
                        },
                                                        itemUri => {
                            localPath = null;     // failed
                        },
                                                        (itemUri, percent) => Event <DownloadProgress> .Raise(uri.AbsolutePath, localPath, percent));
                        remoteFile.Get();

                        if (localPath != null)
                        {
                            return(PackagesFromLocalFile(localPath, null, pkgFilter, collectionFilter).Result);
                        }
                        return(Enumerable.Empty <Package>());
                    });
                }
            }
            catch {
                // ignore what can't be fixed
            }

            // 2. A directory path or filemask (ie, creating a one-time 'feed' )
            if (Directory.Exists(query) || query.IndexOf('\\') > -1 || query.IndexOf('/') > -1 || (query.IndexOf('*') > -1 && query.ToLower().EndsWith(".msi")))
            {
                // specified a folder, or some kind of path that looks like a feed.
                // add it as a feed, and then get the contents of that feed.
                return((Remote.AddFeed(query, true) as Task <PackageManagerResponseImpl>).Continue(response => {
                    // a feed!
                    if (response.Feeds.Any())
                    {
                        return (Remote.FindPackages(null, pkgFilter, collectionFilter, response.Feeds.First().Location) as Task <PackageManagerResponseImpl>).Continue(response1 => response1.Packages).Result;
                    }

                    query = query.GetFullPath();
                    return (Remote.AddFeed(query, true) as Task <PackageManagerResponseImpl>).Continue(response2 => {
                        if (response.Feeds.Any())
                        {
                            return (Remote.FindPackages(null, pkgFilter, collectionFilter, response2.Feeds.First().Location) as Task <PackageManagerResponseImpl>).Continue(response1 => response1.Packages).Result;
                        }
                        return Enumerable.Empty <Package>();
                    }).Result;
                }));
            }

            // 3. A partial/canonical name of a package.
            return((Remote.FindPackages(query, pkgFilter, collectionFilter, location) as Task <PackageManagerResponseImpl>).Continue(response => response.Packages));
        }
Esempio n. 3
0
        public void RequireRemoteFile(string requestReference, IEnumerable <Uri> remoteLocations, string destination, bool force)
        {
            var filename = requestReference.MakeSafeFileName();

            var targetFilename = Path.Combine(destination, filename);

            if (force)
            {
                targetFilename.TryHardToDelete();
            }

            lock (CurrentDownloads) {
                if (CurrentDownloads.ContainsKey(requestReference))
                {
                    // wait for this guy to respond (which should give us what we need)
                    CurrentDownloads[requestReference].Continue(() => {
                        if (File.Exists(targetFilename))
                        {
                            try {
                                Event <DownloadCompleted> .Raise(requestReference, targetFilename);
                            }
                            catch (Exception e) {
                                Logger.Error("CRITICAL: DownloadCompleted event delegate thru an exception of type '{0}' -- {1}".format(e.GetType(), e.StackTrace));
                            }
                            Remote.RecognizeFile(requestReference, targetFilename, (remoteLocations.FirstOrDefault() ?? new Uri("http://nowhere")).AbsoluteUri);
                        }
                    });
                    return;
                }

                // gotta download the file...
                var task = Task.Factory.StartNew(() => {
                    foreach (var location in remoteLocations)
                    {
                        try {
                            // a filesystem location (remote or otherwise)
                            var uri = location;
                            if (uri.IsFile)
                            {
                                // try to copy the file local.
                                var remoteFile = uri.AbsoluteUri.CanonicalizePath();

                                // if this fails, we'll just move down the line.
                                File.Copy(remoteFile, targetFilename);
                                Remote.RecognizeFile(requestReference, targetFilename, uri.AbsoluteUri);
                                return;
                            }

                            // A web location
                            Task progressTask = null;
                            var success       = false;
                            var rf            = new RemoteFile(uri, targetFilename,
                                                               itemUri => {
                                Remote.RecognizeFile(requestReference, targetFilename, uri.AbsoluteUri);
                                try {
                                    Event <DownloadCompleted> .Raise(requestReference, targetFilename);
                                }
                                catch (Exception e) {
                                    Logger.Error("CRITICAL: DownloadCompleted event delegate thru an exception of type '{0}' -- {1}".format(e.GetType(), e.StackTrace));
                                }
                                // remove it from the list of current downloads
                                CurrentDownloads.Remove(requestReference);
                                success = true;
                            },
                                                               itemUri => {
                                success = false;
                            },
                                                               (itemUri, percent) => {
                                if (progressTask == null)
                                {
                                    // report progress to the engine
                                    progressTask = Remote.DownloadProgress(requestReference, percent);
                                    progressTask.Continue(() => {
                                        progressTask = null;
                                    });
                                }
                                try {
                                    Event <DownloadProgress> .Raise(requestReference, targetFilename, percent);
                                }
                                catch (Exception e) {
                                    Logger.Error("CRITICAL: DownloadCompleted event delegate thru an exception of type '{0}' -- {1}".format(e.GetType(), e.StackTrace));
                                }
                            });

                            rf.Get();

                            if (success && File.Exists(targetFilename))
                            {
                                return;
                            }
                        }
                        catch (Exception e) {
                            // bogus, dude.
                            // try the next one.
                            Logger.Error(e);
                        }
                        // loop around and try again?
                    }

                    // was there a file there from before?
                    if (File.Exists(targetFilename))
                    {
                        try {
                            Event <DownloadCompleted> .Raise(requestReference, targetFilename);
                        }
                        catch (Exception e) {
                            Logger.Error("CRITICAL: DownloadCompleted event delegate thru an exception of type '{0}' -- {1}".format(e.GetType(), e.StackTrace));
                        }
                        Remote.RecognizeFile(requestReference, targetFilename, (remoteLocations.FirstOrDefault() ?? new Uri("http://nowhere")).AbsoluteUri);
                    }

                    // remove it from the list of current downloads
                    CurrentDownloads.Remove(requestReference);

                    // if we got here, that means we couldn't get the file. too bad, so sad.
                    Remote.UnableToAcquire(requestReference);
                }, TaskCreationOptions.AttachedToParent);

                CurrentDownloads.Add(targetFilename, task);
            }
        }
        public void RequireRemoteFile(string requestReference, IEnumerable<Uri> remoteLocations, string destination, bool force)
        {
            var filename = requestReference.MakeSafeFileName();

            var targetFilename = Path.Combine(destination, filename);
            lock (CurrentDownloads) {
                if (CurrentDownloads.ContainsKey(requestReference)) {
                    // wait for this guy to respond (which should give us what we need)
                    CurrentDownloads[requestReference].Continue(() => {
                        if (File.Exists(targetFilename)) {
                            try {
                                Event<DownloadCompleted>.Raise(requestReference, targetFilename);
                            }
                            catch (Exception e) {
                                Logger.Error("CRITICAL: DownloadCompleted event delegate thru an exception of type '{0}' -- {1}".format(e.GetType(), e.StackTrace));
                            }
                            Remote.RecognizeFile(requestReference, targetFilename, (remoteLocations.FirstOrDefault() ?? new Uri("http://nowhere")).AbsoluteUri);
                        }
                    });
                    return;
                }

                // gotta download the file...
                var task = Task.Factory.StartNew(() => {
                    foreach (var location in remoteLocations) {
                        try {
                            // a filesystem location (remote or otherwise)
                            var uri = location;
                            if (uri.IsFile) {
                                // try to copy the file local.
                                var remoteFile = uri.AbsoluteUri.CanonicalizePath();

                                // if this fails, we'll just move down the line.
                                File.Copy(remoteFile, targetFilename);
                                Remote.RecognizeFile(requestReference, targetFilename, uri.AbsoluteUri);
                                return;
                            }

                            // A web location
                            Task progressTask = null;
                            var success = false;
                            var rf = new RemoteFile(uri, targetFilename,
                                itemUri => {
                                    Remote.RecognizeFile(requestReference, targetFilename, uri.AbsoluteUri);
                                    try {
                                        Event<DownloadCompleted>.Raise(requestReference, targetFilename);
                                    }
                                    catch (Exception e) {
                                        Logger.Error("CRITICAL: DownloadCompleted event delegate thru an exception of type '{0}' -- {1}".format(e.GetType(), e.StackTrace));
                                    }
                                    // remove it from the list of current downloads
                                    CurrentDownloads.Remove(requestReference);
                                    success = true;
                                },
                                itemUri => {
                                    success = false;
                                },
                                (itemUri, percent) => {
                                    if (progressTask == null) {
                                        // report progress to the engine
                                        progressTask = Remote.DownloadProgress(requestReference, percent);
                                        progressTask.Continue(() => {
                                            progressTask = null;
                                        });
                                    }
                                    try {
                                        Event<DownloadProgress>.Raise(requestReference, targetFilename, percent);
                                    }
                                    catch (Exception e) {
                                        Logger.Error("CRITICAL: DownloadCompleted event delegate thru an exception of type '{0}' -- {1}".format(e.GetType(), e.StackTrace));
                                    }
                                });

                            rf.Get();

                            if (success && File.Exists(targetFilename)) {
                                return;
                            }
                        }
                        catch (Exception e) {
                            // bogus, dude.
                            // try the next one.
                            Logger.Error(e);
                        }
                        // loop around and try again?
                    }

                    // was there a file there from before?
                    if (File.Exists(targetFilename)) {
                        try {
                            Event<DownloadCompleted>.Raise(requestReference, targetFilename);
                        }
                        catch (Exception e) {
                            Logger.Error("CRITICAL: DownloadCompleted event delegate thru an exception of type '{0}' -- {1}".format(e.GetType(), e.StackTrace));
                        }
                        Remote.RecognizeFile(requestReference, targetFilename, (remoteLocations.FirstOrDefault() ?? new Uri("http://nowhere")).AbsoluteUri);
                    }

                    // remove it from the list of current downloads
                    CurrentDownloads.Remove(requestReference);

                    // if we got here, that means we couldn't get the file. too bad, so sad.
                    Remote.UnableToAcquire(requestReference);
                }, TaskCreationOptions.AttachedToParent);

                CurrentDownloads.Add(targetFilename, task);
            }
        }