public ReleaseEventSeriesForApiContract(ReleaseEventSeries series, ContentLanguagePreference languagePreference, ReleaseEventSeriesOptionalFields fields, IAggregatedEntryImageUrlFactory thumbPersister)
        {
            Category = series.Category;
            Id       = series.Id;
            Name     = series.TranslatedName[languagePreference];
            Status   = series.Status;
            UrlSlug  = series.UrlSlug;
            Version  = series.Version;

            if (fields.HasFlag(ReleaseEventSeriesOptionalFields.AdditionalNames))
            {
                AdditionalNames = series.Names.GetAdditionalNamesStringForLanguage(languagePreference);
            }

            if (fields.HasFlag(ReleaseEventSeriesOptionalFields.Description))
            {
                Description = series.Description;
            }

            if (fields.HasFlag(ReleaseEventSeriesOptionalFields.Events))
            {
                Events = series.Events.Select(e => new ReleaseEventForApiContract(e, languagePreference, ReleaseEventOptionalFields.None, thumbPersister)).ToArray();
            }

            if (thumbPersister != null && fields.HasFlag(ReleaseEventSeriesOptionalFields.MainPicture))
            {
                MainPicture = EntryThumbForApiContract.Create(EntryThumb.Create(series), thumbPersister);
            }

            if (fields.HasFlag(ReleaseEventSeriesOptionalFields.Names))
            {
                Names = series.Names.Select(n => new LocalizedStringContract(n)).ToArray();
            }

            if (fields.HasFlag(ReleaseEventSeriesOptionalFields.WebLinks))
            {
                WebLinks = series.WebLinks.Select(w => new WebLinkForApiContract(w)).ToArray();
            }
        }
Esempio n. 2
0
        public TagBaseContract Update(TagForEditContract contract, UploadedFileContract uploadedImage)
        {
            ParamIs.NotNull(() => contract);

            PermissionContext.VerifyPermission(PermissionToken.EditTags);

            return(repository.HandleTransaction(ctx => {
                var tag = LoadTagById(ctx, contract.Id);

                permissionContext.VerifyEntryEdit(tag);

                var diff = new TagDiff();

                if (tag.CategoryName != contract.CategoryName)
                {
                    diff.CategoryName.Set();
                }

                diff.Description.Set(tag.Description.CopyFrom(contract.Description));

                if (tag.HideFromSuggestions != contract.HideFromSuggestions)
                {
                    diff.HideFromSuggestions.Set();
                }

                if (tag.Targets != contract.Targets)
                {
                    diff.Targets.Set();
                }

                if (tag.TranslatedName.DefaultLanguage != contract.DefaultNameLanguage)
                {
                    tag.TranslatedName.DefaultLanguage = contract.DefaultNameLanguage;
                    diff.OriginalName.Set();
                }

                var nameDiff = SyncNames(ctx.OfType <TagName>(), tag, contract.Names);

                if (nameDiff.Changed)
                {
                    diff.Names.Set();
                }

                if (!Tag.Equals(tag.Parent, contract.Parent))
                {
                    var newParent = GetRealTag(ctx, contract.Parent, tag);

                    if (!Equals(newParent, tag.Parent))
                    {
                        diff.Parent.Set();
                        tag.SetParent(newParent);
                    }
                }

                var relatedTagsDiff = tag.SyncRelatedTags(contract.RelatedTags, tagId => ctx.Load(tagId));
                ctx.Sync(relatedTagsDiff);
                diff.RelatedTags.Set(relatedTagsDiff.Changed);

                var webLinkDiff = tag.WebLinks.Sync(contract.WebLinks, tag);
                ctx.OfType <TagWebLink>().Sync(webLinkDiff);

                if (webLinkDiff.Changed)
                {
                    diff.WebLinks.Set();
                }

                if (tag.Status != contract.Status)
                {
                    diff.Status.Set();
                }

                tag.CategoryName = contract.CategoryName;
                tag.HideFromSuggestions = contract.HideFromSuggestions;
                tag.Status = contract.Status;
                tag.Targets = contract.Targets;

                if (uploadedImage != null)
                {
                    diff.Picture.Set();

                    var thumb = new EntryThumb(tag, uploadedImage.Mime);
                    tag.Thumb = thumb;
                    var thumbGenerator = new ImageThumbGenerator(imagePersister);
                    thumbGenerator.GenerateThumbsAndMoveImage(uploadedImage.Stream, thumb, Tag.ImageSizes, originalSize: Constants.RestrictedImageOriginalSize);
                }

                var logStr = string.Format("updated properties for tag {0} ({1})", entryLinkFactory.CreateEntryLink(tag), diff.ChangedFieldsString);
                ctx.AuditLogger.AuditLog(logStr);

                var archived = Archive(ctx, tag, diff, EntryEditEvent.Updated, contract.UpdateNotes);
                AddEntryEditedEntry(ctx.OfType <ActivityEntry>(), tag, EntryEditEvent.Updated, archived);

                ctx.Update(tag);

                return new TagBaseContract(tag, LanguagePreference);
            }));
        }
