Example #1
0
        public static void Flush(ExportBinContents ths)
        {
            switch (ths.mExportBinType)
            {
            case ExportBinType.Household:
                if (ths.mHouseholdContents != null)
                {
                    DownloadContent.DeleteHouseholdContents(ths.mHouseholdContents.ContentId);
                }
                ths.mHouseholdContents = null;
                break;

            case ExportBinType.HouseholdLot:
                if (ths.mLotContents != null)
                {
                    DownloadContent.DeleteLotContents(ths.mLotContents.ContentId);
                }
                ths.mLotContents       = null;
                ths.mHouseholdContents = null;
                break;

            case ExportBinType.Lot:
                if (ths.mLotContents != null)
                {
                    DownloadContent.DeleteLotContents(ths.mLotContents.ContentId);
                }
                ths.mLotContents       = null;
                ths.mHouseholdContents = null;
                break;
            }

            Household.CleanupOldIdToNewSimDescriptionMap();
        }
Example #2
0
        public static DownloadContent downloadUrl(Uri uri)
        {
            DownloadContent down = null;

            try
            {
                var httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(uri);

                httpWebRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

                httpWebRequest.Method = "GET";

                httpWebRequest.AllowAutoRedirect = true;

                var webResponse = httpWebRequest.GetResponse();

                if (webResponse is HttpWebResponse)
                {
                    down = ProcessResponse((HttpWebResponse)webResponse);
                    if (down != null)
                    {
                        return(down);
                    }
                }
            }
            catch (Exception e)
            {
            }

            return(down);
        }
Example #3
0
        public static SimDescription UnpackSim(MiniSimDescription ths, bool updateGenealogy)
        {
            try
            {
                if (ths == null)
                {
                    return(null);
                }

                // Calling ImportSimDescription prior to the Aging Manager being available is invalid, don't allow it
                if (AgingManager.Singleton == null)
                {
                    return(null);
                }

                SimDescription             desc      = new SimDescription();
                ResourceKeyContentCategory installed = ResourceKeyContentCategory.kInstalled;

                DownloadContent.ImportSimDescription(ths.mTravelKey, desc, ref installed);

                desc.SimDescriptionId = ths.mSimDescriptionId;

                if (desc.CareerManager != null)
                {
                    // Fixup for careers require a household, which is not set until later in this process

                    desc.CareerManager.mJob    = null;
                    desc.CareerManager.mSchool = null;
                }

                desc.Fixup();

                if (updateGenealogy && !GameStates.IsTravelling)
                {
                    if (desc.DefaultOutfitKey == ResourceKey.kInvalidResourceKey)
                    {
                        SimOutfit outfit = desc.GetOutfit(OutfitCategories.Everyday, 0x0);
                        if ((outfit == null) || (!outfit.IsValid))
                        {
                            desc.Dispose(false, false);
                            return(null);
                        }

                        desc.UpdateFromOutfit(OutfitCategories.Everyday);
                    }
                    desc.CASGenealogy = ths.CASGenealogy;
                }

                Corrections.CleanupBrokenSkills(desc, null);

                OccultTypeHelper.ValidateOccult(desc, null);

                return(desc);
            }
            catch (Exception e)
            {
                Common.Exception(ths.FullName, e);
                return(null);
            }
        }
Example #4
0
        public static bool ExportHouseholdForTravel(Household household)
        {
            try
            {
                foreach (Sim sim in Households.AllSims(household))
                {
                    sim.SetObjectToReset();

                    DreamCatcher.PruneDreamManager(sim);
                }

                Common.Sleep();

                ulong id = DownloadContent.GenerateGUID();
                HouseholdContentsProxy householdContents = new HouseholdContentsProxy(household);
                householdContents.Contents.ContentId = id;
                if (DownloadContent.StoreHouseholdContents(householdContents, id))
                {
                    DownloadContent.ExportLotContentsToTravelBin(id);
                    return(true);
                }
            }
            catch (Exception e)
            {
                Common.Exception("ExportHouseholdForTravel", e);
            }

            return(false);
        }
Example #5
0
    /// <summary>
    ///   下载失败
    /// </summary>
    void OnFailed(EmErrorCode code)
    {
        lock (lock_obj_)
        {
            if (content_ != null)
            {
                content_.State = DownloadContent.emState.Failed;
                content_.Close();
                content_ = null;
            }

            if (http_request_ != null)
            {
                http_request_.Abort();
                http_request_ = null;
            }

            IsDone    = true;
            ErrorCode = code;

            if (error_callback_ != null)
            {
                error_callback_(this);
            }
        }
    }
Example #6
0
 /// <summary>
 /// 提供外部调用下载开始接口函数
 /// </summary>
 public void Start(string root, string localname, Action <HttpAsyDownload, long> notify = null, Action <HttpAsyDownload> error = null)
 {
     lock (lock_object)
     {
         //TODO 不知道为什么要先终止下载
         //下载之前先终止
         Root      = root;
         LocalName = localname;
         if (notify != null)
         {
             notify_callback = notify;
         }
         if (error != null)
         {
             error_callback = error;
         }
         //设置各种属性的初始值
         IsDone          = false;
         ErrorCode       = emErrorCode.None;
         Content         = new DownloadContent(FullName);
         CompletedLength = 0;
         Length          = 0;
         //开始下载
     }
 }
