private void HandleGetCCImage(ApiRequest request)
 {
     lock (request)
     {
         try
         {
             Image licenseImage;
             var   token = request.Parameters.GetValues("token");
             if (token != null)
             {
                 licenseImage = CreativeCommonsLicense.FromToken(token[0]).GetImage();
             }
             else
             {
                 licenseImage = request.CurrentBook.GetLicenseMetadata().License.GetImage();
             }
             // ReplyWithImage uses the extension to determine the content type
             using (TempFile tempFile = TempFile.WithExtension(".png"))
             {
                 licenseImage.Save(tempFile.Path);
                 request.ReplyWithImage(tempFile.Path);
             }
             licenseImage.Dispose();
         }
         catch (Exception e)
         {
             Logger.WriteError("Unable to get cc license image for dialog", e);
             request.Failed();
         }
     }
 }
Пример #2
0
        private static Metadata GetMetadataWithDefaultCopyrightAndLicense(string brandingNameOrPath)
        {
            var metadata = new Metadata();

            Logger.WriteEvent("For BL-3166 Investigation: GetMetadata() setting to default license");
            metadata.License = new CreativeCommonsLicense(true, true, CreativeCommonsLicense.DerivativeRules.Derivatives);

            //OK, that's all we need, the rest is blank. That is, unless we are we are working with a brand
            //that has declared some defaults in a settings.json file:
            var settings = BrandingApi.GetSettings(brandingNameOrPath);

            if (settings != null)
            {
                if (!string.IsNullOrEmpty(settings.CopyrightNotice))
                {
                    metadata.CopyrightNotice = settings.CopyrightNotice;
                }
                if (!string.IsNullOrEmpty(settings.LicenseUrl))
                {
                    metadata.License = CreativeCommonsLicense.FromLicenseUrl(settings.LicenseUrl);
                }
                if (!string.IsNullOrEmpty(settings.LicenseUrl))
                {
                    metadata.License.RightsStatement = settings.LicenseRightsStatement;
                }
            }
            return(metadata);
        }
