示例#1
0
        /// <summary>
        /// You can download .plist, .pdf, and different assets
        /// </summary>
        /// <param name="path">The path to the magazine directory</param>
        /// <param name="name">Name of collection</param>
        public MagazineManager(string path, string name)
        {
            this._path = path;
            this._name = name;
            StatusText = "";

            _pList = new LibrelioUrl(0, path, name + ".plist");
        }
示例#2
0
        public static LibrelioUrl ConvertFromLocalUrl(LibrelioLocalUrl url)
        {
            var mag = new LibrelioUrl(url.Index, url.Url.Replace(url.RelativePath, ""), url.RelativePath);

            mag.Title    = url.Title;
            mag.Subtitle = url.Subtitle;

            return(mag);
        }
示例#3
0
        /// <summary>
        /// Use for small assets
        /// </summary>
        /// <param name="url"></param>
        /// <param name="cancelToken"></param>
        /// <returns></returns>
        public static async Task<IRandomAccessStream> DownloadFileAsync(LibrelioUrl url, CancellationToken cancelToken = default(CancellationToken))
        {
            var stream = new InMemoryRandomAccessStream();

            using(var response = await new HttpClient().GetAsync(url.AbsoluteUrl))
            {
                var buffer = await response.Content.ReadAsStringAsync();
                var dataWriter = new DataWriter(stream.GetOutputStreamAt(0));
                dataWriter.WriteString(buffer);
                await dataWriter.StoreAsync();
                await dataWriter.FlushAsync();
            }

            return stream;
        }
示例#4
0
        /// <summary>
        /// Use for small assets
        /// </summary>
        /// <param name="url"></param>
        /// <param name="cancelToken"></param>
        /// <returns></returns>
        public static async Task<IRandomAccessStream> DownloadFileAsync(LibrelioUrl url, CancellationToken cancelToken = default(CancellationToken))
        {
            var stream = new InMemoryRandomAccessStream();
            var client = new HttpClient();
            client.DefaultRequestHeaders.Add("user-agent", "LibrelioWinRT");

            using (var response = await client.GetAsync(url))
            {
                var buffer = await response.Content.ReadAsStringAsync();
                var dataWriter = new DataWriter(stream.GetOutputStreamAt(0));
                dataWriter.WriteString(buffer);
                await dataWriter.StoreAsync();
                await dataWriter.FlushAsync();
            }

            return stream;
        }
示例#5
0
        /// <summary>
        /// Use for small assets
        /// </summary>
        /// <param name="url"></param>
        /// <param name="cancelToken"></param>
        /// <returns></returns>
        public static async Task <IRandomAccessStream> DownloadFileAsync(LibrelioUrl url, CancellationToken cancelToken = default(CancellationToken))
        {
            var stream = new InMemoryRandomAccessStream();

            using (var response = await new HttpClient().GetAsync(url.AbsoluteUrl))
            {
                var buffer = await response.Content.ReadAsStringAsync();

                var dataWriter = new DataWriter(stream.GetOutputStreamAt(0));
                dataWriter.WriteString(buffer);
                await dataWriter.StoreAsync();

                await dataWriter.FlushAsync();
            }

            return(stream);
        }
示例#6
0
        /// <summary>
        /// Use for small assets
        /// </summary>
        /// <param name="url"></param>
        /// <param name="cancelToken"></param>
        /// <returns></returns>
        public static async Task <IRandomAccessStream> DownloadFileAsync(LibrelioUrl url, CancellationToken cancelToken = default(CancellationToken))
        {
            var stream = new InMemoryRandomAccessStream();
            var client = new HttpClient();

            client.DefaultRequestHeaders.Add("user-agent", "LibrelioWinRT");

            using (var response = await client.GetAsync(url))
            {
                var buffer = await response.Content.ReadAsStringAsync();

                var dataWriter = new DataWriter(stream.GetOutputStreamAt(0));
                dataWriter.WriteString(buffer);
                await dataWriter.StoreAsync();

                await dataWriter.FlushAsync();
            }

            return(stream);
        }
