Ejemplo n.º 1
0
        public ActionResult Create(Create model)
        {
            if (string.IsNullOrWhiteSpace(model.NameOriginal) && string.IsNullOrWhiteSpace(model.NameRomaji) && string.IsNullOrWhiteSpace(model.NameEnglish))
            {
                ModelState.AddModelError("Names", ViewRes.EntryCreateStrings.NeedName);
            }

            if (string.IsNullOrWhiteSpace(model.Description) && string.IsNullOrWhiteSpace(model.WebLinkUrl))
            {
                ModelState.AddModelError("Description", ViewRes.Artist.CreateStrings.NeedWebLinkOrDescription);
            }

            var coverPicUpload = Request.Files["pictureUpload"];
            PictureDataContract pictureData = ParseMainPicture(coverPicUpload, "Picture");

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var contract = model.ToContract();

            contract.PictureData = pictureData;

            var album = Service.Create(contract);

            return(RedirectToAction("Edit", new { id = album.Id }));
        }
Ejemplo n.º 2
0
        protected PictureDataContract ParseMainPicture(HttpPostedFileBase pictureUpload, string fieldName)
        {
            PictureDataContract pictureData = null;

            if (Request.Files.Count > 0 && pictureUpload != null && pictureUpload.ContentLength > 0)
            {
                if (pictureUpload.ContentLength > ImageHelper.MaxImageSizeBytes)
                {
                    ModelState.AddModelError(fieldName, "Picture file is too large.");
                }

                if (!ImageHelper.IsValidImageExtension(pictureUpload.FileName))
                {
                    ModelState.AddModelError(fieldName, "Picture format is not valid.");
                }

                if (ModelState.IsValid)
                {
                    pictureData = ImageHelper.GetOriginalAndResizedImages(
                        pictureUpload.InputStream, pictureUpload.ContentLength, pictureUpload.ContentType ?? string.Empty);
                }
            }

            return(pictureData);
        }
Ejemplo n.º 3
0
        public ActionResult Edit(AlbumEdit model)
        {
            if (!OptionalDateTime.IsValid(model.ReleaseYear, model.ReleaseDay, model.ReleaseMonth))
            {
                ModelState.AddModelError("ReleaseYear", "Invalid date");
            }

            var coverPicUpload = Request.Files["coverPicUpload"];
            PictureDataContract pictureData = ParseMainPicture(coverPicUpload, "CoverPicture");

            ParseAdditionalPictures(coverPicUpload, model.Pictures);

            if (!ModelState.IsValid)
            {
                var oldContract = Service.GetAlbumForEdit(model.Id);
                model.CopyNonEditableFields(oldContract);
                return(View(model));
            }

            var contract = model.ToContract();

            Service.UpdateBasicProperties(contract, pictureData);

            return(RedirectToAction("Details", new { id = model.Id }));
        }
Ejemplo n.º 4
0
        public PictureData(PictureDataContract contract)
        {
            ParamIs.NotNull(() => contract);

            Bytes = contract.Bytes;

            if (contract.Thumb250 != null)
            {
                Thumb250 = new PictureThumb250(contract.Thumb250);
            }
        }
Ejemplo n.º 5
0
        public static PictureDataContract GetOriginalAndResizedImages(Stream input, int length, string contentType)
        {
            var buf = new Byte[length];

            input.Read(buf, 0, length);

            var contract = new PictureDataContract(buf, contentType);
            var thumbs   = GenerateThumbs(input, new[] { 250 });
            var thumb250 = thumbs.FirstOrDefault(t => t.Size == 250);

            contract.Thumb250 = thumb250;

            return(contract);
        }
Ejemplo n.º 6
0
        public MikuDbAlbumContract GetAlbumData(HtmlDocument doc, string url)
        {
            var data = new ImportedAlbumDataContract();

            var titleElem = doc.DocumentNode.SelectSingleNode("//div[@class = 'pgtitle_in']/h1/span");

            if (titleElem != null)
            {
                data.Title = HtmlEntity.DeEntitize(titleElem.InnerText);
            }

            var mainPanel = doc.GetElementbyId("main_ref");

            if (mainPanel != null)
            {
                var descBox = mainPanel.SelectSingleNode("p[@class = 'overview']");

                if (descBox != null)
                {
                    data.Description = descBox.InnerText;
                }

                var infoBox = mainPanel.SelectSingleNode("div[1]");

                if (infoBox != null)
                {
                    ParseInfoBox(data, infoBox);
                }

                var tracklistElem = mainPanel.SelectSingleNode("div[@class = 'songlistbox']");

                if (tracklistElem != null)
                {
                    ParseTracklist(data, tracklistElem);
                }
            }

            var coverElem = doc.DocumentNode.SelectSingleNode("//div[@id = 'sub_ref']/div[@class = 'artwork']/div/a/img");
            PictureDataContract coverPic = null;

            if (coverElem != null)
            {
                coverPic = DownloadCoverPicture("https://karent.jp" + coverElem.Attributes["src"].Value);
            }

            return(new MikuDbAlbumContract(data)
            {
                CoverPicture = coverPic, SourceUrl = url
            });
        }