Пример #3
0
        public void TypicalEmbedInJson()
        {
            // example
            var license = new CreativeCommonsLicense(true, false, CreativeCommonsLicense.DerivativeRules.Derivatives);

            license.RightsStatement = "Please acknowledge using 'Based on work of SIL International'";

            var json = "{\"license\":'" + license.Token + "\",\"rights\":\"" + license.RightsStatement + "\"}";
            // store json somewhere.

            // parse json and get
            var token  = "custom";            // from license field
            var rights = "Academic Institutions may use this free of charge";

            // reconstitute the license
            var recoveredLicense = LicenseInfo.FromToken(token);

            license.RightsStatement = rights;

            Assert.That(recoveredLicense, Is.InstanceOf <CustomLicense>());

            Assert.That(LicenseInfo.FromToken("ask"), Is.InstanceOf <NullLicense>());
            var ccLicense = LicenseInfo.FromToken("by-nc-sa");

            Assert.That(ccLicense, Is.InstanceOf <CreativeCommonsLicense>());
            Assert.That(((CreativeCommonsLicense)ccLicense).AttributionRequired, Is.True);
        }
        public void Url_GivesVersion()
        {
            var original = new CreativeCommonsLicense(true, true, CreativeCommonsLicense.DerivativeRules.Derivatives);

            original.Version = "2.2";
            Assert.AreEqual("http://creativecommons.org/licenses/by/2.2/", original.Url);
        }
        public void ChangeAttributionRequired_HasChanges_True()
        {
            var l = new CreativeCommonsLicense(true, true, CreativeCommonsLicense.DerivativeRules.DerivativesWithShareAndShareAlike);

            l.HasChanges          = false;
            l.AttributionRequired = !l.AttributionRequired;
            Assert.IsTrue(l.HasChanges);
        }
        public void ChangeVersion_HasChanges_True()
        {
            var l = new CreativeCommonsLicense(true, true, CreativeCommonsLicense.DerivativeRules.DerivativesWithShareAndShareAlike);

            l.HasChanges = false;
            l.Version    = "3.23";
            Assert.IsTrue(l.HasChanges);
        }
        public void FromLicenseUrl_IGO_IGORead()
        {
            var license = CreativeCommonsLicense.FromLicenseUrl("http://creativecommons.org/licenses/by/3.0/");

            Assert.IsFalse(license.IntergovernmentalOriganizationQualifier);
            license = CreativeCommonsLicense.FromLicenseUrl("http://creativecommons.org/licenses/by/3.0/IGO");
            Assert.IsTrue(license.IntergovernmentalOriganizationQualifier);
        }
        public void ChangeCommercialUseAllowed_HasChanges_True()
        {
            var l = new CreativeCommonsLicense(true, true, CreativeCommonsLicense.DerivativeRules.DerivativesWithShareAndShareAlike);

            l.HasChanges           = false;
            l.CommercialUseAllowed = !l.CommercialUseAllowed;
            Assert.IsTrue(l.HasChanges);
        }
        public void ChangeDerivativeRule_HasChanges_True()
        {
            var l = new CreativeCommonsLicense(true, true, CreativeCommonsLicense.DerivativeRules.DerivativesWithShareAndShareAlike);

            l.HasChanges     = false;
            l.DerivativeRule = CreativeCommonsLicense.DerivativeRules.NoDerivatives;
            Assert.IsTrue(l.HasChanges);
        }
        public void Url_QualifierIsIGO_UrlHasIgo()
        {
            var original = new CreativeCommonsLicense(true, true, CreativeCommonsLicense.DerivativeRules.Derivatives);

            original.Version = "3.0";
            original.IntergovernmentalOriganizationQualifier = true;
            Assert.AreEqual("http://creativecommons.org/licenses/by/3.0/igo/", original.Url);
        }
        public void GetDescription_NoTranslation_GivesEnglish()
        {
            var    l = new CreativeCommonsLicense(true, true, CreativeCommonsLicense.DerivativeRules.DerivativesWithShareAndShareAlike);
            string languageUsed;

            Assert.AreEqual(l.GetDescription(new[] { "en" }, out languageUsed), l.GetDescription(new[] { "qx", "en" }, out languageUsed));
            Assert.AreEqual("en", languageUsed);
        }
        public void SetIGO_VersionNumerIsAppropriate()
        {
            var l = new CreativeCommonsLicense(true, true, CreativeCommonsLicense.DerivativeRules.Derivatives)
            {
                IntergovernmentalOriganizationQualifier = true
            };

            Assert.AreEqual("3.0", l.Version, "The igo version of CC did not have a version beyond 3.0 as of Nov 2016");
        }
        public void RoundTrip_BY_SA()
        {
            var original = new CreativeCommonsLicense(true, true, CreativeCommonsLicense.DerivativeRules.DerivativesWithShareAndShareAlike);
            var copy     = CreativeCommonsLicense.FromLicenseUrl(original.Url);

            Assert.AreEqual(copy.AttributionRequired, true);
            Assert.AreEqual(copy.CommercialUseAllowed, true);
            Assert.AreEqual(copy.DerivativeRule, CreativeCommonsLicense.DerivativeRules.DerivativesWithShareAndShareAlike);
        }
        public void Url_CC0_Correct()
        {
            // CC0 is essentially just "do what you want". So we don't have a particular property for it, we
            // just set all three aspects to be permissive.
            var original = new CreativeCommonsLicense(false, true, CreativeCommonsLicense.DerivativeRules.Derivatives, "1.0");

            //notice that the url for cc0 does not follow the conventions of the other licenses.
            Assert.AreEqual(CreativeCommonsLicense.CC0Url, original.Url);
        }
        public void RoundTrip_BY_IGO()
        {
            var original = new CreativeCommonsLicense(true, true, CreativeCommonsLicense.DerivativeRules.DerivativesWithShareAndShareAlike);

            original.IntergovernmentalOriganizationQualifier = true;
            var copy = CreativeCommonsLicense.FromLicenseUrl(original.Url);

            Assert.IsTrue(copy.IntergovernmentalOriganizationQualifier);
        }
        public void RoundTrip_BY_NC_ND()
        {
            var original = new CreativeCommonsLicense(true, false, CreativeCommonsLicense.DerivativeRules.NoDerivatives);
            var copy     = CreativeCommonsLicense.FromLicenseUrl(original.Url);

            Assert.AreEqual(copy.AttributionRequired, true);
            Assert.AreEqual(copy.CommercialUseAllowed, false);
            Assert.AreEqual(copy.DerivativeRule, CreativeCommonsLicense.DerivativeRules.NoDerivatives);
        }
        public void HasChanges_CanToggle()
        {
            var l = new CreativeCommonsLicense(true, true, CreativeCommonsLicense.DerivativeRules.DerivativesWithShareAndShareAlike);

            l.HasChanges = false;
            Assert.IsFalse(l.HasChanges);
            l.HasChanges = true;
            Assert.IsTrue(l.HasChanges);
        }