Esempio n. 3
0
        public EntryRevertedContract RevertToVersion(int archivedAlbumVersionId)
        {
            PermissionContext.VerifyPermission(PermissionToken.RestoreRevisions);

            return(HandleTransaction(session => {
                var archivedVersion = session.Load <ArchivedAlbumVersion>(archivedAlbumVersionId);
                var album = archivedVersion.Album;

                session.AuditLogger.SysLog("reverting " + album + " to version " + archivedVersion.Version);

                var fullProperties = ArchivedAlbumContract.GetAllProperties(archivedVersion);
                var warnings = new List <string>();
                var diff = new AlbumDiff();

                album.Description.Original = fullProperties.Description;
                album.Description.English = fullProperties.DescriptionEng ?? string.Empty;
                album.DiscType = fullProperties.DiscType;
                album.TranslatedName.DefaultLanguage = fullProperties.TranslatedName.DefaultLanguage;

                // Picture
                var versionWithPic = archivedVersion.GetLatestVersionWithField(AlbumEditableFields.Cover);

                if (versionWithPic != null)
                {
                    album.CoverPictureData = versionWithPic.CoverPicture;
                    album.CoverPictureMime = versionWithPic.CoverPictureMime;

                    if (versionWithPic.CoverPicture != null)
                    {
                        var thumbGenerator = new ImageThumbGenerator(imagePersister);
                        using (var stream = new MemoryStream(versionWithPic.CoverPicture.Bytes)) {
                            var thumb = new EntryThumb(album, versionWithPic.CoverPictureMime);
                            thumbGenerator.GenerateThumbsAndMoveImage(stream, thumb, ImageSizes.Thumb | ImageSizes.SmallThumb | ImageSizes.TinyThumb);
                        }
                    }
                }
                else
                {
                    album.CoverPictureData = null;
                    album.CoverPictureMime = null;
                }

                // Assume picture was changed if there's a version between the current version and the restored version where the picture was changed.
                diff.Cover.Set(!Equals(album.ArchivedVersionsManager.GetLatestVersionWithField(AlbumEditableFields.Cover, album.Version), versionWithPic));

                // Original release
                album.OriginalRelease = (fullProperties.OriginalRelease != null ? new AlbumRelease(fullProperties.OriginalRelease, session.NullSafeLoad <ReleaseEvent>(fullProperties.OriginalRelease.ReleaseEvent)) : null);

                // Artists
                DatabaseContextHelper.RestoreObjectRefs <ArtistForAlbum, Artist, ArchivedArtistForAlbumContract>(
                    session.OfType <Artist>(), warnings, album.AllArtists, fullProperties.Artists,
                    (a1, a2) => (a1.Artist != null && a1.Artist.Id == a2.Id) || (a1.Artist == null && a2.Id == 0 && a1.Name == a2.NameHint),
                    (artist, albumRef) => RestoreArtistRef(album, artist, albumRef),
                    albumForArtist => albumForArtist.Delete());

                // Songs
                DatabaseContextHelper.RestoreObjectRefs <SongInAlbum, Song, SongInAlbumRefContract>(
                    session.OfType <Song>(), warnings, album.AllSongs, fullProperties.Songs,
                    (a1, a2) => ((a1.Song != null && a1.Song.Id == a2.Id) || a1.Song == null && a2.Id == 0 && a1.Name == a2.NameHint),
                    (song, songRef) => RestoreTrackRef(album, song, songRef),
                    songInAlbum => songInAlbum.Delete());

                // Names
                if (fullProperties.Names != null)
                {
                    var nameDiff = album.Names.SyncByContent(fullProperties.Names, album);
                    session.Sync(nameDiff);
                }

                // Weblinks
                if (fullProperties.WebLinks != null)
                {
                    var webLinkDiff = WebLink.SyncByValue(album.WebLinks, fullProperties.WebLinks, album);
                    session.Sync(webLinkDiff);
                }

                // PVs
                if (fullProperties.PVs != null)
                {
                    var pvDiff = CollectionHelper.Diff(album.PVs, fullProperties.PVs, (p1, p2) => (p1.PVId == p2.PVId && p1.Service == p2.Service));

                    foreach (var pv in pvDiff.Added)
                    {
                        session.Save(album.CreatePV(new PVContract(pv)));
                    }

                    foreach (var pv in pvDiff.Removed)
                    {
                        pv.OnDelete();
                        session.Delete(pv);
                    }
                }

                album.UpdateArtistString();
                album.UpdateRatingTotals();

                Archive(session, album, diff, AlbumArchiveReason.Reverted, string.Format("Reverted to version {0}", archivedVersion.Version));
                AuditLog(string.Format("reverted {0} to revision {1}", EntryLinkFactory.CreateEntryLink(album), archivedVersion.Version), session);

                return new EntryRevertedContract(album, warnings);
            }));
        }