Example #7
0
        private void PollDownloadContent(
            string inputJson,
            string downloadPath,
            string localPath,
            string overrides,
            bool reload,
            string cacheKey
            )
        {
            var queueName = "Download" + cacheKey;

            // Get queue lock
            var queueLock  = StringCache.getCache(queueName);
            var downloaded = false;
            var inputData  = new Inputs().FromJson(inputJson);

            if (reload)
            {
                StringCache.setCache(InstanceGuid.ToString(), "");
            }

            if (queueLock != "true" && inputData.Task != null)
            {
                StringCache.setCache(queueName, "true");
                StringCache.setCache(cacheKey, null);
                QueueManager.addToQueue(queueName, () => {
                    try
                    {
                        while (!downloaded)
                        {
                            StringCache.setCache(cacheKey + "progress", "Downloading...");
                            ExpireSolutionThreadSafe(true);

                            downloaded = DownloadContent.Download(inputJson, downloadPath, localPath, overrides);
                            StringCache.setCache(cacheKey, downloaded.ToString());

                            if (!downloaded)
                            {
                                StringCache.setCache(cacheKey + "progress", "Waiting for results...");
                                ExpireSolutionThreadSafe(true);
                                Thread.Sleep(60000);
                            }
                            else
                            {
                                StringCache.setCache(cacheKey + "progress", "Downloaded files");
                            }
                            ExpireSolutionThreadSafe(true);
                        }
                    }
                    catch (Exception e)
                    {
                        StringCache.setCache(InstanceGuid.ToString(), e.Message);
                        StringCache.setCache(cacheKey, "error");
                    }
                    ExpireSolutionThreadSafe(true);
                    Thread.Sleep(2000);
                    StringCache.setCache(queueName, "");
                });
            }
        }
        // 3/10/2020 I wish that we could both be there
        public async void Download()
        {
            if (DownloadContent.IsUgoira)
            {
                DownloadGif();
                return;
            }

            string path = null;

            try
            {
                await using var memory = await PixivIOHelper.Download(DownloadContent.GetDownloadUrl(), new Progress <double>(d => Progress = d), cancellationTokenSource.Token);

                if (DownloadContent.FromSpotlight)
                {
                    path = IsFromManga
                        ? Path.Combine(Directory.CreateDirectory(AppContext.DownloadPathProvider.GetSpotlightPath(DownloadContent.SpotlightTitle)).FullName, DownloadContent.Id, AppContext.FileNameFormatter.FormatManga(DownloadContent, MangaIndex))
                        : Path.Combine(Directory.CreateDirectory(AppContext.DownloadPathProvider.GetSpotlightPath(DownloadContent.SpotlightTitle)).FullName, AppContext.FileNameFormatter.Format(DownloadContent));
                }
                else if (IsFromManga)
                {
                    path = Path.Combine(Directory.CreateDirectory(AppContext.DownloadPathProvider.GetMangaPath(DownloadContent.Id)).FullName, AppContext.FileNameFormatter.FormatManga(DownloadContent, MangaIndex));
                }
                else
                {
                    path = Path.Combine(Directory.CreateDirectory(AppContext.DownloadPathProvider.GetIllustrationPath()).FullName, AppContext.FileNameFormatter.Format(DownloadContent));
                }
                if (cancellationTokenSource.IsCancellationRequested)
                {
                    return;
                }
                await using var fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
                memory.WriteTo(fileStream);
                Application.Current.Invoke(() => DownloadFinished?.Invoke(this));
            }
            catch (TaskCanceledException)
            {
                if (path != null && File.Exists(path))
                {
                    File.Delete(path);
                }
            }
            catch (Exception e)
            {
                if (!retried)
                {
                    Restart();
                    retried = true;
                }
                else
                {
                    HandleError(e, path);
                }
            }
        }
Example #9
0
 public void AddDownload(DownloadContent download)
 {
     if (download != null && !string.IsNullOrEmpty(download.GetString()))
     {
         Script script = new Script();
         script.Body = download.GetString();
         script.Name = UrlHelper.FileName(this.DownloadTask.RelativeUrl);
         this.Manager.SiteDb.Routes.AddOrUpdate(this.DownloadTask.RelativeUrl, script, this.Manager.UserId);
         this.Manager.SiteDb.Scripts.AddOrUpdate(script, this.Manager.UserId);
     }
 }
Example #10
0
        public static string DownloadHtml(string fullurl)
        {
            DownloadContent content = downloadUrl(fullurl);

            if (content == null)
            {
                return(string.Empty);
            }
            else
            {
                return(content.getString());
            }
        }
        public async void Download()
        {
            if (!modifiable)
            {
                return;
            }

            DownloadState.Value = DownloadStateEnum.Downloading;
            var downloadPath = GetPath();

            if (DownloadContent.IsUgoira)
            {
                DownloadGif();
                return;
            }

            try
            {
                await using var memory = await PixivIO.Download(DownloadContent.GetDownloadUrl(),
                                                                new Progress <double>(d => Progress = d),
                                                                cancellationTokenSource.Token);

                if (cancellationTokenSource.IsCancellationRequested)
                {
                    return;
                }
                await using var fileStream = new FileStream(downloadPath, FileMode.OpenOrCreate, FileAccess.ReadWrite,
                                                            FileShare.None);
                memory.WriteTo(fileStream);
                DownloadState.Value = DownloadStateEnum.Finished;
            }
            catch (OperationCanceledException)
            {
                if (downloadPath != null && File.Exists(downloadPath))
                {
                    File.Delete(downloadPath);
                }
            }
            catch (Exception e)
            {
                if (!retried)
                {
                    Restart();
                    retried = true;
                }
                else
                {
                    HandleError(e, downloadPath);
                }
            }
        }
