Exemple #1
0
        public Error AttachNoteToSalesOrder(SalesOrderHeaderModel soh, UserModel sender,
                                            string subject, string message,
                                            List <string> attachments = null,
                                            FileCopyType copyType     = FileCopyType.Move)
        {
            var error = new Error();

            var note = new NoteModel {
                CompanyId   = soh.CompanyId,
                NoteType    = NoteType.Sale,
                ParentId    = soh.Id,
                CreatedById = sender.Id,
                Subject     = subject,
                Message     = message
            };

            InsertOrUpdateNote(note, sender, "");

            error.Id = note.Id;
            if (!error.IsError && attachments != null)
            {
                foreach (var attachment in attachments)
                {
                    AttachMediaItemToNote(note, sender, attachment, attachment.FileName(), copyType);
                }
            }
            return(error);
        }
Exemple #2
0
        public Error AttachNoteToPurchaseOrder(PurchaseOrderHeader poh, UserModel sender,
                                               string subject, string message,
                                               List <string> attachments,
                                               FileCopyType copyType)
        {
            var error = new Error();

            var note = new NoteModel {
                CompanyId   = poh.CompanyId,
                NoteType    = NoteType.Purchase,
                ParentId    = poh.Id,
                CreatedById = sender.Id,
                Subject     = subject,
                Message     = message
            };

            InsertOrUpdateNote(note, sender, "");
            error.Id = note.Id;

            foreach (var attachment in attachments)
            {
                AttachMediaItemToNote(note, sender, attachment, attachment.FileName(), copyType);
            }

            return(error);
        }
        public static Error CopyOrMoveFile(string sourceFile, string targetFile, FileCopyType copyType)
        {
            var error = new Error();

            switch (copyType)
            {
            case FileCopyType.Move:
                DeleteFile(targetFile);
                CreateDirectory(targetFile.FolderName());

                try {
                    File.Move(sourceFile, targetFile);
                    dbStatic.LogTestFile(targetFile);
                } catch (Exception ex) {
                    error.SetError(ex);
                }
                break;

            case FileCopyType.Copy:
                CreateDirectory(targetFile.FolderName());

                try {
                    File.Copy(sourceFile, targetFile, true);
                    dbStatic.LogTestFile(targetFile);
                } catch (Exception ex) {
                    error.SetError(ex);
                }
                break;
            }
            return(error);
        }
 public void AddAttachments(List <string> fileNames, FileCopyType copyType = FileCopyType.Copy)
 {
     if (fileNames != null)
     {
         FileCopyType = copyType;
         Attachments.AddRange(fileNames);
     }
 }
 public void AddAttachment(string fileName, FileCopyType copyType = FileCopyType.Copy)
 {
     if (!string.IsNullOrEmpty(fileName))
     {
         FileCopyType = copyType;
         Attachments.Add(fileName);
     }
 }
        private Error addAttachments(EMailQueue email, List <string> attachments, FileCopyType fileCopyType)
        {
            var error = new Error();

            if (attachments != null && attachments.Count() > 0)
            {
                // Queue the attachments
                int orderNo = 1;
                foreach (var fileName in attachments)
                {
                    // Copy or move the file to a temporary location for sending. After sending, we delete it.
                    string tempFolder = GetConfigurationSetting("MediaFolder", "") + "\\EMailQueue\\" + email.Id.ToString();
                    try {
                        Directory.CreateDirectory(tempFolder);
                    } catch { }
                    db.LogTestFolder(tempFolder);

                    // Copy/move the file
                    string targetFile = tempFolder + "\\" + fileName.FileName();

                    try {
                        if (fileCopyType == FileCopyType.Move)
                        {
                            File.Move(fileName, targetFile);
                        }
                        else
                        {
                            File.Copy(fileName, targetFile, true);
                        }

                        var attachment = new EMailQueueAttachment {
                            EMailQueue = email,
                            FileName   = targetFile,
                            OrderNo    = orderNo++
                        };
                        db.InsertOrUpdateEMailQueueAttachment(attachment);
                    } catch (Exception e1) {
                        error.SetError(e1);
                        break;
                    }
                }
            }
            return(error);
        }
        public Error AddMediaToProduct(ProductModel product,
                                       CompanyModel company,
                                       UserModel user,
                                       string mediaFile,
                                       ProductMediaModel prodMedia,      // output
                                       FileCopyType copyType)
        {
            var error = new Error();

            var media = new MediaModel();

            error = MediaServices.InsertOrUpdateMedia(media,
                                                      company,
                                                      user,
                                                      MediaFolder.Product,
                                                      mediaFile,
                                                      "",
                                                      product.Id,
                                                      -1,
                                                      copyType);
            if (!error.IsError)
            {
                var tempProdMedia = db.FindProductMedias(product.Id)
                                    .Where(pm => pm.MediaId == media.Id)
                                    .FirstOrDefault();
                if (tempProdMedia == null)
                {
                    // Not currently linked, so link it
                    tempProdMedia = new ProductMedia {
                        Id        = 0,
                        ProductId = product.Id,
                        MediaId   = media.Id
                    };
                    db.InsertOrUpdateProductMedia(tempProdMedia);
                }
                Mapper.Map <ProductMedia, ProductMediaModel>(tempProdMedia, prodMedia);
            }

            return(error);
        }