Ejemplo n.º 7
0
        public ActionResult EditBasicDetails(ArtistEdit model, IEnumerable <GroupForArtistContract> groups)
        {
            var coverPicUpload = Request.Files["pictureUpload"];
            PictureDataContract pictureData = ParseMainPicture(coverPicUpload, "Picture");

            ParseAdditionalPictures(coverPicUpload, model.Pictures);

            if (!ModelState.IsValid)
            {
                SaveErrorsToTempData();
                return(RedirectToAction("Edit", new { id = model.Id }));
            }

            Service.UpdateBasicProperties(model.ToContract(), pictureData, LoginManager);

            return(RedirectToAction("Details", new { id = model.Id }));
        }
Ejemplo n.º 8
0
        private MikuDbAlbumContract GetAlbumData(HtmlDocument doc, string url)
        {
            var data = new ImportedAlbumDataContract();

            string title     = string.Empty;
            var    titleElem = doc.DocumentNode.SelectSingleNode(".//h2[@class='posttitle']/a");

            if (titleElem != null)
            {
                title = HtmlEntity.DeEntitize(titleElem.InnerText);
            }

            var coverPicLink = doc.DocumentNode.SelectSingleNode(".//div[@class='postcontent']/table/tr[1]/td[1]/a/img");
            PictureDataContract coverPicture = null;

            if (coverPicLink != null)
            {
                var address = coverPicLink.Attributes["src"].Value;

                coverPicture = DownloadCoverPicture(address);
            }

            var infoBox = doc.DocumentNode.SelectSingleNode(".//div[@class='postcontent']/table/tr[1]/td[2]");

            if (infoBox != null)
            {
                ParseInfoBox(data, infoBox);
            }

            var trackListRow = FindTracklistRow(doc, (infoBox != null ? infoBox.ParentNode.NextSibling : null));

            if (trackListRow != null)
            {
                ParseTrackList(data, trackListRow);
            }

            return(new MikuDbAlbumContract {
                Title = title, Data = data, CoverPicture = coverPicture, SourceUrl = url
            });
        }
