Exemplo n.º 1
0
        public Operation <string> UpdateProfileImage(EncodedBinaryData image, string oldImageUrl)
        => _authorizer.AuthorizeAccess(UserContext.CurrentProcessPermissionProfile(), () =>
        {
            var user = UserContext.CurrentUser();

            var url = _blobStore.Delete(oldImageUrl) //shouldnt fail even if oldImageUrl is null
                      .Then(opr => _blobStore.Persist(image))
                      .Resolve();

            //create the ProfileImage UserData and store the url
            var userData = _query.GetUserData(user, Constants.UserData_ProfileImage) ?? new UserData
            {
                Name    = Constants.UserData_ProfileImage,
                OwnerId = user.UserId,
                Type    = CommonDataType.Url
            };

            //set the data
            userData.Data = url;

            if (userData.EntityId > 0)
            {
                return(_pcommand.Update(userData).Resolve().Data);
            }

            else
            {
                return(_pcommand.Add(userData).Resolve().Data);
            }
        });
Exemplo n.º 2
0
        public Operation <string> Persist(EncodedBinaryData blob)
        => Operation.Try(() =>
        {
            var mime     = blob.MimeObject();
            var fileName = GenerateUniqueName(mime);

            using (var stream = new FileInfo(fileName).OpenWrite())
                stream.Write(blob.Data, 0, blob.Data.Length);

            if (!string.IsNullOrWhiteSpace(blob.Metadata))
            {
                using (var writer = new StreamWriter(new FileInfo(fileName + ".meta").OpenWrite()))
                {
                    blob.MetadataTags().ForAll((cnt, next) => writer.WriteLine(next));
                    writer.Flush();
                }
            }

            return(UrlProvider.GenerateBlobUrl(new FileInfo(fileName).Name));
        });