예제 #1
0
        public async Task <IHttpActionResult> PutQrProfile(Guid id, QrProfile qrProfile)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != qrProfile.Id)
            {
                return(BadRequest());
            }

            db.Entry(qrProfile).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!QrProfileExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public async Task <IHttpActionResult> Upload(Guid profileId)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                var provider = new MultipartFileStreamProvider(Path.GetTempPath());
                var content  = new StreamContent(HttpContext.Current.Request.GetBufferlessInputStream(true));
                foreach (var header in Request.Content.Headers)
                {
                    content.Headers.TryAddWithoutValidation(header.Key, header.Value);
                }
                var tsk = await content.ReadAsMultipartAsync(provider);

                var result   = new List <AudioFile>();
                var docfiles = new List <string>();
                foreach (var fileData in tsk.FileData)
                {
                    AudioFile audioFile = null;
                    // Sometimes the filename has a leading and trailing double-quote character
                    // when uploaded, so we trim it; otherwise, we get an illegal character exception
                    var fileName      = Path.GetFileName(fileData.Headers.ContentDisposition.FileName.Trim('"'));
                    var mediaType     = fileData.Headers.ContentType.MediaType;
                    var localFileName = fileData.LocalFileName;
                    using (var fileStream = File.OpenRead(fileData.LocalFileName))
                    {
                        fileStream.Position        = 0;
                        audioFile                  = new AudioFile();
                        audioFile.QrProfileId      = profileId;
                        audioFile.UploadedByUserId = this.User.Identity.GetUserId();
                        audioFile.UploadedOn       = DateTime.Now;
                        audioFile.Extension        = Path.GetExtension(fileData.Headers.ContentDisposition.FileName.Replace("\"", ""));
                        db.AudioFiles.Add(audioFile);
                        var service = new BlobService();
                        await service.UploadBlob(audioFile.Id.ToString(), mediaType, fileStream, BlobHelper.Repository.Audio);

                        await db.SaveChangesAsync();
                    }
                    result.Add(audioFile);
                    File.Delete(fileData.LocalFileName);
                }
                return(Ok(result));
            }
            catch (InvalidDataException ex)
            {
                return(BadRequest(ex.Message));
            }
        }
        public async Task <IHttpActionResult> Upload(Guid profileId)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                var hasVideoAlbum = db.Videos.Any(q => q.QrProfileId == profileId);
                var profile       = db.QrProfiles.First(q => q.Id == profileId);
                var profileName   = profile.FirstName + " " + profile.LastName;

                var provider = new MultipartFileStreamProvider(Path.GetTempPath());
                var content  = new StreamContent(HttpContext.Current.Request.GetBufferlessInputStream(true));
                foreach (var header in Request.Content.Headers)
                {
                    content.Headers.TryAddWithoutValidation(header.Key, header.Value);
                }
                var tsk = await content.ReadAsMultipartAsync(provider);

                var result   = new List <VideoFile>();
                var docfiles = new List <string>();
                foreach (var fileData in tsk.FileData)
                {
                    VideoFile videoFile = null;
                    // Sometimes the filename has a leading and trailing double-quote character
                    // when uploaded, so we trim it; otherwise, we get an illegal character exception
                    var fileName      = Path.GetFileName(fileData.Headers.ContentDisposition.FileName.Trim('"'));
                    var mediaType     = fileData.Headers.ContentType.MediaType;
                    var localFileName = fileData.LocalFileName;
                    using (var fileStream = File.OpenRead(fileData.LocalFileName))
                    {
                        fileStream.Position = 0;

                        //var client = new VimeoDotNet.VimeoClient("101df01f949f134370f77a375d8e169e7cc43ae8", "9rZXXj+4iRk8Wv95QPNNjf4sU+MI9+O7CczWnRp4L1dQF0V7hGeHA/bR0repqePh+TyvZNnRW2aFlWd34RD5JCfp7clj8nQuFZHRTxYjeSXwutIyJOSW/G9Sv5athNfB");
                        var  client       = new VimeoDotNet.VimeoClient("7510a353d5691dd4d17f90455d063329");
                        long?videoAlbumId = IsProd ? 4572966 : 4573995;
                        //if (!hasVideoAlbum)
                        //{
                        //    var editAlbumParameters = new VimeoDotNet.Parameters.EditAlbumParameters()
                        //    {
                        //        Description = $"Album for {profileName}",
                        //        Name = profileName,
                        //        Privacy = VimeoDotNet.Parameters.EditAlbumPrivacyOption.Anybody
                        //    };
                        //    var album = await client.CreateAlbumAsync(editAlbumParameters);
                        //    profile.VideoAlbumId = album.GetAlbumId().Value;
                        //}
                        //videoAlbumId = profile.VideoAlbumId;

                        var uploadRequest = await client.UploadEntireFileAsync(new BinaryContent(fileStream, fileData.Headers.ContentType.MediaType));

                        await client.AddToAlbumAsync(videoAlbumId.Value, uploadRequest.ClipId.Value);

                        videoFile                  = new VideoFile();
                        videoFile.QrProfileId      = profileId;
                        videoFile.UploadedByUserId = this.User.Identity.GetUserId();
                        videoFile.UploadedOn       = DateTime.Now;
                        videoFile.Extension        = Path.GetExtension(fileData.Headers.ContentDisposition.FileName.Replace("\"", ""));
                        videoFile.Url              = uploadRequest.ClipUri.Replace("videos", "video");
                        videoFile.ClipId           = uploadRequest.ClipId.Value;

                        db.Videos.Add(videoFile);

                        //var service = new BlobService();
                        //await service.UploadBlob( videoFile.Id.ToString(), mediaType, fileStream, BlobHelper.Repository.Video );
                        await db.SaveChangesAsync();
                    }
                    result.Add(videoFile);
                    File.Delete(fileData.LocalFileName);
                }
                return(Ok(result));
            }
            catch (InvalidDataException ex)
            {
                return(BadRequest(ex.Message));
            }
        }