Ejemplo n.º 9
0
        public AlbumForEditContract UpdateBasicProperties(AlbumForEditContract properties, PictureDataContract pictureData)
        {
            ParamIs.NotNull(() => properties);

            return(HandleQuery(session => {
                using (var tx = session.BeginTransaction()) {
                    var album = session.Load <Album>(properties.Id);

                    VerifyEntryEdit(album);

                    var diff = new AlbumDiff(DoSnapshot(album.ArchivedVersionsManager.GetLatestVersion(), GetLoggedUser(session)));

                    SysLog(string.Format("updating properties for {0}", album));

                    if (album.DiscType != properties.DiscType)
                    {
                        album.DiscType = properties.DiscType;
                        album.UpdateArtistString();
                        diff.DiscType = true;
                    }

                    if (album.Description != properties.Description)
                    {
                        album.Description = properties.Description;
                        diff.Description = true;
                    }

                    if (album.TranslatedName.DefaultLanguage != properties.TranslatedName.DefaultLanguage)
                    {
                        album.TranslatedName.DefaultLanguage = properties.TranslatedName.DefaultLanguage;
                        diff.OriginalName = true;
                    }

                    var validNames = properties.Names.AllNames;
                    var nameDiff = album.Names.Sync(validNames, album);
                    SessionHelper.Sync(session, nameDiff);

                    album.Names.UpdateSortNames();

                    if (nameDiff.Changed)
                    {
                        diff.Names = true;
                    }

                    var validWebLinks = properties.WebLinks.Where(w => !string.IsNullOrEmpty(w.Url));
                    var webLinkDiff = WebLink.Sync(album.WebLinks, validWebLinks, album);
                    SessionHelper.Sync(session, webLinkDiff);

                    if (webLinkDiff.Changed)
                    {
                        diff.WebLinks = true;
                    }

                    var newOriginalRelease = (properties.OriginalRelease != null ? new AlbumRelease(properties.OriginalRelease) : new AlbumRelease());

                    if (album.OriginalRelease == null)
                    {
                        album.OriginalRelease = new AlbumRelease();
                    }

                    if (!album.OriginalRelease.Equals(newOriginalRelease))
                    {
                        album.OriginalRelease = newOriginalRelease;
                        diff.OriginalRelease = true;
                    }

                    NHibernateUtil.Initialize(album.CoverPictureData);
                    if (pictureData != null)
                    {
                        album.CoverPictureData = new PictureData(pictureData);
                        diff.Cover = true;
                    }

                    if (album.Status != properties.Status)
                    {
                        album.Status = properties.Status;
                        diff.Status = true;
                    }

                    var songGetter = new Func <SongInAlbumEditContract, Song>(contract => {
                        if (contract.SongId != 0)
                        {
                            return session.Load <Song>(contract.SongId);
                        }
                        else
                        {
                            SysLog(string.Format("creating a new song '{0}' to {1}", contract.SongName, album));

                            var song = new Song(new LocalizedString(contract.SongName, ContentLanguageSelection.Unspecified));
                            session.Save(song);

                            Services.Songs.Archive(session, song, SongArchiveReason.Created,
                                                   string.Format("Created for album '{0}'", album.DefaultName));

                            AuditLog(string.Format("created {0} for {1}",
                                                   EntryLinkFactory.CreateEntryLink(song), EntryLinkFactory.CreateEntryLink(album)), session);
                            AddEntryEditedEntry(session, song, EntryEditEvent.Created);

                            return song;
                        }
                    });

                    var tracksDiff = album.SyncSongs(properties.Songs, songGetter);

                    SessionHelper.Sync(session, tracksDiff);

                    if (tracksDiff.Changed)
                    {
                        var add = string.Join(", ", tracksDiff.Added.Select(i => i.Song.ToString()));
                        var rem = string.Join(", ", tracksDiff.Removed.Select(i => i.Song.ToString()));
                        var edit = string.Join(", ", tracksDiff.Edited.Select(i => i.Song.ToString()));

                        var str = string.Format("edited tracks (added: {0}, removed: {1}, reordered: {2})", add, rem, edit)
                                  .Truncate(300);

                        AuditLog(str, session);

                        diff.Tracks = true;
                    }

                    var picsDiff = album.Pictures.SyncPictures(properties.Pictures, GetLoggedUser(session), album.CreatePicture);
                    SessionHelper.Sync(session, picsDiff);
                    ImageHelper.GenerateThumbsAndMoveImages(picsDiff.Added);

                    if (picsDiff.Changed)
                    {
                        diff.Pictures = true;
                    }

                    var pvDiff = album.SyncPVs(properties.PVs);
                    SessionHelper.Sync(session, pvDiff);

                    if (pvDiff.Changed)
                    {
                        diff.PVs = true;
                    }

                    var logStr = string.Format("updated properties for {0} ({1})",
                                               EntryLinkFactory.CreateEntryLink(album), diff.ChangedFieldsString)
                                 + (properties.UpdateNotes != string.Empty ? " " + properties.UpdateNotes : string.Empty)
                                 .Truncate(400);

                    AuditLog(logStr, session);

                    AddEntryEditedEntry(session, album, EntryEditEvent.Updated);

                    Archive(session, album, diff, AlbumArchiveReason.PropertiesUpdated, properties.UpdateNotes);
                    session.Update(album);
                    tx.Commit();
                    return new AlbumForEditContract(album, PermissionContext.LanguagePreference);
                }
            }));
        }
Ejemplo n.º 10
0
        public PictureData(PictureDataContract contract)
        {
            ParamIs.NotNull(() => contract);

            Bytes = contract.Bytes;
        }