Exemple #8
0
        public Error AttachMediaItemToNote(NoteModel note,
                                           UserModel user,
                                           string fileName,
                                           string description,
                                           FileCopyType copyType)
        {
            var error = new Error();

            var media   = new MediaModel();
            var company = new CompanyModel {
                Id = note.CompanyId
            };

            string folderName = MediaServices.GetMediaFolder((MediaFolder)note.NoteType,
                                                             company.Id, note.ParentId, note.Id,
                                                             false, true);

            error = MediaServices.InsertOrUpdateMedia(media,
                                                      company,
                                                      user,
                                                      (MediaFolder)note.NoteType,
                                                      fileName,
                                                      "",
                                                      note.ParentId,
                                                      note.Id,
                                                      copyType);
            if (!error.IsError)
            {
                NoteAttachment attachment = new NoteAttachment {
                    Company = db.FindCompany(note.CompanyId),
                    NoteId  = note.ParentId,
                    Note    = db.FindNote(note.Id),
                    Medium  = db.FindMedia(media.Id)
                };
                db.InsertOrUpdateNoteAttachment(attachment);
            }

            return(error);
        }
Exemple #9
0
        public Error InsertOrUpdateMedia(MediaModel model,
                                         CompanyModel company, UserModel user,
                                         MediaFolder mediaFolder,
                                         string sourceFile,         // Fully qualified file name or URL
                                         string lgs,
                                         int itemId,
                                         int subItemId,
                                         FileCopyType copyType)
        {
            // Filename must be a web URL or a fully qualified filename
            var error = new Error();

            if (string.IsNullOrEmpty(sourceFile))
            {
                error.SetError(EvolutionResources.errInvalidFileName, "", sourceFile);
            }
            else if (!string.IsNullOrEmpty(sourceFile) && !IsValidMediaType(sourceFile))
            {
                error.SetError(EvolutionResources.errFileTypeNotValidForUpload, "", sourceFile.FileExtension());
            }
            else
            {
                string tempFolderName = GetMediaFolder(mediaFolder,
                                                       company.Id,
                                                       itemId,
                                                       subItemId,
                                                       true,
                                                       true) + "/";
                string tempFileName = sourceFile.IsWebUrl() ? sourceFile : sourceFile.FileName();

                var media = db.FindMedia(tempFolderName, tempFileName);
                if (media == null)
                {
                    media = new Medium {
                        CreatedDate = DateTimeOffset.Now,
                        CreatedById = user.Id,
                        MediaType   = db.FindMediaType(sourceFile),
                        Title       = tempFileName,
                        FolderName  = tempFolderName,
                        FileName    = tempFileName,
                        ImageW      = 640,
                        ImageH      = 480
                    };
                    if (company != null && company.Id != -1)
                    {
                        media.CompanyId = company.Id;
                    }
                    db.InsertOrUpdateMedia(media);
                }
                else
                {
                    if (!db.IsLockStillValid(typeof(Medium).ToString(), model.Id, lgs))
                    {
                        error.SetError(EvolutionResources.errRecordChangedByAnotherUser);
                    }
                    else
                    {
                        media.ModifiedDate = DateTimeOffset.Now;
                        media.ModifiedById = user.Id;
                        media.MediaType    = db.FindMediaType(sourceFile);
                        media.Title        = tempFileName;
                        media.ImageW       = 640;
                        media.ImageH       = 480;
                        db.InsertOrUpdateMedia(media);
                    }
                }

                if (!error.IsError)
                {
                    // Now move the file if it exists
                    int actualW = 0,
                        actualH = 0,
                        thumbW  = 0,
                        thumbH  = 0;

                    if (File.Exists(sourceFile))
                    {
                        string targetFolder = GetMediaFolder() + media.FolderName.Replace("/", "\\");
                        string targetFile   = targetFolder + media.FileName;

                        error = MediaService.CopyOrMoveFile(sourceFile, targetFile, copyType);

                        if (!error.IsError)
                        {
                            // Create thumbnails
                            if (media.MediaType.CreateThumb)
                            {
                                CreateThumbNail(targetFile, GetThumbFileName(targetFile, MediaSize.Small), (int)MediaSize.SmallW, (int)MediaSize.SmallH, true, ref actualW, ref actualH, ref thumbW, ref thumbH);
                                media.ImageW = actualW;
                                media.ImageH = actualH;
                                db.InsertOrUpdateMedia(media);

                                CreateThumbNail(targetFile, GetThumbFileName(targetFile, MediaSize.Medium), (int)MediaSize.MediumW, (int)MediaSize.MediumH, true, ref actualW, ref actualH, ref thumbW, ref thumbH);
                                CreateThumbNail(targetFile, GetThumbFileName(targetFile, MediaSize.Large), (int)MediaSize.LargeW, (int)MediaSize.LargeH, true, ref actualW, ref actualH, ref thumbW, ref thumbH);
                            }
                        }
                    }
                    else if (media.MediaType != null &&
                             media.MediaType.CreateThumb &&
                             media.FileName.IsWebUrl())
                    {
                        // Media is a url to a thumbable media item
                        error = GetImageSize(sourceFile, ref actualW, ref actualH);
                        if (!error.IsError)
                        {
                            media.ImageW = actualW;
                            media.ImageH = actualH;
                            db.InsertOrUpdateMedia(media);
                        }
                    }

                    Mapper.Map <Medium, MediaModel>(media, model);
                    model.Lightboxable = media.MediaType.Lightboxable;
                    model.MediaHtml    = GetMediaHtml(model, MediaSize.Small, (int)MediaSize.SmallW, (int)MediaSize.SmallH);
                }
            }
            return(error);
        }