ShareX/ShareX.UploadersLib/ImageUploaders/GooglePhotos.cs

188 lines
6.2 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2018-01-02 03:59:14 +13:00
Copyright (c) 2007-2018 ShareX Team
2013-11-03 23:53:49 +13:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
2014-12-11 12:19:28 +13:00
using ShareX.HelpersLib;
2016-06-28 05:20:55 +12:00
using ShareX.UploadersLib.Properties;
2013-11-03 23:53:49 +13:00
using System.Collections.Generic;
using System.Collections.Specialized;
2016-06-28 05:20:55 +12:00
using System.Drawing;
2013-11-03 23:53:49 +13:00
using System.IO;
using System.Windows.Forms;
2013-11-03 23:53:49 +13:00
using System.Xml.Linq;
2014-12-11 09:25:20 +13:00
namespace ShareX.UploadersLib.ImageUploaders
2013-11-03 23:53:49 +13:00
{
public class GooglePhotosImageUploaderService : ImageUploaderService
{
public override ImageDestination EnumValue { get; } = ImageDestination.Picasa;
public override Icon ServiceIcon => Resources.GooglePhotos;
2016-06-28 05:20:55 +12:00
public override bool CheckConfig(UploadersConfig config)
{
return OAuth2Info.CheckOAuth(config.PicasaOAuth2Info);
}
public override GenericUploader CreateUploader(UploadersConfig config, TaskReferenceHelper taskInfo)
{
return new GooglePhotos(config.PicasaOAuth2Info)
{
AlbumID = config.PicasaAlbumID
};
}
public override TabPage GetUploadersConfigTabPage(UploadersConfigForm form) => form.tpGooglePhotos;
}
public class GooglePhotos : ImageUploader, IOAuth2
2013-11-03 23:53:49 +13:00
{
private GoogleOAuth2 GoogleAuth { get; set; }
2013-11-03 23:53:49 +13:00
public string AlbumID { get; set; }
private static readonly XNamespace AtomNS = "http://www.w3.org/2005/Atom";
private static readonly XNamespace MediaNS = "http://search.yahoo.com/mrss/";
private static readonly XNamespace GPhotoNS = "http://schemas.google.com/photos/2007";
public GooglePhotos(OAuth2Info oauth)
2013-11-03 23:53:49 +13:00
{
GoogleAuth = new GoogleOAuth2(oauth, this)
{
Scope = "https://picasaweb.google.com/data"
};
2013-11-03 23:53:49 +13:00
}
public OAuth2Info AuthInfo => GoogleAuth.AuthInfo;
public bool RefreshAccessToken()
2013-11-03 23:53:49 +13:00
{
return GoogleAuth.RefreshAccessToken();
2013-11-03 23:53:49 +13:00
}
public bool CheckAuthorization()
2013-11-03 23:53:49 +13:00
{
return GoogleAuth.CheckAuthorization();
2013-11-03 23:53:49 +13:00
}
public string GetAuthorizationURL()
2013-11-03 23:53:49 +13:00
{
return GoogleAuth.GetAuthorizationURL();
}
2013-11-03 23:53:49 +13:00
public bool GetAccessToken(string code)
{
return GoogleAuth.GetAccessToken(code);
2013-11-03 23:53:49 +13:00
}
private NameValueCollection GetAuthHeaders()
{
NameValueCollection headers = GoogleAuth.GetAuthHeaders();
2017-04-06 19:53:37 +12:00
headers.Add("GData-Version", "3");
return headers;
}
public List<GooglePhotosAlbumInfo> GetAlbumList()
2013-11-03 23:53:49 +13:00
{
if (!CheckAuthorization()) return null;
List<GooglePhotosAlbumInfo> albumList = new List<GooglePhotosAlbumInfo>();
2013-11-03 23:53:49 +13:00
string response = SendRequest(HttpMethod.GET, "https://picasaweb.google.com/data/feed/api/user/default", headers: GetAuthHeaders());
2013-11-03 23:53:49 +13:00
if (!string.IsNullOrEmpty(response))
{
XDocument xd = XDocument.Parse(response);
if (xd != null)
{
foreach (XElement entry in xd.Descendants(AtomNS + "entry"))
{
GooglePhotosAlbumInfo album = new GooglePhotosAlbumInfo();
2013-11-03 23:53:49 +13:00
album.ID = entry.GetElementValue(GPhotoNS + "id");
album.Name = entry.GetElementValue(AtomNS + "title");
2013-11-03 23:53:49 +13:00
album.Summary = entry.GetElementValue(AtomNS + "summary");
albumList.Add(album);
}
}
}
return albumList;
}
public override UploadResult Upload(Stream stream, string fileName)
{
if (!CheckAuthorization()) return null;
if (string.IsNullOrEmpty(AlbumID))
{
AlbumID = "default";
}
UploadResult ur = new UploadResult();
string url = string.Format("https://picasaweb.google.com/data/feed/api/user/default/albumid/" + AlbumID);
2018-10-18 05:06:06 +13:00
string contentType = UploadHelpers.GetMimeType(fileName);
2013-11-03 23:53:49 +13:00
NameValueCollection headers = GetAuthHeaders();
headers.Add("Slug", URLHelpers.URLEncode(fileName));
2013-11-03 23:53:49 +13:00
2016-12-23 20:24:38 +13:00
ur.Response = SendRequest(HttpMethod.POST, url, stream, contentType, null, headers);
2013-11-03 23:53:49 +13:00
if (ur.Response != null)
2013-11-03 23:53:49 +13:00
{
XDocument xd = XDocument.Parse(ur.Response);
2013-11-03 23:53:49 +13:00
XElement entry_element = xd.Element(AtomNS + "entry");
if (entry_element != null)
2013-11-03 23:53:49 +13:00
{
XElement group_element = entry_element.Element(MediaNS + "group");
2013-11-03 23:53:49 +13:00
if (group_element != null)
2013-11-03 23:53:49 +13:00
{
XElement content_element = group_element.Element(MediaNS + "content");
if (content_element != null)
{
ur.ThumbnailURL = content_element.GetAttributeValue("url");
2013-11-03 23:53:49 +13:00
int last_slash_index = ur.ThumbnailURL.LastIndexOf(@"/");
2013-11-03 23:53:49 +13:00
ur.URL = ur.ThumbnailURL.Insert(last_slash_index, @"/s0");
}
2013-11-03 23:53:49 +13:00
}
}
}
return ur;
}
}
public class GooglePhotosAlbumInfo
2013-11-03 23:53:49 +13:00
{
public string ID { get; set; }
public string Name { get; set; }
public string Summary { get; set; }
}
}