instant-image-uploader /Twitpic/TwitpicUploader.cs

Language C# Lines 121
MD5 Hash 6f876d4c192283a107b80e11b1c65641 Estimated Cost $2,484 (why?)
Repository https://bitbucket.org/ugaya40/instant-image-uploader View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using InstantImageUploader.Models;
using ImageUploader.Extensions.Twitpic.Properties;
using Codeplex.OAuth;
using System.Reactive.Linq;
using ImageUploader.Extensions.Twitpic;
using System.Xml.Linq;
using System.Diagnostics;
using System.IO;

namespace ImageUploader.Extensions.Twitpic
{
    [Export(typeof(IImageUploader))]
    public class TwitpicUploader : IImageUploader
    {
        internal const string CONSUMER_KEY = "0eRp9Vt6k88OTvy1UrnqGQ";
        internal const string CONSUMER_SECRET = "ZIFuRWCjkHqI1CZ04ULdJxDabx00CGJacgcpQiGlOk8";

        private AccessToken _accessToken;

        public void Initialize()
        {
            Settings.Default.Reload();

            if ((!string.IsNullOrEmpty(Settings.Default.AccessTokenKey))
                && (!string.IsNullOrEmpty(Settings.Default.AccessTokenSecret)))
            {
                AccessToken = new AccessToken(Settings.Default.AccessTokenKey, Settings.Default.AccessTokenSecret);
            }
        }

        public void Upload(string imageFilePath)
        {
            Observable.Defer(() =>
                {
                    var client = new TwitpicClient(CONSUMER_KEY, CONSUMER_SECRET, AccessToken);
                    return client.UploadPicture("UploadPicture", string.Empty, File.ReadAllBytes(imageFilePath));
                })
                .Retry(3)
                .Select(result => (string)XElement.Parse(result).Element("url"))
                .Subscribe(url =>
                    {
                        Process.Start(url);
                        OnSuccessImageUpload(null);
                    }, (Exception ex) => OnFailedImageUpload("Twitpicへのアップロードに失敗しました"));
        }

        public Type SettingWindowType
        {
            get { return typeof(SettingWindow); }
        }

        public AccessToken AccessToken
        {
            get
            {
                return _accessToken;
            }
            set
            {
                _accessToken = value;
                Settings.Default.AccessTokenKey = _accessToken.Key;
                Settings.Default.AccessTokenSecret = _accessToken.Secret;
                Settings.Default.Save();
            }
        }

        public event EventHandler<SuccessImageUploadEventArgs> SuccessImageUpload;

        private void OnSuccessImageUpload(string messageForUser)
        {
            var handler = SuccessImageUpload;
            if (handler != null)
            {
                handler(this, new SuccessImageUploadEventArgs(messageForUser));
            }
        }

        public event EventHandler<FailedImageUploadEventArgs> FailedImageUpload;

        private void OnFailedImageUpload(string messageForUser)
        {
            var handler = FailedImageUpload;
            if (handler != null)
            {
                handler(this, new FailedImageUploadEventArgs(messageForUser));
            }
        }

        public string ImageUploaderName
        {
            get { return "Twitpic"; }
        }

        public string Description
        {
            get { return "Twitpic用アップローダです。"; }
        }

        public string Author
        {
            get { return "Masanori Onoue (ugaya40)"; }
        }

        public string Url
        {
            get { return "http://ugaya40.net"; }
        }


        public bool IsNeedSetting
        {
            get { return AccessToken == null; }
        }
    }
}
Back to Top