コード例 #1
0
ファイル: EditModel.cs プロジェクト: jerry-he/Piranha
		/// <summary>
		/// Saves the edit model.
		/// </summary>
		public bool SaveAll(bool draft) {
			var context = HttpContext.Current;
			var hasfile = UploadedFile != null || ServerFile != null;
			byte[] data = null;
			WebClient web = new WebClient();

			// Check if the original URL has been updated, and if so 
			if (!Content.IsNew && !String.IsNullOrEmpty(Content.OriginalUrl)) {
				var old = Content.GetSingle(Content.Id);
				if (old != null) {
					if (Content.OriginalUrl != old.OriginalUrl) {
						FileUrl = Content.OriginalUrl;
					}
				}
			}

			// Download file from web
			if (!hasfile && !String.IsNullOrEmpty(FileUrl)) {
				data = web.DownloadData(FileUrl);
				Content.OriginalUrl = FileUrl;
				Content.LastSynced = Convert.ToDateTime(web.ResponseHeaders[HttpResponseHeader.LastModified]);
			}

			var media = new MediaFileContent();
			if (hasfile) {
				if (UploadedFile != null) {
					media.Filename = UploadedFile.FileName;
					media.ContentType = UploadedFile.ContentType;
					using (var reader = new BinaryReader(UploadedFile.InputStream)) {
						media.Body = reader.ReadBytes(Convert.ToInt32(UploadedFile.InputStream.Length));
					}
				} else {
					media.Filename = ServerFile.Name;
					media.ContentType = MimeType.Get(ServerFile.Name);
					using (var stream = ServerFile.OpenRead()) {
						media.Body = new byte[ServerFile.Length];
						stream.Read(media.Body, 0, media.Body.Length);
					}
				}
			} else if (data != null) {
				media.Filename = FileUrl.Substring(FileUrl.LastIndexOf('/') + 1);
				media.ContentType = web.ResponseHeaders["Content-Type"];
				media.Body = data;
			} else {
				media = null;
			}

			var saved = false;

			if (!Content.IsFolder) {
				// Only save permalinks for non-folders
				var filename = !String.IsNullOrEmpty(Content.Filename) ? Content.Filename : (!String.IsNullOrEmpty(media.Filename) ? media.Filename : "");
				if (Permalink.IsNew && String.IsNullOrEmpty(Permalink.Name))
					Permalink.Name = Permalink.Generate(!Content.IsFolder ? filename : Content.Name, Models.Permalink.PermalinkType.MEDIA);
				try {
					Permalink.Save();
				} catch (DuplicatePermalinkException) {
					if (Permalink.IsNew) {
						Permalink.Name = Content.Id + Permalink.Name.Substring(Permalink.Name.LastIndexOf('.'));
						Permalink.Save();
					} else throw;
				}
			} else {
				Content.PermalinkId = Guid.Empty;
			}

			if (draft)
				saved = Content.Save(media);
			else saved = Content.SaveAndPublish(media);

			if (saved) {
				// Save related information
				Relation.DeleteByDataId(Content.Id);
				List<Relation> relations = new List<Relation>();
				ContentCategories.ForEach(c => relations.Add(new Relation() {
					DataId = Content.Id, RelatedId = c, IsDraft = false, Type = Relation.RelationType.CONTENTCATEGORY
				})
					);
				relations.ForEach(r => r.Save());

				// Save extensions
				foreach (var ext in Extensions) {
					// Call OnSave
					ext.Body.OnManagerSave(Content);

					ext.ParentId = Content.Id;
					ext.Save();
					if (!draft) {
						if (Extension.GetScalar("SELECT COUNT(extension_id) FROM extension WHERE extension_id=@0 AND extension_draft=0", ext.Id) == 0)
							ext.IsNew = true;
						ext.IsDraft = false;
						ext.Save();
					}
				}
				// Reset file url
				FileUrl = "";

				return true;
			}
			return false;
		}