Esempio n. 4
0
        /// <summary>
        /// Reverts an album to an earlier archived version.
        /// </summary>
        /// <param name="archivedArtistVersionId">Id of the archived version to be restored.</param>
        /// <returns>Result of the revert operation, with possible warnings if any. Cannot be null.</returns>
        /// <remarks>Requires the RestoreRevisions permission.</remarks>
        public async Task <EntryRevertedContract> RevertToVersion(int archivedArtistVersionId)
        {
            PermissionContext.VerifyPermission(PermissionToken.RestoreRevisions);

            return(await HandleTransactionAsync(async session => {
                var archivedVersion = await session.LoadAsync <ArchivedArtistVersion>(archivedArtistVersionId);

                if (archivedVersion.Hidden)
                {
                    PermissionContext.VerifyPermission(PermissionToken.ViewHiddenRevisions);
                }

                var artist = archivedVersion.Artist;

                session.AuditLogger.SysLog("reverting " + artist + " to version " + archivedVersion.Version);

                var fullProperties = ArchivedArtistContract.GetAllProperties(archivedVersion);
                var warnings = new List <string>();
                var diff = new ArtistDiff();

                artist.ArtistType = fullProperties.ArtistType;
                artist.Description.Original = fullProperties.Description;
                artist.Description.English = fullProperties.DescriptionEng ?? string.Empty;
                artist.TranslatedName.DefaultLanguage = fullProperties.TranslatedName.DefaultLanguage;
                artist.BaseVoicebank = DatabaseContextHelper.RestoreWeakRootEntityRef(session, warnings, fullProperties.BaseVoicebank);

                // Picture
                var versionWithPic = archivedVersion.GetLatestVersionWithField(ArtistEditableFields.Picture);

                if (versionWithPic != null)
                {
                    artist.Picture = versionWithPic.Picture;
                    artist.PictureMime = versionWithPic.PictureMime;

                    if (versionWithPic.Picture != null)
                    {
                        var thumbGenerator = new ImageThumbGenerator(imagePersister);
                        using (var stream = new MemoryStream(versionWithPic.Picture.Bytes)) {
                            var thumb = new EntryThumb(artist, versionWithPic.PictureMime, ImagePurpose.Main);
                            thumbGenerator.GenerateThumbsAndMoveImage(stream, thumb, ImageSizes.Thumb | ImageSizes.SmallThumb | ImageSizes.TinyThumb);
                        }
                    }
                }
                else
                {
                    artist.Picture = null;
                    artist.PictureMime = null;
                }

                // Assume picture was changed if there's a version between the current version and the restored version where the picture was changed.
                diff.Picture.Set(!Equals(artist.ArchivedVersionsManager.GetLatestVersionWithField(ArtistEditableFields.Picture, artist.Version), versionWithPic));

                // Groups
                DatabaseContextHelper.RestoreObjectRefs(
                    session, warnings, artist.AllGroups, fullProperties.Groups, (a1, a2) => (a1.Parent.Id == a2.Id),
                    (grp, grpRef) => (!artist.HasGroup(grp) ? artist.AddGroup(grp, grpRef.LinkType) : null),
                    groupForArtist => groupForArtist.Delete());

                // Names
                if (fullProperties.Names != null)
                {
                    var nameDiff = artist.Names.SyncByContent(fullProperties.Names, artist);
                    await session.SyncAsync(nameDiff);
                }

                // Weblinks
                if (fullProperties.WebLinks != null)
                {
                    var webLinkDiff = WebLink.SyncByValue(artist.WebLinks, fullProperties.WebLinks, artist);
                    await session.SyncAsync(webLinkDiff);
                }

                await ArchiveAsync(session, artist, diff, ArtistArchiveReason.Reverted, string.Format("Reverted to version {0}", archivedVersion.Version));
                await AuditLogAsync(string.Format("reverted {0} to revision {1}", entryLinkFactory.CreateEntryLink(artist), archivedVersion.Version), session);

                return new EntryRevertedContract(artist, warnings);
            }));
        }