Example #12
0
    /// <summary>
    ///
    /// </summary>
    void _OnReadCallback(IAsyncResult ar)
    {
        try
        {
            lock (lock_obj_)
            {
                DownloadContent rs = ar.AsyncState as DownloadContent;
                if (rs.ResponseStream == null)
                {
                    return;
                }

                int read = rs.ResponseStream.EndRead(ar);
                if (read > 0)
                {
                    rs.FS.Write(rs.Buffer, 0, read);
                    rs.FS.Flush();
                    CompletedLength += read;

                    if (notify_callback_ != null)
                    {
                        notify_callback_(this, (long)read);
                    }
                }
                else
                {
                    OnFinish();

                    if (notify_callback_ != null)
                    {
                        notify_callback_(this, (long)read);
                    }
                    return;
                }

                _BeginRead(new AsyncCallback(_OnReadCallback));
            }
        }
        catch (WebException e)
        {
            Debug.LogWarning("HttpAsyDownload - \"" + LocalName + "\" download failed!"
                             + "\nMessage:" + e.Message);
            OnFailed(EmErrorCode.DownloadError);
        }
        catch (System.Exception e)
        {
            Debug.LogWarning("HttpAsyDownload - \"" + LocalName + "\" download failed!"
                             + "\nMessage:" + e.Message);
            OnFailed(EmErrorCode.DownloadError);
        }
    }
Example #13
0
        public string MapContentType(DownloadContent downloadContent)
        {
            string fileExtension = Path.GetExtension(downloadContent.FileName).ToLowerInvariant();

            string contentType = downloadContent.ContentTypesDictionary.ContainsKey(fileExtension)
                ? downloadContent.ContentTypesDictionary[fileExtension] : null;

            if (contentType == null && !contentTypeProvider.TryGetContentType(downloadContent.FileName, out contentType))
            {
                contentType = DefaultContentType;
            }

            return(contentType);
        }
Example #14
0
        public async Task <DownloadContent> DownloadFile(string fileId)
        {
            var file = await GetFile(fileId);

            if (file != null)
            {
                var downloadedFile = await fileReader.Download(file.Path);

                var downloadContent = new DownloadContent(downloadedFile, file.Name);

                return(downloadContent);
            }

            return(null);
        }
Example #15
0
        public static HouseholdContentsProxy Import(string packageName)
        {
            ulong lotId = DownloadContent.ImportHouseholdContentsFromExportBin(packageName);

            if (lotId != 0x0L)
            {
                HouseholdContentsProxy householdContents = new HouseholdContentsProxy();
                if (DownloadContent.ImportHouseholdContents(householdContents, lotId))
                {
                    householdContents.mContents.ContentId = lotId;
                    return(householdContents);
                }
            }

            return(null);
        }
Example #16
0
        public void AddDownload(DownloadContent down)
        {
            var sitedb = this.Manager.SiteDb;

            string csstext = down.GetString();

            if (!String.IsNullOrEmpty(csstext))
            {
                Style style = new Style();
                style.Name = UrlHelper.FileName(this.DownloadTask.RelativeUrl);
                CssManager.ProcessResource(ref csstext, this.DownloadTask.AbsoluteUrl, this.Manager, this.DownloadTask.OwnerObjectId);
                style.Body = csstext;
                sitedb.Routes.AddOrUpdate(this.DownloadTask.RelativeUrl, style, this.Manager.UserId);
                sitedb.Styles.AddOrUpdate(style, this.Manager.UserId);
            }
        }
Example #17
0
        public static void Import(ExportBinContents ths, bool origImport)
        {
            switch (ths.mExportBinType)
            {
            case ExportBinType.Household:
                ulong lotId = DownloadContent.ImportHouseholdContentsFromExportBin(ths.mPackageName);
                if (lotId != 0x0L)
                {
                    // Custom
                    HouseholdContentsProxy proxy = new HouseholdContentsProxy();
                    if (DownloadContent.ImportHouseholdContents(proxy, lotId))
                    {
                        ths.mHouseholdContents           = proxy.Contents;
                        ths.mHouseholdContents.ContentId = lotId;
                    }
                }
                break;

            case ExportBinType.HouseholdLot:
            case ExportBinType.Lot:
                ulong oldLotContentId = 0x0L;
                ulong lotContendId    = DownloadContent.ImportLotContentsFromExportBin(ths.mPackageName, ref oldLotContentId);
                if (lotContendId != 0x0L)
                {
                    if (ths.mExportBinType == ExportBinType.HouseholdLot)
                    {
                        // Custom
                        HouseholdContentsProxy proxy = new HouseholdContentsProxy();
                        ths.mHouseholdContents = proxy.Contents;

                        if (DownloadContent.ImportHouseholdContents(proxy, lotContendId))
                        {
                            ths.mHouseholdContents.ContentId = lotContendId;
                        }
                    }

                    LotContents contents2 = new LotContents(ths.mPackageName, lotContendId, ths.mHouseholdContents, ths.mLotContentsWorth, ths.LotType, ths.LotContentsSizeX, ths.LotContentsSizeY, ths.IsHouseboatLot, ths.mLotMaxLevel, oldLotContentId, ths.mResidentialLotSubType, ths.mCommercialLotSubType);
                    ths.mLotContents = contents2;
                }
                break;
            }

            if (origImport || (ths.mIndexMap == null))
            {
                ths.mIndexMap = CreateIndexMap(ths.Household);
            }
        }