Пример #18
0
 private dynamic GetCreativeCommonsInfo(CreativeCommonsLicense ccLicense)
 {
     return(new
     {
         token = ccLicense.Token,
         allowCommercial = ccLicense.CommercialUseAllowed ? "yes" : "no",
         allowDerivatives = GetCcDerivativeRulesAsString(ccLicense.DerivativeRule),
         intergovernmentalVersion = ccLicense.IntergovernmentalOriganizationQualifier
     });
 }
        public void ChangeIGO_HasChanges_True()
        {
            var l = new CreativeCommonsLicense(true, true, CreativeCommonsLicense.DerivativeRules.Derivatives)
            {
                HasChanges = false,
                IntergovernmentalOriganizationQualifier = true
            };

            Assert.IsTrue(l.HasChanges);
        }
Пример #20
0
        public static Metadata CreateMetadata(MultiTextBase copyright, string licenseUrl, MultiTextBase licenseNotes, BookData bookData)
        {
            var metadata = new Metadata();

            if (!copyright.Empty)
            {
                metadata.CopyrightNotice = GetBestMultiTextBaseValue(copyright, bookData);
            }

            if (string.IsNullOrWhiteSpace(licenseUrl))
            {
                //NB: we are mapping "RightsStatement" (which comes from XMP-dc:Rights) to "LicenseNotes" in the html.
                //custom licenses live in this field, so if we have notes (and no URL) it is a custom one.
                if (!licenseNotes.Empty)
                {
                    metadata.License = new CustomLicense {
                        RightsStatement = GetBestMultiTextBaseValue(licenseNotes, bookData)
                    };
                }
                else
                {
                    // The only remaining current option is a NullLicense
                    metadata.License = new NullLicense();                     //"contact the copyright owner
                }
            }
            else             // there is a licenseUrl, which means it is a CC license
            {
                try
                {
                    metadata.License = CreativeCommonsLicense.FromLicenseUrl(licenseUrl);
                }
                catch (IndexOutOfRangeException)
                {
                    // Need to handle urls which do not end with the version number.
                    // Simply set it to the default version.
                    if (!licenseUrl.EndsWith("/"))
                    {
                        licenseUrl += "/";
                    }
                    licenseUrl      += CreativeCommonsLicense.kDefaultVersion;
                    metadata.License = CreativeCommonsLicense.FromLicenseUrl(licenseUrl);
                }
                catch (Exception e)
                {
                    throw new ApplicationException("Bloom had trouble parsing this license url: '" + licenseUrl + "'. (ref BL-4108)", e);
                }
                //are there notes that go along with that?
                if (!licenseNotes.Empty)
                {
                    metadata.License.RightsStatement = GetBestMultiTextBaseValue(licenseNotes, bookData);
                }
            }
            return(metadata);
        }
        public void RoundTrip_CC0()
        {
            // CC0 is essentially just "do what you want". So we don't have a particular property for it, we
            // just set all three aspects to be permissive.
            var original = new CreativeCommonsLicense(false, true, CreativeCommonsLicense.DerivativeRules.Derivatives);
            var copy     = CreativeCommonsLicense.FromLicenseUrl(original.Url);

            Assert.IsFalse(copy.AttributionRequired);
            Assert.IsTrue(copy.CommercialUseAllowed);
            Assert.AreEqual(copy.DerivativeRule, CreativeCommonsLicense.DerivativeRules.Derivatives);
        }