示例#7
0
        public static LibrelioLocalUrl FindInMetadata(LibrelioUrl url, XmlDocument xml)
        {
            if (xml == null)
            {
                return(null);
            }

            string xpath = "/root/mag[url='" + url.AbsoluteUrl + "']";
            var    nodes = xml.SelectNodes(xpath);

            if (nodes.Count > 0)
            {
                var index    = Convert.ToInt32(nodes[0].SelectNodes("index")[0].InnerText);
                var title    = nodes[0].SelectNodes("title")[0].InnerText;
                var subtitle = nodes[0].SelectNodes("subtitle")[0].InnerText;
                var path     = nodes[0].SelectNodes("path")[0].InnerText;
                if (path != "ND")
                {
                    var pos = path.LastIndexOf('\\');
                    path = path.Substring(0, pos + 1);
                }
                var metadata = nodes[0].SelectNodes("metadata")[0].InnerText;
                if (metadata != "ND")
                {
                    var pos = metadata.LastIndexOf('\\');
                    metadata = metadata.Substring(pos + 1);
                }
                var u   = nodes[0].SelectNodes("url")[0].InnerText;
                var rel = nodes[0].SelectNodes("relPath")[0].InnerText;
                var isd = nodes[0].SelectNodes("sampledownloaded")[0].InnerText;

                return(new LibrelioLocalUrl(index, title, subtitle, path, GetFullNameFromUrl(rel), u, rel, isd.Equals("true")));
            }
            else
            {
                return(null);
            }
        }
示例#8
0
 public static LibrelioLocalUrl ConvertToLocalUrl(LibrelioUrl url, StorageFolder folder, bool isd)
 {
     return(new LibrelioLocalUrl(url.Index, url.Title, url.Subtitle, folder.Path + "\\", url.FullName, url.AbsoluteUrl, url.RelativeUrl, isd));
 }
示例#9
0
 public static LibrelioLocalUrl ConvertToLocalUrl(LibrelioUrl url)
 {
     return(new LibrelioLocalUrl(url.Index, url.Title, url.Subtitle, "ND", url.FullName, url.AbsoluteUrl, url.RelativeUrl));
 }
示例#10
0
        public async Task<IRandomAccessStream> DownloadPDFAsync(LibrelioUrl magUrl, StorageFolder folder, IProgress<int> progress = null, CancellationToken cancelToken = default(CancellationToken))
        {
            HttpClient client = new HttpClient();

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, magUrl.AbsoluteUrl);

            int read = 0;
            int offset = 0;
            byte[] responseBuffer = new byte[1024];

            var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancelToken);
            response.EnsureSuccessStatusCode();

            var length = response.Content.Headers.ContentLength;

            cancelToken.ThrowIfCancellationRequested();

            var stream = new InMemoryRandomAccessStream();

            using (var responseStream = await response.Content.ReadAsStreamAsync())
            {
                do
                {
                    cancelToken.ThrowIfCancellationRequested();

                    read = await responseStream.ReadAsync(responseBuffer, 0, responseBuffer.Length);

                    cancelToken.ThrowIfCancellationRequested();

                    await stream.AsStream().WriteAsync(responseBuffer, 0, read);

                    offset += read;
                    uint val = (uint)(offset * 100 / length);
                    if (val >= 100) val = 99;
                    if (val <= 0) val = 1;
                    progress.Report((int)val);
                }
                while (read != 0);
            }

            progress.Report(100);

            await stream.FlushAsync();

            //var folder = await AddMagazineFolderStructure(magUrl);
            //var folder = await StorageFolder.GetFolderFromPathAsync(folderUrl);
            var file = await folder.CreateFileAsync(magUrl.FullName, CreationCollisionOption.ReplaceExisting);

            using (var protectedStream = await DownloadManager.ProtectPDFStream(stream))
            using (var fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
            //using (var unprotectedStream = await DownloadManager.UnprotectPDFStream(protectedStream))
            {

                await RandomAccessStream.CopyAsync(protectedStream, fileStream.GetOutputStreamAt(0));

                await fileStream.FlushAsync();
            }
            var pdfStream = new MagazineData();
            pdfStream.folderUrl = folder.Path + "\\";
            pdfStream.stream = stream;
            //var fileHandle =
            //    await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"Assets\test\testmagazine.pdf");

            //pdfStream.folderUrl = "C:\\Users\\Dorin\\Documents\\Magazines\\wind_355\\";
            //pdfStream.stream = await fileHandle.OpenReadAsync();

            return pdfStream.stream;
        }
