Ejemplo n.º 1
0
        internal static IAsyncOperationWithProgress <FileSearchResult, HttpProgress> SearchAsync(string keyword, Category category, StorageFile file, bool searchSimilar, bool onlyCovers, bool searchExpunged)
        {
            if (file is null)
            {
                throw new ArgumentNullException(nameof(file));
            }

            if (searchSimilar)
            {
                return(AsyncInfo.Run <FileSearchResult, HttpProgress>(async(token, progress) =>
                {
                    var read = FileIO.ReadBufferAsync(file);
                    HttpStringContent contentOf(bool v) => new HttpStringContent(v ? "1" : "0");
                    var data = new HttpMultipartFormDataContent
                    {
                        { contentOf(true), "fs_similar" },
                        { contentOf(onlyCovers), "fs_covers" },
                        { contentOf(searchExpunged), "fs_exp" }
                    };
                    var buf = await read;
                    data.Add(new HttpBufferContent(buf), "sfile", file.Name);
                    await data.BufferAllAsync();
                    var post = Client.Current.HttpClient.PostAsync(Client.Current.Uris.FileSearchUri, data);
                    post.Progress = (s, p) => progress.Report(p);
                    var r = await post;
                    var uri = r.RequestMessage.RequestUri;
                    var query = uri.Query.Split("?&".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    var hashstr = query.Single(s => s.StartsWith("f_shash=")).Substring(8).Split(';');
                    var info = hashstr.FirstOrDefault(value => value.Length != 40);
                    switch (info)
                    {
                    case "monotone":
                        throw new InvalidOperationException(LocalizedStrings.Resources.UnsupportedMonotone);

                    case "corrupt":
                        throw new InvalidOperationException(LocalizedStrings.Resources.UnsupportedFile);

                    case null:
                        break;

                    default:
                        throw new InvalidOperationException(info);
                    }
                    return new FileSearchResult(keyword, category, hashstr.Select(SHA1Value.Parse), file.Name, true, onlyCovers, searchExpunged);
                }));
            }
            else
            {
                return(AsyncInfo.Run <FileSearchResult, HttpProgress>(async(token, progress) =>
                {
                    var hash = await SHA1Value.ComputeAsync(file);
                    return new FileSearchResult(keyword, category, Enumerable.Repeat(hash, 1), file.Name, false, onlyCovers, searchExpunged);
                }));
            }
        }