Example #18
0
        protected static DownloadContent ProcessResponse(HttpWebResponse httpWebResponse)
        {
            using (var responseStream = httpWebResponse.GetResponseStream())
            {
                DownloadContent downcontent = new DownloadContent();

                MemoryStream memorystream = new MemoryStream();
                responseStream.CopyTo(memorystream);
                byte[] databytes = memorystream.ToArray();

                if (httpWebResponse.StatusCode != HttpStatusCode.OK && httpWebResponse.StatusCode != HttpStatusCode.Moved && httpWebResponse.StatusCode != HttpStatusCode.Found)
                {
                    downcontent.StatusCode = 0;
                    return(null);
                }
                else
                {
                    downcontent.StatusCode = 200;
                }

                var contentType = httpWebResponse.Headers["content-type"];

                downcontent.ContentType = contentType.ToLower();

                if (string.IsNullOrEmpty(downcontent.ContentType) || downcontent.ContentType.Contains("text"))
                {
                    string   text     = string.Empty;
                    Encoding encoding = Encoding.Default;

                    encoding = ParseEncoding(databytes, contentType);

                    text = encoding.GetString(databytes).Trim(new [] { '\uFEFF', '\u200B' });

                    downcontent.isString      = true;
                    downcontent.ContentString = text;
                }

                else
                {
                    downcontent.DataBytes = databytes;
                    downcontent.isString  = false;
                }

                return(downcontent);
            }
        }
Example #19
0
        public static string DownloadCss(string fullurl)
        {
            DownloadContent content = downloadUrl(fullurl);

            if (content == null)
            {
                return(string.Empty);
            }

            if (content.ContentType.Contains("css"))
            {
                return(content.getString());
            }
            else
            {
                return(string.Empty);
            }
        }
Example #20
0
 private void OnFinish()
 {
     lock (lock_object)
     {
         if (Content != null)
         {
             Content.State = DownloadContent.emState.Completed;
             Content.Close();
             Content = null;
         }
         if (_httpWebRequest != null)
         {
             _httpWebRequest.Abort();
             _httpWebRequest = null;
         }
         IsDone = true;
     }
 }
Example #21
0
    /// <summary>
    ///   开始下载
    /// </summary>
    public void Start(string root, string local_file_name
                      , Action <HttpAsyDownload, long> notify = null
                      , Action <HttpAsyDownload> error_cb     = null)
    {
        lock (lock_obj_)
        {
            Abort();

            Root             = root;
            LocalName        = local_file_name;
            IsDone           = false;
            ErrorCode        = EmErrorCode.None;
            notify_callback_ = notify;
            error_callback_  = error_cb;
            content_         = new DownloadContent(FullName, false);
            CompletedLength  = 0;
            Length           = 0;
            _Download();
        }
    }
Example #22
0
    /// <summary>
    ///   下载完成
    /// </summary>
    void OnFinish()
    {
        lock (lock_obj_)
        {
            if (content_ != null)
            {
                content_.State = DownloadContent.emState.Completed;
                content_.Close();
                content_ = null;
            }

            if (http_request_ != null)
            {
                http_request_.Abort();
                http_request_ = null;
            }

            IsDone = true;
        }
    }
Example #23
0
 private void OnFailed(emErrorCode code)
 {
     lock (lock_object)
     {
         if (Content != null)
         {
             Content.State = DownloadContent.emState.Failed;
             Content.Close();
             Content = null;
         }
         if (_httpWebRequest != null)
         {
             _httpWebRequest.Abort();
             _httpWebRequest = null;
         }
         if (error_callback != null)
         {
             error_callback(this);
         }
     }
 }
Example #24
0
 private void OnReadCallbcak(IAsyncResult iaAsyncResult)
 {
     try
     {
         lock (lock_object)
         {
             DownloadContent content = iaAsyncResult.AsyncState as DownloadContent;
             if (content.ResponseStream == null)
             {
                 return;
             }
             int read = content.ResponseStream.EndRead(iaAsyncResult);
             if (read > 0)
             {
                 content.FS.Write(content.Buffer, 0, read);
                 content.FS.Flush();
                 CompletedLength += read;
                 if (notify_callback != null)
                 {
                     notify_callback(this, (long)read);
                 }
             }
             else
             {
                 OnFinish();
                 if (notify_callback != null)
                 {
                     notify_callback(this, (long)read);
                 }
             }
             BeginRead(OnReadCallbcak);
         }
     }
     catch (Exception e)
     {
         Debug.LogError(e.Message);
         OnFailed(emErrorCode.DownLoadError);
     }
 }
Example #25
0
        public static HouseholdContents ImportHouseholdForTravel()
        {
            try
            {
                ulong lotId = DownloadContent.ImportHouseholdContentsFromTravelBin();
                if (lotId != 0x0L)
                {
                    HouseholdContentsProxy householdContents = new HouseholdContentsProxy();
                    if (DownloadContent.ImportHouseholdContents(householdContents, lotId))
                    {
                        householdContents.Contents.ContentId = lotId;
                        return(householdContents.Contents);
                    }
                }
            }
            catch (Exception e)
            {
                Common.Exception("ImportHouseholdForTravel", e);
            }

            return(null);
        }
