Example #1
0
        public static void Create(SearchWindow form, string filePath)
        {
            var kisboFile = new KisboFile(form, filePath);

            form.ctlList.Items.Add(kisboFile.ListViewItem);
            form.m_list.Add(kisboFile);
        }
Example #2
0
        public void AddFile(IEnumerable <string> data)
        {
            try
            {
                if (this.InvokeRequired)
                {
                    this.Invoke(new Action <IEnumerable <string> >(this.AddFile), data);
                }
                else
                {
                    this.ctlList.BeginUpdate();

                    lock (this.m_lock)
                    {
                        Debug.WriteLine("AddFile Lock");

                        var lst = new List <string>();

                        foreach (var path in data)
                        {
                            if (KisboMain.Check(path) && !this.m_list.Exists(e => e.OriginalFilePath.Equals(path, StringComparison.OrdinalIgnoreCase)))
                            {
                                lst.Add(path);
                            }
                        }

                        for (int i = 0; i < lst.Count; ++i)
                        {
                            KisboFile.Create(this, lst[i]);
                        }

                        if (lst.Count > 0)
                        {
                            this.m_newItemHandler.Set();
                        }

                        Interlocked.Add(ref this.m_taskbarMax, lst.Count);

                        Debug.WriteLine("AddFile Unlock");
                    }

                    this.ctlList.EndUpdate();

                    this.SetProgress();
                }
            }
            catch
            {
            }
        }
Example #3
0
        private States SearchImage(WebClientx wc, KisboFile file, StreamWriter writer, CancellationToken token)
        {
            string body;

            Size oldSize;

            try
            {
                using (var img = Image.FromFile(file.OriginalFilePath))
                    file.SetBeforeResolution(oldSize = img.Size);
            }
            catch
            {
                return(States.Error);
            }

            try
            {
                {
                    var origBoundary = "-----" + Guid.NewGuid().ToString("N");
                    var boundary     = "--" + origBoundary;

                    var info = new FileInfo(file.OriginalFilePath);

                    // DELETE BOM
                    writer.BaseStream.SetLength(0);
                    writer.WriteLine(boundary);
                    writer.WriteLine("Content-Disposition: form-data; name=\"encoded_image\"; filename=\"{0}\"", info.Name);
                    writer.WriteLine("Content-Type: application/octet-stream");
                    writer.WriteLine();
                    using (var fileStream = info.OpenRead())
                        fileStream.CopyTo(writer.BaseStream, 4096);
                    writer.WriteLine();
                    writer.WriteLine(boundary + "--");

                    writer.BaseStream.Position = 0;

                    do
                    {
                        if (token.IsCancellationRequested)
                        {
                            return(States.Error);
                        }

                        if (!this.m_search.WaitOne(0))
                        {
                            file.State = States.WaitSearch;
                            file.SetStatus();

                            this.m_search.WaitOne();
                            if (token.IsCancellationRequested)
                            {
                                return(States.Error);
                            }

                            file.State = States.Working;
                            file.SetStatus();
                        }

                        body = wc.UploadData(new Uri(this.m_googleUri, "/searchbyimage/upload"), writer.BaseStream, "multipart/form-data; boundary=" + origBoundary);
                        if (body == null)
                        {
                            return(States.Error);
                        }

                        if (body.IndexOf("topstuff", StringComparison.OrdinalIgnoreCase) == -1)
                        {
                            body = null;

                            if (this.m_search.WaitOne(0))
                            {
                                file.State = States.WaitSearch;
                                file.SetStatus();

                                this.m_search.Reset();
                                Thread.Sleep(60 * 1000);
                                this.m_search.Set();

                                if (token.IsCancellationRequested)
                                {
                                    return(States.Error);
                                }

                                file.State = States.Working;
                                file.SetStatus();
                            }
                        }
                    } while (body == null);
                }

                file.GoogleUrl = wc.ResponseUri.AbsoluteUri;

                var html = new HtmlDocument();
                html.LoadHtml(body);
                var docNode = html.DocumentNode;

                bool succ = false;
                foreach (var card_section_node in docNode.SelectNodes(@"//div[contains(@class,'card-section')]"))
                {
                    foreach (var a_node in card_section_node.SelectNodes(".//a"))
                    {
                        var href = a_node.Attributes["href"];
                        if (href == null)
                        {
                            continue;
                        }

                        if (href.Value.IndexOf("tbs=") == -1)
                        {
                            continue;
                        }

                        if ((body = wc.DownloadString(new Uri(this.m_googleUri, href.Value.Replace("&amp;", "&")))) == null)
                        {
                            return(States.Error);
                        }

                        html.LoadHtml(body);
                        docNode = html.DocumentNode;

                        succ = true;
                        break;
                    }
                }

                if (!succ)
                {
                    return(States.NoResult);
                }

                string newExtension = null, newPath, tempPath = null, dir;
                Guid   guid;
                RGMeta rgMeta;

                foreach (var rg_meta_node in docNode.SelectNodes(@"//div[contains(@class,'rg_meta')]"))
                {
                    if (file.Removed)
                    {
                        break;
                    }

                    if (token.IsCancellationRequested)
                    {
                        return(States.Error);
                    }

                    try
                    {
                        rgMeta = RGMeta.Parse(rg_meta_node.InnerHtml);
                        if (rgMeta.Width <= oldSize.Width ||
                            rgMeta.Height <= oldSize.Height)
                        {
                            continue;
                        }

                        tempPath = Path.GetTempFileName();

                        using (var fileStream = File.OpenWrite(tempPath))
                        {
                            fileStream.SetLength(0);
                            if (!wc.DownloadData(rgMeta.ImageUrl, fileStream, rgMeta.ImageUrl.AbsoluteUri))
                            {
                                continue;
                            }
                        }

                        if (file.Removed)
                        {
                            break;
                        }
                        using (var img = Image.FromFile(tempPath))
                        {
                            guid = img.RawFormat.Guid;

                            if (guid == ImageFormat.Bmp.Guid)
                            {
                                newExtension = ".bmp";
                            }
                            else if (guid == ImageFormat.Gif.Guid)
                            {
                                newExtension = ".gif";
                            }
                            else if (guid == ImageFormat.Jpeg.Guid)
                            {
                                newExtension = ".jpg";
                            }
                            else if (guid == ImageFormat.Png.Guid)
                            {
                                newExtension = ".png";
                            }

                            if (img.Width <= oldSize.Width ||
                                img.Height <= oldSize.Height)
                            {
                                continue;
                            }

                            file.SetAfterResolution(img.Size);
                        }

                        if (file.Removed)
                        {
                            break;
                        }

                        dir = Path.Combine(Path.GetDirectoryName(file.OriginalFilePath), "kisbo-original");
                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }

                        File.Move(file.OriginalFilePath, GetSafeFileName(Path.Combine(dir, Path.GetFileName(file.OriginalFilePath))));

                        newPath = Path.ChangeExtension(file.OriginalFilePath, newExtension);
                        File.Move(tempPath, GetSafeFileName(newPath));

                        file.NewFilePath = newPath;

                        return(States.Complete);
                    }
                    catch
                    {
                    }
                    finally
                    {
                        try
                        {
                            File.Delete(tempPath);
                        }
                        catch
                        {
                        }
                    }
                }

                return(States.Pass);
            }
            catch
            {
                return(States.Error);
            }
        }