Esempio n. 5
0
        public void Update(TagContract contract, UploadedFileContract uploadedImage)
        {
            ParamIs.NotNull(() => contract);

            PermissionContext.VerifyPermission(PermissionToken.ManageDatabase);

            repository.HandleTransaction(ctx => {
                var tag = ctx.Load(contract.Name);

                permissionContext.VerifyEntryEdit(tag);

                var diff = new TagDiff();

                var newAliasedTo = contract.AliasedTo ?? string.Empty;
                if (!Tag.Equals(tag.AliasedTo, contract.AliasedTo))
                {
                    diff.AliasedTo = true;
                    tag.AliasedTo  = GetRealTag(ctx, newAliasedTo, tag);
                }

                if (tag.CategoryName != contract.CategoryName)
                {
                    diff.CategoryName = true;
                }

                if (tag.Description != contract.Description)
                {
                    diff.Description = true;
                }

                if (!Tag.Equals(tag.Parent, contract.Parent))
                {
                    var newParent = GetRealTag(ctx, contract.Parent, tag);

                    if (!Equals(newParent, tag.Parent))
                    {
                        diff.Parent = true;
                        tag.SetParent(newParent);
                    }
                }

                if (tag.Status != contract.Status)
                {
                    diff.Status = true;
                }

                tag.CategoryName = contract.CategoryName;
                tag.Description  = contract.Description;
                tag.Status       = contract.Status;

                if (uploadedImage != null)
                {
                    diff.Picture = true;

                    var thumb          = new EntryThumb(tag, uploadedImage.Mime);
                    tag.Thumb          = thumb;
                    var thumbGenerator = new ImageThumbGenerator(imagePersister);
                    thumbGenerator.GenerateThumbsAndMoveImage(uploadedImage.Stream, thumb, ImageSizes.Original | ImageSizes.SmallThumb, originalSize: 500);
                }

                var logStr = string.Format("updated properties for tag {0} ({1})", entryLinkFactory.CreateEntryLink(tag), diff.ChangedFieldsString);
                ctx.AuditLogger.AuditLog(logStr);
                Archive(ctx, tag, diff, EntryEditEvent.Updated);

                ctx.Update(tag);
            });
        }
        public ReleaseEventForApiContract(ReleaseEvent rel, ContentLanguagePreference languagePreference, ReleaseEventOptionalFields fields, IEntryThumbPersister thumbPersister)
        {
            Category     = rel.Category;
            Date         = rel.Date;
            EndDate      = rel.EndDate;
            Id           = rel.Id;
            Name         = rel.TranslatedName[languagePreference];
            SeriesNumber = rel.SeriesNumber;
            SeriesSuffix = rel.SeriesSuffix;
            Status       = rel.Status;
            UrlSlug      = rel.UrlSlug;
            VenueName    = rel.VenueName;
            Version      = rel.Version;

            if (rel.HasSeries)
            {
                SeriesId = rel.Series.Id;
            }

            if (fields.HasFlag(ReleaseEventOptionalFields.AdditionalNames))
            {
                AdditionalNames = rel.Names.GetAdditionalNamesStringForLanguage(languagePreference);
            }

            if (fields.HasFlag(ReleaseEventOptionalFields.Artists))
            {
                Artists = rel.Artists.Select(a => new ArtistForEventContract(a, languagePreference)).ToArray();
            }

            if (fields.HasFlag(ReleaseEventOptionalFields.Description))
            {
                Description = rel.Description;
            }

            if (thumbPersister != null && fields.HasFlag(ReleaseEventOptionalFields.MainPicture))
            {
                MainPicture = EntryThumbForApiContract.Create(EntryThumb.Create(rel) ?? EntryThumb.Create(rel.Series), thumbPersister);
            }

            if (fields.HasFlag(ReleaseEventOptionalFields.Names))
            {
                Names = rel.Names.Select(n => new LocalizedStringContract(n)).ToArray();
            }

            if (fields.HasFlag(ReleaseEventOptionalFields.Series) && rel.HasSeries)
            {
                Series = new ReleaseEventSeriesContract(rel.Series, languagePreference);
            }

            if (fields.HasFlag(ReleaseEventOptionalFields.SongList) && rel.SongList != null)
            {
                SongList = new SongListBaseContract(rel.SongList);
            }

            if (fields.HasFlag(ReleaseEventOptionalFields.WebLinks))
            {
                WebLinks = rel.WebLinks.Select(w => new WebLinkForApiContract(w)).ToArray();
            }
        }