Example #26
0
        public static async Task <SiteObject> continueDownload(SiteDb siteDb, string RelativeUrl)
        {
            if (!siteDb.WebSite.ContinueDownload)
            {
                return(null);
            }

            var oktoDownload = await siteDb.TransferTasks.CanStartDownload(RelativeUrl);

            if (!oktoDownload)
            {
                return(null);
            }

            var history = siteDb.TransferTasks.History().ToList();

            if (history.Count() == 0)
            {
                return(null);
            }
            /// track failed history...
            Guid downloadid             = RelativeUrl.ToHashGuid();
            DownloadFailTrack failtrack = siteDb.DownloadFailedLog.Get(downloadid);

            if (failtrack != null)
            {
                if (failtrack.HistoryTime.Where(o => o > DateTime.Now.AddMinutes(-30)).Any())
                {
                    return(null);
                }
            }
            else
            {
                failtrack    = new DownloadFailTrack();
                failtrack.Id = downloadid;
            }

            string          fullurl  = string.Empty;
            DownloadContent download = null;

            string hostname = TransferHelper.GetPossibleHostName(RelativeUrl);

            if (!string.IsNullOrEmpty(hostname))
            {
                var findurl = history.Find(o => o.ToLower().EndsWith(hostname.ToLower()));

                if (!string.IsNullOrEmpty(findurl))
                {
                    string newrelative = RelativeUrl.Replace(hostname + "/", "");
                    fullurl = UrlHelper.Combine(findurl, newrelative);
                    var cookiecontianer = siteDb.TransferTasks.GetCookieContainerByFullUrl(fullurl);
                    download = await DownloadHelper.DownloadUrlAsync(fullurl, cookiecontianer);
                }
            }

            if (download == null)
            {
                foreach (var item in history)
                {
                    fullurl = UrlHelper.Combine(item, RelativeUrl);
                    var cookiecontianer = siteDb.TransferTasks.GetCookieContainerByFullUrl(fullurl);
                    download = await DownloadHelper.DownloadUrlAsync(fullurl, cookiecontianer);

                    if (download != null)
                    {
                        break;
                    }
                }
            }

            ///// 301, 302, will be converted to 200 and return back as well. So it is safe to == 200.
            if (download != null && download.StatusCode == 200)
            {
                DownloadManager downloadManager = new DownloadManager()
                {
                    SiteDb = siteDb
                };
                SiteObject downloadobject = TransferHelper.AddDownload(downloadManager, download, fullurl, false, true, fullurl);

                if (downloadobject is Page || downloadobject is View)
                {
                    siteDb.TransferPages.AddOrUpdate(new TransferPage()
                    {
                        absoluteUrl = fullurl, PageId = downloadobject.Id
                    });
                }

                /// for continue download content...
                Continue.ContinueTask.Convert(siteDb, downloadobject);

                siteDb.TransferTasks.ReleaseDownload(RelativeUrl);
                return(downloadobject);
            }
            else
            {
                siteDb.TransferTasks.ReleaseDownload(RelativeUrl);
            }

            //download failed.
            failtrack.HistoryTime.Add(DateTime.Now);
            siteDb.DownloadFailedLog.AddOrUpdate(failtrack);

            if (failtrack.HistoryTime.Count() > 5)
            {
                var filetype = Kooboo.Lib.Helper.UrlHelper.GetFileType(RelativeUrl);

                byte consttype;
                switch (filetype)
                {
                case UrlHelper.UrlFileType.Image:
                    consttype = ConstObjectType.Image;
                    break;

                case UrlHelper.UrlFileType.JavaScript:
                    consttype = ConstObjectType.Script;
                    break;

                case UrlHelper.UrlFileType.Style:
                    consttype = ConstObjectType.Style;
                    break;

                case UrlHelper.UrlFileType.File:
                    consttype = ConstObjectType.CmsFile;
                    break;

                case UrlHelper.UrlFileType.PageOrView:
                    consttype = ConstObjectType.Page;
                    break;

                default:
                    consttype = 0;
                    break;
                }

                // siteDb.Routes.EnsureExists(RelativeUrl, consttype, default(Guid));
            }

            return(null);
        }
Example #27
0
        public static async Task <SiteObject> continueDownload(SiteDb siteDb, string RelativeUrl)
        {
            if (!siteDb.WebSite.ContinueDownload)
            {
                return(null);
            }

            string orgimport = null;

            var history = siteDb.TransferTasks.History().ToList();

            if (history.Count() == 0)
            {
                return(null);
            }
            else
            {
                orgimport = history.First();
            }

            /// track failed history...
            Guid downloadid = RelativeUrl.ToHashGuid();

            DownloadFailTrack failtrack = siteDb.DownloadFailedLog.Get(downloadid);

            if (failtrack != null)
            {
                if (failtrack.HistoryTime.Where(o => o > DateTime.Now.AddMinutes(-30)).Any())
                {
                    return(null);
                }

                if (failtrack.HistoryTime.Count() > 3)
                {
                    return(null);
                }
            }
            else
            {
                failtrack    = new DownloadFailTrack();
                failtrack.Id = downloadid;
            }

            var oktoDownload = await siteDb.TransferTasks.CanStartDownload(RelativeUrl);

            if (!oktoDownload)
            {
                return(null);
            }


            string          fullurl  = string.Empty;
            DownloadContent download = null;

            if (RelativeUrl.EndsWith("favicon.ico"))
            {
                return(null);
            }

            string hostname = TransferHelper.GetPossibleHostName(RelativeUrl);

            if (!string.IsNullOrEmpty(hostname))
            {
                var findurl = history.Find(o => o.ToLower().EndsWith(hostname.ToLower()));

                if (!string.IsNullOrEmpty(findurl))
                {
                    string newrelative = RelativeUrl.Replace(hostname + "/", "");
                    fullurl = UrlHelper.Combine(findurl, newrelative);
                    var cookiecontianer = siteDb.TransferTasks.GetCookieContainerByFullUrl(fullurl);
                    download = await DownloadHelper.DownloadUrlAsync(fullurl, cookiecontianer);
                }
                else
                {
                    string newrelative = RelativeUrl.Replace(hostname + "/", "");
                    // check whether it is https or not.
                    // fullurl = UrlHelper.Combine(hostname, newrelative);
                    var protocol = OrgProtocol(orgimport);
                    fullurl = protocol + hostname + newrelative;
                    var cookiecontianer = siteDb.TransferTasks.GetCookieContainerByFullUrl(fullurl);
                    download = await DownloadHelper.DownloadUrlAsync(fullurl, cookiecontianer);
                }
            }

            if (download == null)
            {
                foreach (var item in history)
                {
                    fullurl = UrlHelper.Combine(item, RelativeUrl);
                    var cookiecontianer = siteDb.TransferTasks.GetCookieContainerByFullUrl(fullurl);
                    download = await DownloadHelper.DownloadUrlAsync(fullurl, cookiecontianer);

                    if (download != null)
                    {
                        break;
                    }
                }
            }

            ///// 301, 302, will be converted to 200 and return back as well. So it is safe to == 200.
            if (download != null && download.StatusCode == 200)
            {
                DownloadManager downloadManager = new DownloadManager()
                {
                    SiteDb = siteDb
                };
                SiteObject downloadobject = TransferHelper.AddDownload(downloadManager, download, fullurl, false, true, orgimport);

                if (downloadobject is Page || downloadobject is View)
                {
                    siteDb.TransferPages.AddOrUpdate(new TransferPage()
                    {
                        absoluteUrl = fullurl, PageId = downloadobject.Id
                    });
                }

                siteDb.TransferTasks.ReleaseDownload(RelativeUrl);
                ///for continue download content...
                Continue.ContinueTask.Convert(siteDb, downloadobject);
                return(downloadobject);
            }
            else
            {
                siteDb.TransferTasks.ReleaseDownload(RelativeUrl);
            }

            //download failed.
            failtrack.HistoryTime.Add(DateTime.Now);
            siteDb.DownloadFailedLog.AddOrUpdate(failtrack);

            return(null);
        }