示例#11
0
        public async Task<BitmapSource> DownloadThumbnailAsync(LibrelioUrl magUrl, StorageFolder folder)
        {
            string s = ".";
            if (magUrl.FullName.Contains("_."))
            {
                s = "_.";
            }
            var pos = magUrl.AbsoluteUrl.LastIndexOf(s);
            var url = magUrl.AbsoluteUrl.Substring(0, pos) + ".png";
            var stream = await DownloadManager.DownloadFileAsync(url);

            await DownloadManager.StoreToFolderAsync(magUrl.FullName.Replace(s + "pdf", ".png"), folder, stream);

            var bitmap = new BitmapImage();
            try
            {
                await bitmap.SetSourceAsync(stream);
            }
            catch
            {
                bitmap = null;
            }
            return bitmap;
        }
示例#12
0
 public static LibrelioLocalUrl ConvertToLocalUrl(LibrelioUrl url, StorageFolder folder, bool isd)
 {
     return new LibrelioLocalUrl(url.Index, url.Title, url.Subtitle, folder.Path + "\\", url.FullName, url.AbsoluteUrl, url.RelativeUrl, isd);
 }
示例#13
0
        public async Task<IRandomAccessStream> DownloadMagazineAsync(LibrelioUrl magUrl, string redirectUrl, StorageFolder folder, IProgress<int> progress = null, CancellationToken cancelToken = default(CancellationToken))
        {
            StatusText = "Download in progress";

            var tmpUrl = magUrl.AbsoluteUrl;
            magUrl.AbsoluteUrl = redirectUrl;
            var stream = await DownloadPDFAsync(magUrl, folder, progress, cancelToken);
            magUrl.AbsoluteUrl = tmpUrl;

            await GetUrlsFromPDF(stream);

            StatusText = "Downloading 2/" + (links.Count + 1);
            var url = DownloadManager.ConvertToLocalUrl(magUrl, folder);

            if (url != null)
            {
                await DownloadPDFAssetsAsync(url, links, progress, cancelToken);
            }

            StatusText = "Done";

            return stream;
        }
示例#14
0
        public async Task<StorageFolder> AddMagazineFolderStructure(LibrelioUrl magUrl)
        {
            var currentFolder = _folder;

            var relUrl = magUrl.RelativeUrl;
            var strs = relUrl.Split('/');

            for (int i = 0; i < strs.Length - 1; i++)
            {
                var folder = strs[i];
                if (folder != "")
                {
                    try
                    {
                        var fld = await currentFolder.CreateFolderAsync(folder, CreationCollisionOption.OpenIfExists);
                        currentFolder = fld;
                    }
                    catch
                    {
                        return null;
                    }
                }
            }

            if (currentFolder != _folder)
            {
            //    var magLocal = new LibrelioLocalUrl(magUrl.Title, magUrl.Subtitle, currentFolder.Path + "\\", 
            //                                        magUrl.FullName, magUrl.AbsoluteUrl, magUrl.RelativeUrl);

            //    if (!UpdataLocalUrl(magLocal))
            //        _magazinesLocalUrl.Add(magLocal);

            //    await AddUpdateMetadataEntry(magLocal);

                return currentFolder;
            }

            return null;
        }
示例#15
0
        public async Task MarkAsDownloaded(LibrelioUrl magUrl, StorageFolder currentFolder, bool isd)
        {
            if (currentFolder != _folder)
            {
                var magLocal = new LibrelioLocalUrl(magUrl.Index, magUrl.Title, magUrl.Subtitle, currentFolder.Path + "\\",
                                                    magUrl.FullName, magUrl.AbsoluteUrl, magUrl.RelativeUrl, isd);

                if (!UpdataLocalUrlDownloaded(magLocal))
                    _magazinesLocalUrlDownloaded.Add(magLocal);

                await AddUpdateMetadataDownloadedEntry(magLocal);

                var items = _magazinesLocalUrl.Where(magazine => magazine.FullName.Equals(magLocal.FullName));

                if (items != null && items.Count() > 0)
                {
                    var magazine = items.First();
                    var index = _magazinesLocalUrl.IndexOf(magazine);
                    if (index == -1) return;
                    _magazinesLocalUrl[index] = magLocal;
                }
            }
        }