コード例 #2
0
ファイル: EditModel.cs プロジェクト: becdetat/Piranha
        /// <summary>
        /// Saves the edit model.
        /// </summary>
        public bool SaveAll(bool draft)
        {
            var context = HttpContext.Current;
            var hasfile = UploadedFile != null || ServerFile != null;

            byte[]    data = null;
            WebClient web  = new WebClient();

            // Check if the original URL has been updated, and if so
            if (!Content.IsNew && !String.IsNullOrEmpty(Content.OriginalUrl))
            {
                var old = Content.GetSingle(Content.Id);
                if (old != null)
                {
                    if (Content.OriginalUrl != old.OriginalUrl)
                    {
                        FileUrl = Content.OriginalUrl;
                    }
                }
            }

            // Download file from web
            if (!hasfile && !String.IsNullOrEmpty(FileUrl))
            {
                data = web.DownloadData(FileUrl);
                Content.OriginalUrl = FileUrl;
                Content.LastSynced  = Convert.ToDateTime(web.ResponseHeaders[HttpResponseHeader.LastModified]);
            }

            var media = new MediaFileContent();

            if (hasfile)
            {
                if (UploadedFile != null)
                {
                    media.Filename    = UploadedFile.FileName;
                    media.ContentType = UploadedFile.ContentType;
                    using (var reader = new BinaryReader(UploadedFile.InputStream)) {
                        media.Body = reader.ReadBytes(Convert.ToInt32(UploadedFile.InputStream.Length));
                    }
                }
                else
                {
                    media.Filename    = ServerFile.Name;
                    media.ContentType = MimeType.Get(ServerFile.Name);
                    using (var stream = ServerFile.OpenRead()) {
                        media.Body = new byte[ServerFile.Length];
                        stream.Read(media.Body, 0, media.Body.Length);
                    }
                }
            }
            else if (data != null)
            {
                media.Filename    = FileUrl.Substring(FileUrl.LastIndexOf('/') + 1);
                media.ContentType = web.ResponseHeaders["Content-Type"];
                media.Body        = data;
            }
            else
            {
                media = null;
            }

            var saved = false;

            if (!Content.IsFolder)
            {
                // Only save permalinks for non-folders
                var filename = !String.IsNullOrEmpty(Content.Filename) ? Content.Filename : (!String.IsNullOrEmpty(media.Filename) ? media.Filename : "");
                if (Permalink.IsNew && String.IsNullOrEmpty(Permalink.Name))
                {
                    Permalink.Name = Permalink.Generate(!Content.IsFolder ? filename : Content.Name, Models.Permalink.PermalinkType.MEDIA);
                }
                try {
                    Permalink.Save();
                } catch (DuplicatePermalinkException) {
                    if (Permalink.IsNew)
                    {
                        Permalink.Name = Content.Id + Permalink.Name.Substring(Permalink.Name.LastIndexOf('.'));
                        Permalink.Save();
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            else
            {
                Content.PermalinkId = Guid.Empty;
            }

            if (draft)
            {
                saved = Content.Save(media);
            }
            else
            {
                saved = Content.SaveAndPublish(media);
            }

            if (saved)
            {
                // Save related information
                Relation.DeleteByDataId(Content.Id);
                List <Relation> relations = new List <Relation>();
                ContentCategories.ForEach(c => relations.Add(new Relation()
                {
                    DataId = Content.Id, RelatedId = c, IsDraft = false, Type = Relation.RelationType.CONTENTCATEGORY
                })
                                          );
                relations.ForEach(r => r.Save());

                // Save extensions
                foreach (var ext in Extensions)
                {
                    // Call OnSave
                    ext.Body.OnManagerSave(Content);

                    ext.ParentId = Content.Id;
                    ext.Save();
                    if (!draft)
                    {
                        if (Extension.GetScalar("SELECT COUNT(extension_id) FROM extension WHERE extension_id=@0 AND extension_draft=0", ext.Id) == 0)
                        {
                            ext.IsNew = true;
                        }
                        ext.IsDraft = false;
                        ext.Save();
                    }
                }
                // Reset file url
                FileUrl = "";

                return(true);
            }
            return(false);
        }