Example #28
0
        public static SiteObject AddDownload(DownloadManager manager, DownloadContent download, string absoluteUrl, bool DefaultStartPage, bool AnalyzePage, string originalimporturl = "")
        {
            var sitedb = manager.SiteDb;

            bool issamehost = UrlHelper.isSameHost(originalimporturl, absoluteUrl);

            string relativeurl = UrlHelper.RelativePath(absoluteUrl, issamehost);

            byte consttype = 0;

            var FileType = Kooboo.Lib.Helper.UrlHelper.GetFileType(relativeurl, download.ContentType);

            switch (FileType)
            {
            case UrlHelper.UrlFileType.Image:
            {
                consttype = ConstObjectType.Image;
            }
            break;

            case UrlHelper.UrlFileType.JavaScript:
            {
                consttype = ConstObjectType.Script;
            }
            break;

            case UrlHelper.UrlFileType.Style:
            {
                consttype = ConstObjectType.Style;
            }
            break;

            case UrlHelper.UrlFileType.File:
            {
                consttype = ConstObjectType.File;
            }
            break;

            default:
            {
                string htmlsource = download.GetString();

                if (string.IsNullOrEmpty(htmlsource) || Kooboo.HtmlUtility.HasHtmlOrBodyTag(htmlsource))
                {
                    consttype = ConstObjectType.Page;
                }
                else
                {
                    consttype = ConstObjectType.View;
                }
            }
            break;
            }

            switch (consttype)
            {
            case ConstObjectType.Page:
            {
                Page page = new Page();
                page.IsStatic     = true;
                page.DefaultStart = DefaultStartPage;

                page.Name = Service.PageService.GetPageNameFromUrl(relativeurl);

                string htmlsource = UrlHelper.ReplaceMetaCharSet(download.GetString());

                if (AnalyzePage)
                {
                    var context = AnalyzerManager.Execute(htmlsource, absoluteUrl, page.Id, page.ConstType, manager, originalimporturl);
                    htmlsource = context.HtmlSource;
                }
                page.Body = htmlsource;

                if (page.Name == "untitled")
                {
                    var titleel = Service.DomService.GetTitleElement(page.Dom);
                    if (titleel != null)
                    {
                        string title = titleel.InnerHtml;
                        if (!string.IsNullOrEmpty(title))
                        {
                            title     = Lib.Helper.StringHelper.SementicSubString(title, 0, 30);
                            page.Name = title.Trim();
                        }
                    }
                }

                sitedb.Routes.AddOrUpdate(relativeurl, page, manager.UserId);
                sitedb.Pages.AddOrUpdate(page, manager.UserId);

                return(page);
            }

            case ConstObjectType.Style:
            {
                string csstext = download.GetString();
                if (AnalyzePage)
                {
                    CssManager.ProcessResource(ref csstext, absoluteUrl, manager, default(Guid));
                }
                var style = new Style();
                style.Body = csstext;
                sitedb.Routes.AddOrUpdate(relativeurl, style, manager.UserId);
                sitedb.Styles.AddOrUpdate(style, manager.UserId);
                return(style);
            }

            case ConstObjectType.Script:
            {
                Script script = new Script {
                    ConstType = ConstObjectType.Script, Body = download.GetString()
                };

                sitedb.Routes.AddOrUpdate(relativeurl, script, manager.UserId);
                sitedb.Scripts.AddOrUpdate(script, manager.UserId);
                return(script);
            }

            case ConstObjectType.Image:
            {
                Image koobooimage = new Image
                {
                    ContentBytes = download.DataBytes,
                    Extension    = UrlHelper.FileExtension(relativeurl),
                    Name         = UrlHelper.FileName(relativeurl),
                };


                sitedb.Routes.AddOrUpdate(relativeurl, koobooimage, manager.UserId);
                sitedb.Images.AddOrUpdate(koobooimage, manager.UserId);
                return(koobooimage);
            }

            case ConstObjectType.View:
            {
                var view = new View();

                string name = System.IO.Path.GetFileNameWithoutExtension(absoluteUrl);
                if (string.IsNullOrEmpty(name))
                {
                    name = "untitlted";
                }
                view.Name = name;

                string htmlsource = UrlHelper.ReplaceMetaCharSet(download.GetString());
                if (AnalyzePage)
                {
                    var context = AnalyzerManager.Execute(htmlsource, absoluteUrl, view.Id, view.ConstType, manager, originalimporturl);
                    htmlsource = context.HtmlSource;
                }

                view.Body = htmlsource;

                sitedb.Routes.AddOrUpdate(relativeurl, view, manager.UserId);
                sitedb.Views.AddOrUpdate(view, manager.UserId);
                return(view);
            }

            default:
            {
                /// default treat it as file.
                var kooboofile = new CmsFile
                {
                    ContentType   = download.ContentType,
                    ContentBytes  = download.DataBytes,
                    ContentString = download.ContentString,
                    Extension     = UrlHelper.FileExtension(relativeurl),
                    Name          = UrlHelper.FileName(relativeurl)
                };
                sitedb.Routes.AddOrUpdate(relativeurl, kooboofile, manager.UserId);
                sitedb.Files.AddOrUpdate(kooboofile, manager.UserId);
                return(kooboofile);
            }
            }
        }