示例#16
0
        public async Task<IRandomAccessStream> DownloadPDFAsync(LibrelioUrl magUrl, StorageFolder folder, bool isd, IProgress<int> progress = null, CancellationToken cancelToken = default(CancellationToken))
        {
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Add("user-agent", "LibrelioWinRT");

            var url = magUrl.AbsoluteUrl;
            if (isd) url = url.Replace("_.", ".");

            //HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);

            //int read = 0;
            //int offset = 0;
            //byte[] responseBuffer = new byte[1024];

            //var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancelToken);
            //response.EnsureSuccessStatusCode();

            //var length = response.Content.Headers.ContentLength;

            //cancelToken.ThrowIfCancellationRequested();

            //var stream = new InMemoryRandomAccessStream();

            //using (var responseStream = await response.Content.ReadAsStreamAsync())
            //{
            //    do
            //    {
            //        cancelToken.ThrowIfCancellationRequested();

            //        read = await responseStream.ReadAsync(responseBuffer, 0, responseBuffer.Length);

            //        cancelToken.ThrowIfCancellationRequested();

            //        await stream.AsStream().WriteAsync(responseBuffer, 0, read);

            //        offset += read;
            //        uint val = (uint)(offset * 100 / length);
            //        if (val >= 100) val = 99;
            //        if (val <= 0) val = 1;
            //        progress.Report((int)val);
            //    }
            //    while (read != 0);
            //}

            //progress.Report(100);

            //await stream.FlushAsync();

            ////var folder = await AddMagazineFolderStructure(magUrl);
            ////var folder = await StorageFolder.GetFolderFromPathAsync(folderUrl);
            var name = magUrl.FullName;
            if (isd) name = name.Replace("_.", ".");
            var file = await folder.CreateFileAsync(name, CreationCollisionOption.ReplaceExisting);

            //using (var protectedStream = await DownloadManager.ProtectPDFStream(stream))
            //using (var fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
            ////using (var unprotectedStream = await DownloadManager.UnprotectPDFStream(protectedStream))
            //{

            //    await RandomAccessStream.CopyAsync(protectedStream, fileStream.GetOutputStreamAt(0));

            //    await fileStream.FlushAsync();
            //}

            progress.Report(0);
            BackgroundDownloader downloader = new BackgroundDownloader();
            DownloadOperation download = downloader.CreateDownload(new Uri(url), file);

            await HandleDownloadAsync(download, true, progress, cancelToken);

            progress.Report(100);

            if (cancelToken.IsCancellationRequested)
                return null;

            var stream = await download.ResultFile.OpenAsync(FileAccessMode.ReadWrite);
            var returnStream = new InMemoryRandomAccessStream();
            await RandomAccessStream.CopyAsync(stream.GetInputStreamAt(0), returnStream.GetOutputStreamAt(0));
            await returnStream.FlushAsync();
            var protectedStram = await DownloadManager.ProtectPDFStream(stream);
            await RandomAccessStream.CopyAndCloseAsync(protectedStram.GetInputStreamAt(0), stream.GetOutputStreamAt(0));
            await protectedStram.FlushAsync();
            await stream.FlushAsync();
            protectedStram.Dispose();
            stream.Dispose();

            var pdfStream = new MagazineData();
            pdfStream.folderUrl = folder.Path + "\\";
            pdfStream.stream = returnStream;

            //var fileHandle =
            //    await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"Assets\test\testmagazine.pdf");

            //pdfStream.folderUrl = "C:\\Users\\Dorin\\Documents\\Magazines\\wind_355\\";
            //pdfStream.stream = await fileHandle.OpenReadAsync();

            return pdfStream.stream;
        }
示例#17
0
 public LibrelioLocalUrl FindInMetadata(LibrelioUrl url)
 {
     return DownloadManager.FindInMetadata(url, localXml);
 }
示例#18
0
        public async Task<IRandomAccessStream> DownloadMagazineAsync(LibrelioUrl magUrl, string redirectUrl, StorageFolder folder, bool isd, IProgress<int> progress = null, CancellationToken cancelToken = default(CancellationToken))
        {
            var loader = new ResourceLoader();
            StatusText = loader.GetString("download_progress");

            var tmpUrl = magUrl.AbsoluteUrl;
            magUrl.AbsoluteUrl = redirectUrl;
            var stream = await DownloadPDFAsync(magUrl, folder, isd, progress, cancelToken);
            magUrl.AbsoluteUrl = tmpUrl;
            if (stream == null || cancelToken.IsCancellationRequested) return null;

            await GetUrlsFromPDF(stream);

            StatusText = loader.GetString("downloading") + " 2/" + (links.Count + 1);
            var url = DownloadManager.ConvertToLocalUrl(magUrl, folder, isd);

            if (url != null)
            {
                await DownloadPDFAssetsAsync(url, links, progress, cancelToken);
            }

            StatusText = loader.GetString("done");

            return stream;
        }
