<commit_before>﻿using Microsoft.Phone.Reactive;
using Microsoft.Xna.Framework.Media;
using System;
using System.Diagnostics;
using System.Net;
using System.Windows;

namespace WPPMM.CameraManager
{
    class Downloader
    {

        public Downloader()
        {
        }

        internal void DownloadImageFile(Uri uri, Action<Picture> OnCompleted, Action<ImageDLError> OnError)
        {
            WebRequest request;
            try { request = HttpWebRequest.Create(uri); }
            catch (Exception e)
            {
                Debug.WriteLine("Exception: HttpWebRequest.create(uri): " + e.Message);
                Deployment.Current.Dispatcher.BeginInvoke(() => OnError.Invoke(ImageDLError.Argument));
                return;
            }

            Observable.FromAsyncPattern<WebResponse>(request.BeginGetResponse, request.EndGetResponse)()
            .Select(res =>
            {
                try
                {
                    if (res == null)
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() => OnError.Invoke(ImageDLError.Network));
                        return null;
                    }
                    var strm = res.GetResponseStream();
                    if (strm == null)
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() => OnError.Invoke(ImageDLError.Network));
                    }
                    return strm;
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Caught exception at getting stream: " + e.Message);
                    Deployment.Current.Dispatcher.BeginInvoke(() => OnError.Invoke(ImageDLError.Network));
                    return null;
                }
            })
            .Select(strm =>
            {
                if (strm == null) { return null; }
                try
                {
                    var pic = new MediaLibrary().SavePictureToCameraRoll(string.Format("CameraRemote{0:yyyyMMdd_HHmmss}.jpg", DateTime.Now), strm);
                    if (pic == null)
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() => OnError.Invoke(ImageDLError.Saving));
                    }
                    return pic;
                }
                catch (Exception e)
                {
                    // Some devices throws exception while saving picture to camera roll.
                    // e.g.) HTC 8S
                    Debug.WriteLine("Caught exception at saving picture: " + e.Message);
                    Deployment.Current.Dispatcher.BeginInvoke(() => OnError.Invoke(ImageDLError.Saving));
                    return null;
                }
            })
            .ObserveOnDispatcher()
            .Subscribe(pic =>
            {
                if (pic != null)
                {
                    OnCompleted(pic);
                }
            }
            , e =>
            {
                OnError.Invoke(ImageDLError.Unknown);
            });
        }
    }

    public enum ImageDLError
    {
        Network,
        Saving,
        Argument,
        Unknown
    }
}<commit_msg>Use Where clause to filter null value, Add "DeviceInternal" error code.<commit_after>﻿using Microsoft.Phone.Reactive;
using Microsoft.Xna.Framework.Media;
using System;
using System.Diagnostics;
using System.Net;
using System.Windows;

namespace WPPMM.CameraManager
{
    class Downloader
    {

        public Downloader()
        {
        }

        internal void DownloadImageFile(Uri uri, Action<Picture> OnCompleted, Action<ImageDLError> OnError)
        {
            WebRequest request;
            try { request = HttpWebRequest.Create(uri); }
            catch (Exception e)
            {
                Debug.WriteLine("Exception: HttpWebRequest.create(uri): " + e.Message);
                Deployment.Current.Dispatcher.BeginInvoke(() => OnError.Invoke(ImageDLError.Argument));
                return;
            }

            Observable.FromAsyncPattern<WebResponse>(request.BeginGetResponse, request.EndGetResponse)()
            .Select(res =>
            {
                if (res == null)
                {
                    Deployment.Current.Dispatcher.BeginInvoke(() => OnError.Invoke(ImageDLError.Network));
                    return null;
                }
                try
                {
                    return res.GetResponseStream();
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Caught exception at getting stream: " + e.Message);
                    return null;
                }
            })
            .Select(strm =>
            {
                if (strm == null)
                {
                    Deployment.Current.Dispatcher.BeginInvoke(() => OnError.Invoke(ImageDLError.Network));
                }
                return strm;
            })
            .Where(strm => strm != null)
            .Select(strm =>
            {
                try
                {
                    var pic = new MediaLibrary().SavePictureToCameraRoll(//
                        string.Format("CameraRemote{0:yyyyMMdd_HHmmss}.jpg", DateTime.Now), strm);
                    if (pic == null)
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() => OnError.Invoke(ImageDLError.Saving));
                    }
                    return pic;
                }
                catch (Exception e)
                {
                    // Some devices throws exception while saving picture to camera roll.
                    // e.g.) HTC 8S
                    Debug.WriteLine("Caught exception at saving picture: " + e.Message);
                    Deployment.Current.Dispatcher.BeginInvoke(() => OnError.Invoke(ImageDLError.DeviceInternal));
                    return null;
                }
            })
            .Where(pic => pic != null)
            .ObserveOnDispatcher()
            .Subscribe(pic => { OnCompleted(pic); }, e => { OnError.Invoke(ImageDLError.Unknown); });
        }
    }

    public enum ImageDLError
    {
        Network,
        Saving,
        Argument,
        DeviceInternal,
        Unknown
    }
}
{'fraction_non_alphanumeric': 0.09508615188257817, 'fraction_numerical': 0.0006381620931716656, 'mean_word_length': 1.235734664764622, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '921192', 'n_tokens_mistral': 1572, 'n_tokens_neox': 1436, 'n_words': 278}