Example #29
0
        //public static string NExportHousehold(Bin ths, Household household, bool includeLotContents, bool isMovingPacked)
        //{
        //    return NExportHousehold(ths, household, includeLotContents, isMovingPacked, false);
        //}
        public static string NExportHousehold(Bin ths, Household household, bool includeLotContents, bool isMovingPacked, bool noReset, bool noThum)
        {
            if (ths == null)
            {
                throw new NullReferenceException();
            }
            if (household == null)
            {
                throw new ArgumentNullException("household");
            }
            try
            {
                string createdPackageFile = null;
                if (GameUtils.IsInstalled(ProductVersion.EP4))
                {
                    OccultImaginaryFriend.ForceHouseholdImaginaryFriendsBackToInventory(household);
                }

                if (!noReset)
                {
                    foreach (Sim sim in household.AllActors)
                    {
                        sim.SetObjectToReset();
                    }
                }

                if (Simulator.CheckYieldingContext(false))
                {
                    Simulator.Sleep(0);
                }

                ThumbnailSizeMask sizeMask;
                if (includeLotContents)
                {
                    Lot lotHome = household.LotHome;
                    if (lotHome != null)
                    {
                        int householdFunds  = household.FamilyFunds;
                        int lotHomeLotWorth = World.GetEmptyLotWorth(lotHome.LotId) + ((int)World.GetLotAdditionalPropertyValue(lotHome.LotId));

                        household.SetFamilyFunds(householdFunds + lotHomeLotWorth, false);

                        EditTownModel.SendObjectsToProperLot(lotHome);

                        ulong contentID = DownloadContent.StoreLotContents(lotHome, lotHome.LotId);
                        if (contentID != 0)
                        {
                            ThumbnailHelper.GenerateLotThumbnailSet(lotHome.LotId, contentID, ThumbnailSizeMask.ExtraLarge);
                            ThumbnailHelper.GenerateLotThumbnail(lotHome.LotId, contentID, 0, ThumbnailSizeMask.Large | ThumbnailSizeMask.Medium);
                            sizeMask = ThumbnailSizeMask.Large | ThumbnailSizeMask.ExtraLarge | ThumbnailSizeMask.Medium;
                            ThumbnailManager.GenerateHouseholdThumbnail(household.HouseholdId, contentID, sizeMask);

                            if (!noThum && household.AllSimDescriptions.Count < 12)
                            {
                                ths.GenerateSimThumbnails(household, contentID, ThumbnailSizeMask.Large | ThumbnailSizeMask.ExtraLarge);
                            }

                            HouseholdContentsProxy contents = new HouseholdContentsProxy(household);

                            if (DownloadContent.StoreHouseholdContents(contents, contentID))
                            {
                                createdPackageFile = DownloadContent.ExportLotContentsToExportBin(contentID);
                            }

                            ThumbnailManager.InvalidateLotThumbnails(lotHome.LotId, contentID, ThumbnailSizeMask.ExtraLarge);
                            ThumbnailManager.InvalidateLotThumbnailsForGroup(lotHome.LotId, contentID, ThumbnailSizeMask.Medium, 0);
                            ThumbnailManager.InvalidateHouseholdThumbnail(household.HouseholdId, contentID, sizeMask);

                            try
                            {
                                ths.InvalidateSimThumbnails(household, contentID);
                            }
                            catch (Exception)
                            { if (IsOpenDGSInstalled)
                              {
                                  return(null);
                              }
                            }
                        }
                        household.SetFamilyFunds(householdFunds, false);
                        return(createdPackageFile);
                    }
                    //return createdPackageFile;
                }

                int familyFunds     = household.FamilyFunds;
                int realEstateFunds = 0;
                if (household.RealEstateManager != null)
                {
                    foreach (IPropertyData data in household.RealEstateManager.AllProperties)
                    {
                        realEstateFunds += data.TotalValue;
                    }
                }

                if (household.LotHome != null)
                {
                    int lotWorth = 0;
                    if (isMovingPacked)
                    {
                        lotWorth = World.GetUnfurnishedLotWorth(household.LotHome.LotId) + realEstateFunds;
                    }
                    else
                    {
                        lotWorth = World.GetLotWorth(household.LotHome.LotId) + realEstateFunds;
                    }

                    household.SetFamilyFunds(household.FamilyFunds + lotWorth, false);
                }

                if (household.FamilyFunds < 20000)
                {
                    household.SetFamilyFunds(20000, false);
                }

                ulong gGUID = DownloadContent.GenerateGUID();
                HouseholdContentsProxy householdContents = new HouseholdContentsProxy(household);
                householdContents.Contents.ContentId = gGUID;

                sizeMask = ThumbnailSizeMask.Large | ThumbnailSizeMask.ExtraLarge | ThumbnailSizeMask.Medium;

                if (!noThum && household.AllSimDescriptions.Count < 12)
                {
                    ThumbnailManager.GenerateHouseholdThumbnail(household.HouseholdId, gGUID, sizeMask);
                    ths.GenerateSimThumbnails(household, gGUID, ThumbnailSizeMask.Large | ThumbnailSizeMask.ExtraLarge);
                }

                if (DownloadContent.StoreHouseholdContents(householdContents, gGUID))
                {
                    createdPackageFile = DownloadContent.ExportLotContentsToExportBin(gGUID);
                }

                ThumbnailManager.InvalidateHouseholdThumbnail(household.HouseholdId, gGUID, sizeMask);

                try
                {
                    ths.InvalidateSimThumbnails(household, gGUID);
                }
                catch (Exception)
                { if (IsOpenDGSInstalled)
                  {
                      return(null);
                  }
                }

                household.SetFamilyFunds(familyFunds, false);
                return(createdPackageFile);
            }
            catch (Exception)
            { return(null); }
        }
