示例#1
0
        public static NoteFile Create(string name, string mime, byte[] data, Note owner, string extension = null)
        {
            var folder = Hub.Instance.Storage.GetNoteFolderPath(owner.ID);
            var ext    = extension ?? MimeType.GetDefaultExtension(mime);

            if (string.IsNullOrWhiteSpace(ext) && name != null && name.IndexOf(".", StringComparison.Ordinal) != -1)
            {
                ext = name.Split('.')[1];
            }

            if (string.IsNullOrWhiteSpace(ext))
            {
                throw new Exception("Unable to get file extension.");
            }

            var finalFileName = owner.ID + "_" + GetNewFilenumber(owner.Files.ToList()) + ext;
            var finalFilePath = folder + "\\" + finalFileName;

            try
            {
                File.WriteAllBytes(finalFilePath, data);

                var nf = new NoteFile(finalFileName, name ?? finalFileName, owner.ID);
                owner.Files.Add(nf);
                owner.Save();
                return(nf);
            }
            catch (Exception)
            {
                return(null);
            }
        }