Пример #22
0
        public Metadata GetLicenseMetadata()
        {
            var data = new DataSet();

            GatherDataItemsFromXElement(data, _dom.RawDom);
            var metadata = new Metadata();
            NamedMutliLingualValue d;

            if (data.TextVariables.TryGetValue("copyright", out d))
            {
                metadata.CopyrightNotice = d.TextAlternatives.GetFirstAlternative();
            }
            string licenseUrl = "";

            if (data.TextVariables.TryGetValue("licenseUrl", out d))
            {
                licenseUrl = d.TextAlternatives.GetFirstAlternative();
            }

            //Enhance: have a place for notes (amendments to license). It's already in the frontmatter, under "licenseNotes"
            if (licenseUrl == null || licenseUrl.Trim() == "")
            {
                //NB: we are mapping "RightsStatement" (which comes from XMP-dc:Rights) to "LicenseNotes" in the html.
                //custom licenses live in this field
                if (data.TextVariables.TryGetValue("licenseNotes", out d))
                {
                    string licenseNotes = d.TextAlternatives.GetFirstAlternative();

                    metadata.License = new CustomLicense {
                        RightsStatement = licenseNotes
                    };
                }
                else
                {
                    //how to detect a null license was chosen? We're using the fact that it has a description, but nothing else.
                    if (data.TextVariables.TryGetValue("licenseDescription", out d))
                    {
                        metadata.License = new NullLicense();                         //"contact the copyright owner
                    }
                    else
                    {
                        //looks like the first time. Nudge them with a nice default
                        metadata.License = new CreativeCommonsLicense(true, true,
                                                                      CreativeCommonsLicense.DerivativeRules.Derivatives);
                    }
                }
            }
            else
            {
                metadata.License = CreativeCommonsLicense.FromLicenseUrl(licenseUrl);
            }
            return(metadata);
        }
        public void UnSetIGO_IGOAlreadyUnset_DoesntAffectPreviousVersionNumber()
        {
            var l = new CreativeCommonsLicense(true, true, CreativeCommonsLicense.DerivativeRules.Derivatives)
            {
                IntergovernmentalOriganizationQualifier = false,
                Version = "3.0"
            };

            // SUT
            l.IntergovernmentalOriganizationQualifier = false;
            Assert.AreEqual("3.0", l.Version,
                            "Setting igo to false when it was already false should not change an older version.");
        }
        public void UnSetIGO_VersionNumberIsHighestDefault()
        {
            var l = new CreativeCommonsLicense(true, true, CreativeCommonsLicense.DerivativeRules.Derivatives)
            {
                IntergovernmentalOriganizationQualifier = true,
                Version = "3.0"
            };

            // SUT
            l.IntergovernmentalOriganizationQualifier = false;
            Assert.AreEqual(CreativeCommonsLicense.kDefaultVersion, l.Version,
                            "Setting igo to false when it was true should change version to the current default version.");
        }
Пример #25
0
        private Metadata GetMetadataFromJson(ApiRequest request, bool forBook)
        {
            var json     = request.RequiredPostJson();
            var settings = DynamicJson.Parse(json);

            if (forBook)
            {
                Model.CurrentBook.BookInfo.MetaData.UseOriginalCopyright = settings.derivativeInfo.useOriginalCopyright;

                if (settings.derivativeInfo.useOriginalCopyright)
                {
                    return(BookCopyrightAndLicense.GetOriginalMetadata(Model.CurrentBook.Storage.Dom, Model.CurrentBook.BookData));
                }
            }

            var metadata = new Metadata {
                Creator = settings.copyrightInfo.imageCreator
            };

            metadata.SetCopyrightNotice(settings.copyrightInfo.copyrightYear, settings.copyrightInfo.copyrightHolder);

            if (settings.licenseInfo.licenseType == "creativeCommons")
            {
                metadata.License = new CreativeCommonsLicense(
                    true,
                    settings.licenseInfo.creativeCommonsInfo.allowCommercial == "yes",
                    GetCcDerivativeRule(settings.licenseInfo.creativeCommonsInfo.allowDerivatives))
                {
                    IntergovernmentalOriganizationQualifier = settings.licenseInfo.creativeCommonsInfo.intergovernmentalVersion
                };
            }
            else if (settings.licenseInfo.licenseType == "publicDomain")
            {
                metadata.License = CreativeCommonsLicense.FromToken("cc0");
            }
            else if (settings.licenseInfo.licenseType == "contact")
            {
                metadata.License = new NullLicense();
            }
            else
            {
                metadata.License = new CustomLicense();
            }

            metadata.License.RightsStatement = settings.licenseInfo.rightsStatement;

            return(metadata);
        }