Example #30
0
        public static string ExportHousehold(Bin ths, Household household, bool includeLotContents, bool isMovingPacked)
        {
            try
            {
                string str = null;
                if (GameUtils.IsInstalled(ProductVersion.EP4))
                {
                    OccultImaginaryFriend.ForceHouseholdImaginaryFriendsBackToInventory(household);
                }

                foreach (Sim sim in household.AllActors)
                {
                    sim.SetObjectToReset();
                }

                Common.Sleep();

                if (includeLotContents)
                {
                    Lot lotHome = household.LotHome;
                    if (lotHome != null)
                    {
                        int num  = household.FamilyFunds;
                        int num2 = World.GetEmptyLotWorth(lotHome.LotId) + ((int)World.GetLotAdditionalPropertyValue(lotHome.LotId));
                        household.SetFamilyFunds(household.FamilyFunds + num2, false);
                        EditTownModel.SendObjectsToProperLot(lotHome);
                        ulong contentId = DownloadContent.StoreLotContents(lotHome, lotHome.LotId);
                        if (contentId != 0x0L)
                        {
                            ThumbnailHelper.GenerateLotThumbnailSet(lotHome.LotId, contentId, ThumbnailSizeMask.ExtraLarge);
                            ThumbnailHelper.GenerateLotThumbnail(lotHome.LotId, contentId, 0x0, ThumbnailSizeMask.Large | ThumbnailSizeMask.Medium);
                            ThumbnailSizeMask mask = ThumbnailSizeMask.Large | ThumbnailSizeMask.ExtraLarge | ThumbnailSizeMask.Medium;
                            ThumbnailManager.GenerateHouseholdThumbnail(household.HouseholdId, contentId, mask);
                            ths.GenerateSimThumbnails(household, contentId, ThumbnailSizeMask.Large | ThumbnailSizeMask.ExtraLarge);
                            HouseholdContentsProxy contents = new HouseholdContentsProxy(household);
                            if (DownloadContent.StoreHouseholdContents(contents, contentId))
                            {
                                str = DownloadContent.ExportLotContentsToExportBin(contentId);
                            }
                            ThumbnailManager.InvalidateLotThumbnails(lotHome.LotId, contentId, ThumbnailSizeMask.ExtraLarge);
                            ThumbnailManager.InvalidateLotThumbnailsForGroup(lotHome.LotId, contentId, ThumbnailSizeMask.Medium, 0x0);
                            ThumbnailManager.InvalidateHouseholdThumbnail(household.HouseholdId, contentId, mask);
                            ths.InvalidateSimThumbnails(household, contentId);
                        }
                        household.SetFamilyFunds(num, false);
                    }
                    return(str);
                }

                int familyFunds     = household.FamilyFunds;
                int realEstateFunds = 0;
                if (household.RealEstateManager != null)
                {
                    foreach (IPropertyData data in household.RealEstateManager.AllProperties)
                    {
                        realEstateFunds += data.TotalValue;
                    }
                }
                if (household.LotHome != null)
                {
                    int lotWorth = 0;
                    if (isMovingPacked)
                    {
                        lotWorth = World.GetUnfurnishedLotWorth(household.LotHome.LotId) + realEstateFunds;
                    }
                    else
                    {
                        lotWorth = World.GetLotWorth(household.LotHome.LotId) + realEstateFunds;
                    }

                    household.SetFamilyFunds(household.FamilyFunds + lotWorth, false);
                }

                if (household.FamilyFunds < 0x4e20)
                {
                    household.SetFamilyFunds(0x4e20, false);
                }

                ulong cacheId = DownloadContent.GenerateGUID();
                HouseholdContentsProxy householdContents = new HouseholdContentsProxy(household);
                householdContents.Contents.ContentId = cacheId;
                ThumbnailSizeMask sizeMask = ThumbnailSizeMask.Large | ThumbnailSizeMask.ExtraLarge | ThumbnailSizeMask.Medium;
                ThumbnailManager.GenerateHouseholdThumbnail(household.HouseholdId, cacheId, sizeMask);
                ths.GenerateSimThumbnails(household, cacheId, ThumbnailSizeMask.Large | ThumbnailSizeMask.ExtraLarge);
                if (DownloadContent.StoreHouseholdContents(householdContents, cacheId))
                {
                    str = DownloadContent.ExportLotContentsToExportBin(cacheId);
                }

                ThumbnailManager.InvalidateHouseholdThumbnail(household.HouseholdId, cacheId, sizeMask);
                ths.InvalidateSimThumbnails(household, cacheId);
                household.SetFamilyFunds(familyFunds, false);
                return(str);
            }
            catch (Exception e)
            {
                Common.Exception("ExportHousehold", e);
                return(null);
            }
        }