/// <summary>
        /// Write a thumbnail from Hydrus into the thumbs folder.
        /// </summary>
        /// <param name="hydrusMetadata"></param>
        /// <returns></returns>
        private string DownloadThumbnail(HydrusMetadata hydrusMetadata, string thumbsDir)
        {
            HttpWebRequest request = this.CreateRequest("/get_files/thumbnail?file_id=" + hydrusMetadata.FileId);

            request.AutomaticDecompression = DecompressionMethods.GZip;

            Directory.CreateDirectory(thumbsDir);
            string filePath = thumbsDir + "hydrus-file-" + hydrusMetadata.FileId + "." + hydrusMetadata.Extension;

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) {
                using (var fileStream = File.Create(filePath)) {
                    response.GetResponseStream().CopyTo(fileStream);
                }
            }

            return(filePath);
        }
        public void SetHydrusMetadata(HydrusMetadata hydrusMetadata)
        {
            this.HydrusFileId = hydrusMetadata.FileId;

            this.local.Width  = hydrusMetadata.Width;
            this.local.Height = hydrusMetadata.Height;
            this.local.Size   = hydrusMetadata.Size;
            this.local.Hash   = hydrusMetadata.Hash;

            switch (hydrusMetadata.Mime)
            {
            case "image/jpg": this.local.Format = "jpg"; break;

            case "image/jpeg": this.local.Format = "jpg"; break;

            case "image/png": this.local.Format = "png"; break;

            case "image/bmp": this.local.Format = "bmp"; break;

            case "image/webp": this.local.Format = "webp"; break;

            case "image/tiff": this.local.Format = "tiff"; break;
            }
        }
Exemple #3
0
        private async void Button_ExecuteQuery_Click(object sender, RoutedEventArgs e)
        {
            this.EnableOrDisableExecuteQueryButton(false);

            uint limit = this.Limit;

            // Add limit tag
            if (limit > 0)
            {
                Tag tag = new Tag("limit = " + limit, "system");

                if (!this.ListBox_Tags.Items.Contains(tag))
                {
                    this.ListBox_Tags.Items.Add(tag);
                }
            }

            string tagServiceKey  = null;
            string fileServiceKey = null;

            if (App.hydrusApi.Unreachable)
            {
                this.CancelQuery();

                return;
            }

            bool doesSearchFilesSupportsServiceArguments = await App.hydrusApi.DoesSearchFilesSupportsServiceArguments();

            if (doesSearchFilesSupportsServiceArguments)
            {
                ComboBoxItem selectedTagService  = (ComboBoxItem)this.ComboBox_TagService.SelectedItem;
                ComboBoxItem selectedFileService = (ComboBoxItem)this.ComboBox_FileService.SelectedItem;

                if (selectedTagService != null)
                {
                    tagServiceKey = selectedTagService.Tag.ToString();
                }

                if (selectedFileService != null)
                {
                    fileServiceKey = selectedFileService.Tag.ToString();
                }
            }

            // Get files
            JArray fileIds = await App.hydrusApi.SearchFiles(this.Tags, (bool)this.CheckBox_InboxOnly.IsChecked, (bool)this.CheckBox_ArchiveOnly.IsChecked, tagServiceKey, fileServiceKey);

            if (App.hydrusApi.Unreachable)
            {
                this.CancelQuery();

                return;
            }

            if (fileIds == null || fileIds.Count < 1)
            {
                this.NoResult();

                return;
            }

            // Create a smaller array according to the limit
            // As of Hydrus version 448 this should not be needed anymore thanks to the "system:limit" tag
            if (limit > 0 && limit < fileIds.Count)
            {
                JArray limitedFileIds = new JArray();

                for (int i = 0; i < limit; i++)
                {
                    limitedFileIds.Add(fileIds[i]);
                }

                fileIds        = limitedFileIds;
                limitedFileIds = null;
            }

            // Warn the user about a huge number of files being imported
            if ((bool)this.CheckBox_WarnBeforeImport.IsChecked && !App.AskUser("You're about to import " + fileIds.Count + " files, are you sure about that?"))
            {
                this.CancelQuery();

                return;
            }

            // Get files' metadata
            JArray jTokens = await App.hydrusApi.GetFilesMetadata(fileIds);

            if (App.hydrusApi.Unreachable)
            {
                this.CancelQuery();

                return;
            }

            if (jTokens == null || jTokens.Count < 1)
            {
                this.NoResult();

                return;
            }

            // Fill the HydrusMetadata list from the JTokens
            this.hydrusMetadataList = new List <HydrusMetadata>();
            List <string> unsupportedMimetypes = new List <string>();

            foreach (JToken jToken in jTokens)
            {
                HydrusMetadata hydrusMetadata = new HydrusMetadata(jToken);

                if (hydrusMetadata.IsImage)
                {
                    this.hydrusMetadataList.Add(hydrusMetadata);

                    continue;
                }

                if (!unsupportedMimetypes.Contains(hydrusMetadata.Mime))
                {
                    unsupportedMimetypes.Add(hydrusMetadata.Mime);
                }
            }

            // Warn about files not imported due to unsupported type
            if (unsupportedMimetypes.Count > 0)
            {
                MessageBox.Show(
                    jTokens.Count + " files were found from the query but only " + this.hydrusMetadataList.Count + " will be imported due to some file types not being supported:\n\n"
                    + String.Join(", ", unsupportedMimetypes.ToArray())
                    );
            }

            this.Close();
        }
 /// <summary>
 /// Retrieve a file's thumbnail and store it in the thumbs folder.
 /// </summary>
 /// <param name="hydrusMetadata"></param>
 /// <param name="thumbsDir"></param>
 /// <returns></returns>
 public Task <string> DownloadThumbnailAsync(HydrusMetadata hydrusMetadata, string thumbsDir)
 {
     return(Task.Run(() => this.DownloadThumbnail(hydrusMetadata, thumbsDir)));
 }