Пример #26
0
        public void MetadataSetter_WasIGO_UncheckingProducesCurrentDefaultLicense()
        {
            var m = new Metadata();

            m.CopyrightNotice = "test1";
            m.Creator         = "test2";
            var ccLicense = new CreativeCommonsLicense(true, false, CreativeCommonsLicense.DerivativeRules.DerivativesWithShareAndShareAlike);

            ccLicense.IntergovernmentalOriganizationQualifier = true;
            m.License = ccLicense;
            Assert.That(m.License.Url, Is.StringEnding("3.0/igo/"));
            // SUT
            ccLicense.IntergovernmentalOriganizationQualifier = false;
            m.License = ccLicense;
            Assert.That(m.License.Url, Is.StringEnding(CreativeCommonsLicense.kDefaultVersion + "/"));
        }
        private static bool TryFormatCreativeCommonsTextElement(CreativeCommonsLicense licenseToFormat, XNamespaceAliasSet namespaceAliases, out XElement licenseElement)
        {
            licenseElement = default;

            if (string.IsNullOrWhiteSpace(licenseToFormat?.Value))
            {
                return(false);
            }

            namespaceAliases.EnsureNamespaceAlias(CreativeCommonsExtensionConstants.NamespaceAlias, CreativeCommonsExtensionConstants.Namespace);
            licenseElement = new XElement(CreativeCommonsExtensionConstants.Namespace + "license")
            {
                Value = licenseToFormat.Value
            };
            return(true);
        }
Пример #28
0
        public Metadata GetLicenseMetadata()
        {
            var data = new DataSet();

            GatherDataItemsFromXElement(data, _dom.RawDom);
            var metadata = new Metadata();
            NamedMutliLingualValue d;

            if (data.TextVariables.TryGetValue("copyright", out d))
            {
                metadata.CopyrightNotice = WebUtility.HtmlDecode(d.TextAlternatives.GetFirstAlternative());
            }
            string licenseUrl = "";

            if (data.TextVariables.TryGetValue("licenseUrl", out d))
            {
                licenseUrl = WebUtility.HtmlDecode(d.TextAlternatives.GetFirstAlternative());
            }

            if (licenseUrl == null || licenseUrl.Trim() == "")
            {
                //NB: we are mapping "RightsStatement" (which comes from XMP-dc:Rights) to "LicenseNotes" in the html.
                //custom licenses live in this field, so if we have notes (and no URL) it is a custom one.
                if (data.TextVariables.TryGetValue("licenseNotes", out d))
                {
                    string licenseNotes = d.TextAlternatives.GetFirstAlternative();

                    metadata.License = new CustomLicense {
                        RightsStatement = WebUtility.HtmlDecode(licenseNotes)
                    };
                }
                else
                {
                    // The only remaining current option is a NullLicense
                    metadata.License = new NullLicense();                     //"contact the copyright owner
                }
            }
            else             // there is a licenseUrl, which means it is a CC license
            {
                metadata.License = CreativeCommonsLicense.FromLicenseUrl(licenseUrl);
                if (data.TextVariables.TryGetValue("licenseNotes", out d))
                {
                    metadata.License.RightsStatement = WebUtility.HtmlDecode(d.TextAlternatives.GetFirstAlternative());
                }
            }
            return(metadata);
        }
        public void ShowFullDialogTwiceToCheckRoundTripping()
        {
            var m = new Metadata();

            m.License = CreativeCommonsLicense.FromToken("by");
            m.License.RightsStatement = "some restrictions";

            using (var dlg = new MetadataEditorDialog(m))
            {
                dlg.ShowDialog();
                m = dlg.Metadata;
            }

            using (var dlg = new MetadataEditorDialog(m))
            {
                dlg.ShowDialog();
            }
        }
Пример #30
0
        public void MetadataSetter_WasCC3_thenIGO_UncheckingStillProducesCurrentDefaultLicense()
        {
            var m = new Metadata();

            m.CopyrightNotice = "test1";
            m.Creator         = "test2";
            var ccLicense = new CreativeCommonsLicense(true, false, CreativeCommonsLicense.DerivativeRules.DerivativesWithShareAndShareAlike);

            ccLicense.Version = "3.0";             // set old version (but non-IGO)
            m.License         = ccLicense;
            Assert.That(m.License.Url, Is.StringEnding("3.0/"));
            ccLicense.IntergovernmentalOriganizationQualifier = true;
            m.License = ccLicense;
            Assert.That(m.License.Url, Is.StringEnding("3.0/igo/"));
            // SUT
            ccLicense.IntergovernmentalOriganizationQualifier = false;
            m.License = ccLicense;
            // Considered an acceptable loss of information, since the user was messing with the IGO setting.
            Assert.That(m.License.Url, Is.StringEnding(CreativeCommonsLicense.kDefaultVersion + "/"));
        }