Esempio n. 7
0
        public EntryForApiContract(ReleaseEvent releaseEvent, ContentLanguagePreference languagePreference, IEntryThumbPersister thumbPersister,
                                   EntryOptionalFields includedFields)
            : this(releaseEvent, languagePreference, includedFields)
        {
            ActivityDate           = releaseEvent.Date.DateTime;
            EventCategory          = releaseEvent.InheritedCategory;
            ReleaseEventSeriesName = releaseEvent.Series?.TranslatedName[languagePreference];
            Status  = releaseEvent.Status;
            UrlSlug = releaseEvent.UrlSlug;

            if (includedFields.HasFlag(EntryOptionalFields.MainPicture))
            {
                MainPicture = EntryThumbForApiContract.Create(EntryThumb.Create(releaseEvent) ?? EntryThumb.Create(releaseEvent.Series), thumbPersister);
            }

            if (includedFields.HasFlag(EntryOptionalFields.WebLinks))
            {
                WebLinks = releaseEvent.WebLinks.Select(w => new ArchivedWebLinkContract(w)).ToArray();
            }
        }
Esempio n. 8
0
        public void CreateMissingThumbs()
        {
            VerifyAdmin();

            var imagePersister = new ServerEntryThumbPersister();
            var thumbGenerator = new ImageThumbGenerator(imagePersister);
            var artistIds      = new int[0];

            HandleQuery(session => {
                artistIds = session.Query <Artist>().Where(a => !a.Deleted && a.PictureMime != null && a.PictureMime != "").Select(a => a.Id).ToArray();
            });

            for (int i = 0; i < artistIds.Length; i += 100)
            {
                var ids = artistIds.Skip(i).Take(100).ToArray();

                HandleQuery(session => {
                    var artists = session.Query <Artist>().Where(a => ids.Contains(a.Id)).ToArray();

                    foreach (var artist in artists)
                    {
                        var data = new EntryThumb(artist, artist.PictureMime, ImagePurpose.Main);

                        if (artist.Picture.Bytes == null || imagePersister.HasImage(data, ImageSize.Thumb))
                        {
                            continue;
                        }

                        using (var stream = new MemoryStream(artist.Picture.Bytes)) {
                            thumbGenerator.GenerateThumbsAndMoveImage(stream, data, ImageSizes.Thumb | ImageSizes.SmallThumb | ImageSizes.TinyThumb);
                        }
                    }
                });
            }

            /*var albumIds = new int[0];
             *
             * HandleQuery(session => {
             *      albumIds = session.Query<Album>().Where(a => !a.Deleted && a.CoverPictureData.Mime != null && a.CoverPictureData.Mime != "").Select(a => a.Id).ToArray();
             * });
             *
             * for (int i = 0; i < albumIds.Length; i += 100) {
             *
             *      var ids = albumIds.Skip(i).Take(100).ToArray();
             *
             *      HandleQuery(session => {
             *
             *              var albums = session.Query<Album>().Where(a => ids.Contains(a.Id)).ToArray();
             *
             *              foreach (var album in albums) {
             *
             *                      var data = new EntryThumb(album, album.CoverPictureData.Mime);
             *
             *                      if (album.CoverPictureData.Bytes == null || imagePersister.HasImage(data, ImageSize.Thumb))
             *                              continue;
             *
             *                      using (var stream = new MemoryStream(album.CoverPictureData.Bytes)) {
             *                              thumbGenerator.GenerateThumbsAndMoveImage(stream, data, ImageSizes.Thumb | ImageSizes.SmallThumb | ImageSizes.TinyThumb);
             *                      }
             *
             *              }
             *
             *      });
             *
             * }*/

            HandleQuery(session => {
                /*var artistPic = session.Query<ArtistPictureFile>().ToArray();
                 *
                 * foreach (var pic in artistPic) {
                 *
                 *      var thumbFile = ImageHelper.GetImagePathSmallThumb(pic);
                 *      var origPath = ImageHelper.GetImagePath(pic);
                 *
                 *      if (File.Exists(origPath) && !File.Exists(thumbFile)) {
                 *
                 *              using (var original = Image.FromFile(ImageHelper.GetImagePath(pic))) {
                 *
                 *                      if (original.Width > ImageHelper.DefaultSmallThumbSize || original.Height > ImageHelper.DefaultSmallThumbSize) {
                 *                              using (var thumb = ImageHelper.ResizeToFixedSize(original, ImageHelper.DefaultSmallThumbSize, ImageHelper.DefaultSmallThumbSize)) {
                 *                                      thumb.Save(thumbFile);
                 *                              }
                 *                      } else {
                 *                              File.Copy(origPath, thumbFile);
                 *                      }
                 *
                 *              }
                 *
                 *      }
                 *
                 * }
                 *
                 * var albumPic = session.Query<AlbumPictureFile>().ToArray();
                 *
                 * foreach (var pic in albumPic) {
                 *
                 *      var thumbFile = ImageHelper.GetImagePathSmallThumb(pic);
                 *      var origPath = ImageHelper.GetImagePath(pic);
                 *
                 *      if (File.Exists(origPath) && !File.Exists(thumbFile)) {
                 *
                 *              using (var original = Image.FromFile(ImageHelper.GetImagePath(pic))) {
                 *
                 *                      if (original.Width > ImageHelper.DefaultSmallThumbSize || original.Height > ImageHelper.DefaultSmallThumbSize) {
                 *                              using (var thumb = ImageHelper.ResizeToFixedSize(original, ImageHelper.DefaultSmallThumbSize, ImageHelper.DefaultSmallThumbSize)) {
                 *                                      thumb.Save(thumbFile);
                 *                              }
                 *                      } else {
                 *                              File.Copy(origPath, thumbFile);
                 *                      }
                 *
                 *              }
                 *
                 *      }
                 *
                 * }*/
            });
        }