예제 #4
0
        public async Task <IHttpActionResult> Upload(Guid profileId)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                var provider = new MultipartFileStreamProvider(Path.GetTempPath());
                var content  = new StreamContent(HttpContext.Current.Request.GetBufferlessInputStream(true));
                foreach (var header in Request.Content.Headers)
                {
                    content.Headers.TryAddWithoutValidation(header.Key, header.Value);
                }
                var tsk = await content.ReadAsMultipartAsync(provider);

                var result   = new List <Photo>();
                var docfiles = new List <string>();
                foreach (var fileData in tsk.FileData)
                {
                    Photo photo = null;
                    // Sometimes the filename has a leading and trailing double-quote character
                    // when uploaded, so we trim it; otherwise, we get an illegal character exception
                    var fileName      = Path.GetFileName(fileData.Headers.ContentDisposition.FileName.Trim('"'));
                    var mediaType     = fileData.Headers.ContentType.MediaType;
                    var localFileName = fileData.LocalFileName;
                    using (var fileStream = File.OpenRead(fileData.LocalFileName))
                    {
                        if (IsValidFile(fileStream))
                        {
                            fileStream.Position = 0;
                            using (var tinyPng = ResizeImage(fileStream))
                            {
                                photo                  = new Photo();
                                photo.QrProfileId      = profileId;
                                photo.UploadedByUserId = this.User.Identity.GetUserId();
                                photo.UploadedOn       = DateTime.Now;
                                var image = Image.FromStream(fileStream);
                                if (image.RawFormat.Equals(ImageFormat.Png))
                                {
                                    photo.Extension = ".png";
                                }
                                else if (image.RawFormat.Equals(ImageFormat.Jpeg))
                                {
                                    photo.Extension = ".jpg";
                                }
                                db.Photos.Add(photo);
                                var service = new BlobService();
                                await service.UploadBlob(photo.Id.ToString(), mediaType, tinyPng, BlobHelper.Repository.Photos);

                                await db.SaveChangesAsync();
                            }
                        }
                    }
                    File.Delete(fileData.LocalFileName);
                }
                return(Ok());
            }
            catch (InvalidDataException ex)
            {
                return(BadRequest(ex.Message));
            }
        }
        public async Task <IHttpActionResult> PutQrProfile(Guid id, QrProfile profile)
        {
            var existingParent = db.QrProfiles
                                 .Where(p => p.Id == profile.Id)
                                 .Include(p => p.Links)
                                 .SingleOrDefault();

            if (existingParent != null)
            {
                foreach (var link in profile.Links)
                {
                    link.QrProfileId = profile.Id;
                }
                var userId = this.User.Identity.GetUserId();
                profile.UserId = userId;
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                if (id != profile.Id)
                {
                    return(BadRequest());
                }

                // Update parent
                db.Entry(existingParent).CurrentValues.SetValues(profile);

                #region Links
                // Delete children
                foreach (var existingChild in existingParent.Links.ToList())
                {
                    if (!profile.Links.Any(c => c.Id == existingChild.Id))
                    {
                        db.Links.Remove(existingChild);
                    }
                }

                // Update and Insert children
                foreach (var childModel in profile.Links)
                {
                    var existingChild = existingParent.Links
                                        .Where(c => c.Id == childModel.Id)
                                        .SingleOrDefault();

                    if (existingChild != null)
                    {
                        // Update child
                        db.Entry(existingChild).CurrentValues.SetValues(childModel);
                    }
                    else
                    {
                        // Insert child
                        var newChild = new QrLink
                        {
                            Id          = childModel.Id,
                            Label       = childModel.Label,
                            QrProfileId = childModel.QrProfileId,
                            Url         = childModel.Url
                        };
                        existingParent.Links.Add(newChild);
                    }
                }
                #endregion

                try
                {
                    await db.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!QrProfileExists(id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            //var i = 0;
            //foreach (var l in QrProfile.Links)
            //{
            //	l.QrProfileId = QrProfile.Id;
            //	l.Id = i++;
            //}
            //var userId = this.User.Identity.GetUserId();
            //QrProfile.UserId = userId;
            //if (!ModelState.IsValid)
            //{
            //	return BadRequest(ModelState);
            //}

            //if (id != QrProfile.Id)
            //{
            //	return BadRequest();
            //}

            //db.Entry(QrProfile).State = EntityState.Modified;

            //try
            //{
            //	await db.SaveChangesAsync();
            //}
            //catch (DbUpdateConcurrencyException)
            //{
            //	if (!QrProfileExists(id))
            //	{
            //		return NotFound();
            //	}
            //	else
            //	{
            //		throw;
            //	}
            //}
            return(Ok(existingParent));
        }