示例#19
0
        public static LibrelioUrl ConvertFromLocalUrl(LibrelioLocalUrl url)
        {
            var mag = new LibrelioUrl(url.Index, url.Url.Replace(url.RelativePath, ""), url.RelativePath);
            mag.Title = url.Title;
            mag.Subtitle = url.Subtitle;

            return mag;
        }
示例#20
0
        public async Task<IRandomAccessStream> DownloadMagazineAsync(LibrelioUrl magUrl, StorageFolder folder, bool isd, IProgress<int> progress = null, CancellationToken cancelToken = default(CancellationToken))
        {
            StatusText = "Download in progress";

            var stream = await DownloadPDFAsync(magUrl, folder, isd, progress, cancelToken);
            if (stream == null || cancelToken.IsCancellationRequested) return null;

            await GetUrlsFromPDF(stream);

            StatusText = "Downloading 2/" + (links.Count+1);
            var url = DownloadManager.ConvertToLocalUrl(magUrl, folder, isd);

            if (url != null)
            {
                await DownloadPDFAssetsAsync(url, links, progress, cancelToken);
            }

            StatusText = "Done";

            return stream;
        }
示例#21
0
 public static LibrelioLocalUrl ConvertToLocalUrl(LibrelioUrl url)
 {
     return new LibrelioLocalUrl(url.Index, url.Title, url.Subtitle, "ND", url.FullName, url.AbsoluteUrl, url.RelativeUrl);
 }
示例#22
0
        public async Task MarkAsDownloaded(LibrelioUrl magUrl, StorageFolder currentFolder, bool isd)
        {
            if (currentFolder != _folder)
            {
                var magLocal = new LibrelioLocalUrl(magUrl.Index, magUrl.Title, magUrl.Subtitle, currentFolder.Path + "\\",
                                                    magUrl.FullName, magUrl.AbsoluteUrl, magUrl.RelativeUrl, isd);

                if (!UpdataLocalUrl(magLocal))
                    _magazinesLocalUrl.Add(magLocal);

                await AddUpdateMetadataEntry(magLocal);
            }
        }
示例#23
0
        public static LibrelioLocalUrl FindInMetadata(LibrelioUrl url, XmlDocument xml)
        {
            if (xml == null) return null;

            string xpath = "/root/mag[url='" + url.AbsoluteUrl + "']";
            var nodes = xml.SelectNodes(xpath);

            if (nodes.Count > 0)
            {
                var index = Convert.ToInt32(nodes[0].SelectNodes("index")[0].InnerText);
                var title = nodes[0].SelectNodes("title")[0].InnerText;
                var subtitle = nodes[0].SelectNodes("subtitle")[0].InnerText;
                var path = nodes[0].SelectNodes("path")[0].InnerText;
                if (path != "ND")
                {
                    var pos = path.LastIndexOf('\\');
                    path = path.Substring(0, pos + 1);
                }
                var metadata = nodes[0].SelectNodes("metadata")[0].InnerText;
                if (metadata != "ND")
                {
                    var pos = metadata.LastIndexOf('\\');
                    metadata = metadata.Substring(pos + 1);
                }
                var u = nodes[0].SelectNodes("url")[0].InnerText;
                var rel = nodes[0].SelectNodes("relPath")[0].InnerText;
                var isd = nodes[0].SelectNodes("sampledownloaded")[0].InnerText;

                return new LibrelioLocalUrl(index, title, subtitle, path, GetFullNameFromUrl(rel), u, rel, isd.Equals("true"));
            }
            else
            {
                return null;
            }
        }
示例#24
0
        private async Task ReadPList(XmlDocument plist)
        {
            _magazinesUrl.Clear();

            var items = plist.SelectNodes("/plist/array/dict");
            for (int i = 0; i < items.Count; i++)
            {
                var dict = items[i];
                LibrelioUrl url = null;
                string tite = "";
                string subtitle = "";

                foreach (var key in dict.SelectNodes("key"))
                {
                    if (key.InnerText == "FileName")
                    {
                        var relUrl = GetValue(key);

                        if (relUrl != "")
                            url = new LibrelioUrl(i, this._path, relUrl);
                    }
                    else if (key.InnerText == "Title")
                    {
                        tite = GetValue(key);
                    }
                    else if (key.InnerText == "Subtitle")
                    {
                        subtitle = GetValue(key);
                    }
                }

                if (url != null && tite != "")
                    url.Title = tite;
                if (url != null && subtitle != "")
                    url.Subtitle = subtitle;
                if (url != null)
                    _magazinesUrl.Add(url);
            }

            await UpdateLocalMetadataFromPLIST();
        }