Ejemplo n.º 11
0
        public void UpdateBasicProperties(ArtistForEditContract properties, PictureDataContract pictureData, IUserPermissionContext permissionContext)
        {
            ParamIs.NotNull(() => properties);
            ParamIs.NotNull(() => permissionContext);

            HandleTransaction(session => {
                var artist = session.Load <Artist>(properties.Id);

                VerifyEntryEdit(artist);

                var diff = new ArtistDiff(DoSnapshot(artist.GetLatestVersion(), GetLoggedUser(session)));

                SysLog(string.Format("updating properties for {0}", artist));

                if (artist.ArtistType != properties.ArtistType)
                {
                    artist.ArtistType = properties.ArtistType;
                    diff.ArtistType   = true;
                }

                if (artist.Description != properties.Description)
                {
                    artist.Description = properties.Description;
                    diff.Description   = true;
                }

                if (artist.TranslatedName.DefaultLanguage != properties.TranslatedName.DefaultLanguage)
                {
                    artist.TranslatedName.DefaultLanguage = properties.TranslatedName.DefaultLanguage;
                    diff.OriginalName = true;
                }

                NHibernateUtil.Initialize(artist.Picture);
                if (pictureData != null)
                {
                    artist.Picture = new PictureData(pictureData);
                    diff.Picture   = true;
                }

                if (artist.Status != properties.Status)
                {
                    artist.Status = properties.Status;
                    diff.Status   = true;
                }

                var nameDiff = artist.Names.Sync(properties.Names.AllNames, artist);
                SessionHelper.Sync(session, nameDiff);

                if (nameDiff.Changed)
                {
                    diff.Names = true;
                }

                var validWebLinks = properties.WebLinks.Where(w => !string.IsNullOrEmpty(w.Url));
                var webLinkDiff   = WebLink.Sync(artist.WebLinks, validWebLinks, artist);
                SessionHelper.Sync(session, webLinkDiff);

                if (webLinkDiff.Changed)
                {
                    diff.WebLinks = true;
                }

                if (diff.ArtistType || diff.Names)
                {
                    foreach (var song in artist.Songs)
                    {
                        song.Song.UpdateArtistString();
                        session.Update(song);
                    }
                }

                var groupsDiff = CollectionHelper.Diff(artist.Groups, properties.Groups, (i, i2) => (i.Id == i2.Id));

                foreach (var grp in groupsDiff.Removed)
                {
                    grp.Delete();
                    session.Delete(grp);
                }

                foreach (var grp in groupsDiff.Added)
                {
                    var link = artist.AddGroup(session.Load <Artist>(grp.Group.Id));
                    session.Save(link);
                }

                if (groupsDiff.Changed)
                {
                    diff.Groups = true;
                }

                var picsDiff = artist.Pictures.SyncPictures(properties.Pictures, GetLoggedUser(session), artist.CreatePicture);
                SessionHelper.Sync(session, picsDiff);
                ImageHelper.GenerateThumbsAndMoveImages(picsDiff.Added);

                if (picsDiff.Changed)
                {
                    diff.Pictures = true;
                }

                /*
                 * var albumGetter = new Func<AlbumForArtistEditContract, Album>(contract => {
                 *
                 *      Album album;
                 *
                 *      if (contract.AlbumId != 0) {
                 *
                 *              album = session.Load<Album>(contract.AlbumId);
                 *
                 *      } else {
                 *
                 *              AuditLog(string.Format("creating a new album '{0}' to {1}", contract.AlbumName, artist));
                 *
                 *              album = new Album(contract.AlbumName);
                 *              session.Save(album);
                 *
                 *              Services.Albums.Archive(session, album, AlbumArchiveReason.Created,
                 *                      string.Format("Created for artist '{0}'", artist.DefaultName));
                 *
                 *              AuditLog(string.Format("created {0} for {1}",
                 *                      EntryLinkFactory.CreateEntryLink(album), EntryLinkFactory.CreateEntryLink(artist)), session);
                 *              AddEntryEditedEntry(session, album, EntryEditEvent.Created);
                 *
                 *      }
                 *
                 *      return album;
                 *
                 * });
                 *
                 * if (properties.AlbumLinks != null
                 *      && !properties.TooManyAlbums
                 *      && (properties.AlbumLinks.Any() || artist.Albums.Count() < ArtistForEditContract.MaxAlbums / 2)
                 *      && artist.Albums.Count() <= ArtistForEditContract.MaxAlbums) {
                 *
                 *      var albumDiff = artist.SyncAlbums(properties.AlbumLinks, albumGetter);
                 *
                 *      SessionHelper.Sync(session, albumDiff);
                 *
                 *      if (albumDiff.Changed) {
                 *
                 *              diff.Albums = true;
                 *
                 *              var add = string.Join(", ", albumDiff.Added.Select(i => i.Album.ToString()));
                 *              var rem = string.Join(", ", albumDiff.Removed.Select(i => i.Album.ToString()));
                 *
                 *              var str = string.Format("edited albums (added: {0}, removed: {1})", add, rem)
                 *                      .Truncate(300);
                 *
                 *              AuditLog(str, session);
                 *
                 *      }
                 *
                 * }*/

                var logStr = string.Format("updated properties for {0} ({1})", EntryLinkFactory.CreateEntryLink(artist), diff.ChangedFieldsString)
                             + (properties.UpdateNotes != string.Empty ? " " + properties.UpdateNotes : string.Empty)
                             .Truncate(400);

                AuditLog(logStr, session);
                AddEntryEditedEntry(session, artist, EntryEditEvent.Updated);

                Archive(session, artist, diff, ArtistArchiveReason.PropertiesUpdated, properties.UpdateNotes);
                session.Update(artist);

                return(true);
            });
        }