public ActionResult Upload(AgreementImage agreementImage, HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                using (var context = new DatabaseContext())
                {
                    var agreementImages = context.AgreementImages
                        .Where(x => x.AgreementId == agreementImage.AgreementId).ToList();
                    var sequenceNumber = (byte)(agreementImages.Count == 0 ? 1
                        : agreementImages.Max(x => x.SequenceNumber) + 1);
                    agreementImage.SequenceNumber = sequenceNumber;
                    var bytes = agreementImage.Initialize(file.InputStream, file.ContentLength, file.FileName);

                    context.AgreementImages.Add(agreementImage);
                    context.SaveChanges();

                    var storage = new AgreementImageStorage();
                    storage.Upload(agreementImage.Id, bytes);
                }
            }
            return RedirectToAction("WithImages", "Agreement", new { id = agreementImage.AgreementId });
        }
예제 #2
0
        /// <summary>Adds automatic images to the new agreement</summary>
        private static void AddAutomaticImages(DatabaseContext context, Agreement agreement)
        {
            if (agreement.Type == null) return;

            var fileInfos = new List<Tuple<AgreementType, byte, int>>
            {
                new Tuple<AgreementType, byte, int>(AgreementType.MetricsPassportsIds, 1, 1368708),
                new Tuple<AgreementType, byte, int>(AgreementType.MetricsPassportsIds, 3, 1204257),
                new Tuple<AgreementType, byte, int>(AgreementType.MetricsPassportsIds, 4, 1368708),
                new Tuple<AgreementType, byte, int>(AgreementType.DocumentPackageGathering, 2, 1019502),
                new Tuple<AgreementType, byte, int>(AgreementType.DocumentPackageGathering, 3, 850639)
            };

            var infos = fileInfos.Where(x => x.Item1 == agreement.Type).ToList();
            if (infos.Count == 0) return;

            var storage = new AgreementImageStorage();
            infos.ForEach(info =>
            {
                using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(string.Format(
                    "O.Danheli.Azure.WebRole.Content.Agreements.{0}.Page{1}.jpg", info.Item1, info.Item2)))
                {
                    var agreementImage = new AgreementImage
                    {
                        AgreementId = agreement.Id,
                        SequenceNumber = info.Item2
                    };
                    var bytes = agreementImage.Initialize(stream, info.Item3, string.Format("Page{0}.jpg", info.Item2));
                    context.AgreementImages.Add(agreementImage);
                    context.SaveChanges();
                    storage.Upload(agreementImage.Id, bytes);
                }
            });
        }