github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/system/initial/local_migrations/m_images.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2023, Filippov Alex
     4  //
     5  // This library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU Lesser General Public
     7  // License as published by the Free Software Foundation; either
     8  // version 3 of the License, or (at your option) any later version.
     9  //
    10  // This library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  // Library General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public
    16  // License along with this library.  If not, see
    17  // <https://www.gnu.org/licenses/>.
    18  
    19  package local_migrations
    20  
    21  import (
    22  	"context"
    23  	"os"
    24  	"path"
    25  	"strings"
    26  
    27  	"github.com/e154/smart-home/adaptors"
    28  	"github.com/e154/smart-home/common"
    29  	m "github.com/e154/smart-home/models"
    30  	. "github.com/e154/smart-home/system/initial/assertions"
    31  )
    32  
    33  type MigrationImages struct {
    34  	adaptors *adaptors.Adaptors
    35  	dir      string
    36  }
    37  
    38  func NewMigrationImages(adaptors *adaptors.Adaptors, dir string) *MigrationImages {
    39  	if dir == "" {
    40  		dir = "./"
    41  	}
    42  	return &MigrationImages{
    43  		adaptors: adaptors,
    44  		dir:      dir,
    45  	}
    46  }
    47  
    48  func (i *MigrationImages) Up(ctx context.Context, adaptors *adaptors.Adaptors) (err error) {
    49  
    50  	if adaptors != nil {
    51  		i.adaptors = adaptors
    52  	}
    53  
    54  	imageList := map[string]*m.Image{
    55  		"button_v1_off": {
    56  			Image:    "30d2f4116a09fd14b49c266985db8109.svg",
    57  			MimeType: "text/html; charset=utf-8",
    58  			Size:     2518,
    59  			Name:     "button_v1_off.svg",
    60  		},
    61  		"button_v1_refresh": {
    62  			Image:    "86486ca5d086aafd5724d61251b94bba.svg",
    63  			MimeType: "text/html; charset=utf-8",
    64  			Size:     3212,
    65  			Name:     "button_v1_refresh.svg",
    66  		},
    67  		"lamp_v1_r": {
    68  			Image:    "2d4a761241e24a77725287180656b466.svg",
    69  			MimeType: "text/xml; charset=utf-8",
    70  			Size:     2261,
    71  			Name:     "lamp_v1_r.svg",
    72  		},
    73  		"socket_v1_b": {
    74  			Image:    "bef910d70c56f38b22cea0c00d92d8cc.svg",
    75  			MimeType: "text/html; charset=utf-8",
    76  			Size:     7326,
    77  			Name:     "socket_v1_b.svg",
    78  		},
    79  		"button_v1_on": {
    80  			Image:    "7c145f62dcaf8da2a9eb43f2b23ea2b1.svg",
    81  			MimeType: "text/html; charset=utf-8",
    82  			Size:     2398,
    83  			Name:     "button_v1_on.svg",
    84  		},
    85  		"socket_v1_def": {
    86  			Image:    "4c28edf0700531731df43ed055ebf56d.svg",
    87  			MimeType: "text/html; charset=utf-8",
    88  			Size:     7326,
    89  			Name:     "socket_v1_def.svg",
    90  		},
    91  		"socket_v1_r": {
    92  			Image:    "e91e461f7c9a800eed5a074101d3e5a5.svg",
    93  			MimeType: "text/html; charset=utf-8",
    94  			Size:     7326,
    95  			Name:     "socket_v1_r.svg",
    96  		},
    97  		"lamp_v1_def": {
    98  			Image:    "91e93ee7e7734654083dee0a5cbe55e9.svg",
    99  			MimeType: "text/xml; charset=utf-8",
   100  			Size:     2266,
   101  			Name:     "lamp_v1_def.svg",
   102  		},
   103  		"socket_v1_g": {
   104  			Image:    "4819b36056dfa786f5856fa45e9a3151.svg",
   105  			MimeType: "text/html; charset=utf-8",
   106  			Size:     7326,
   107  			Name:     "socket_v1_g.svg",
   108  		},
   109  		"lamp_v1_y": {
   110  			Image:    "c1c5ec4e75bb6ec33f5f8cfd87b0090e.svg",
   111  			MimeType: "text/xml; charset=utf-8",
   112  			Size:     2261,
   113  			Name:     "lamp_v1_y.svg",
   114  		},
   115  		"socket_v2_b": {
   116  			Image:    "c813ac54bb4dd6b99499d097eda67310.svg",
   117  			MimeType: "text/html; charset=utf-8",
   118  			Size:     3060,
   119  			Name:     "socket_v2_b.svg",
   120  		},
   121  		"socket_v2_def": {
   122  			Image:    "f0ea38f2b388dc2bb2566f6efc7731b0.svg",
   123  			MimeType: "text/html; charset=utf-8",
   124  			Size:     3060,
   125  			Name:     "socket_v2_def.svg",
   126  		},
   127  		"socket_v2_g": {
   128  			Image:    "fa6b42c81056069d03857cfbb2cf95eb.svg",
   129  			MimeType: "text/html; charset=utf-8",
   130  			Size:     3060,
   131  			Name:     "socket_v2_g.svg",
   132  		},
   133  		"socket_v2_r": {
   134  			Image:    "e565f191030491cfdc39ad728559c18f.svg",
   135  			MimeType: "text/html; charset=utf-8",
   136  			Size:     3060,
   137  			Name:     "socket_v2_r.svg",
   138  		},
   139  		"socket_v3_b": {
   140  			Image:    "297d56426098a53091fb8f91aabe3cd7.svg",
   141  			MimeType: "text/html; charset=utf-8",
   142  			Size:     2718,
   143  			Name:     "socket_v3_b.svg",
   144  		},
   145  		"socket_v3_def": {
   146  			Image:    "becf0f8f635061c143acb4329f744615.svg",
   147  			MimeType: "text/html; charset=utf-8",
   148  			Size:     2718,
   149  			Name:     "socket_v3_def.svg",
   150  		},
   151  		"socket_v3_g": {
   152  			Image:    "850bf4da00cb9de85e1442695230a127.svg",
   153  			MimeType: "text/html; charset=utf-8",
   154  			Size:     2718,
   155  			Name:     "socket_v3_g.svg",
   156  		},
   157  		"socket_v3_r": {
   158  			Image:    "434514389e95cab6d684b978378055d5.svg",
   159  			MimeType: "text/html; charset=utf-8",
   160  			Size:     2718,
   161  			Name:     "socket_v3_r.svg",
   162  		},
   163  		"map-schematic-original": {
   164  			Image:    "9384f1f6f9c2f4bf00fbc6debaae9b26.svg",
   165  			MimeType: "text/html; charset=utf-8",
   166  			Size:     195108,
   167  			Name:     "map-schematic-original.svg",
   168  		},
   169  		"temp_v1_r": {
   170  			Image:    "688d2d752252de21c9d62a643c37ea40.svg",
   171  			MimeType: "text/html; charset=utf-8",
   172  			Size:     3980,
   173  			Name:     "temp_v1_r.svg",
   174  		},
   175  		"temp_v1_y": {
   176  			Image:    "8b2f46785aa3bdf7a6a487fc89a0f99e.svg",
   177  			MimeType: "text/html; charset=utf-8",
   178  			Size:     3980,
   179  			Name:     "temp_v1_y.svg",
   180  		},
   181  		"temp_v1_def": {
   182  			Image:    "655d491beafaefce2117cb2012dc674a.svg",
   183  			MimeType: "text/html; charset=utf-8",
   184  			Size:     3980,
   185  			Name:     "temp_v1_def.svg",
   186  		},
   187  		"temp_v1_original": {
   188  			Image:    "e8dee745788685f9f86e611cf5758cab.svg",
   189  			MimeType: "text/html; charset=utf-8",
   190  			Size:     3770,
   191  			Name:     "temp_v1_original.svg",
   192  		},
   193  		"fan_v1_r": {
   194  			Image:    "eaf1c68959341c466fac68363f21cbbe.svg",
   195  			MimeType: "text/html; charset=utf-8",
   196  			Size:     6238,
   197  			Name:     "fan_v1_r.svg",
   198  		},
   199  		"fan_v1_y": {
   200  			Image:    "33a5d5e7290e0f37a4c160cdbd0b5f23.svg",
   201  			MimeType: "text/html; charset=utf-8",
   202  			Size:     6238,
   203  			Name:     "fan_v1_y.svg",
   204  		},
   205  		"fan_v1_def": {
   206  			Image:    "fd64ec639417d88e37b1c2cc167bcafc.svg",
   207  			MimeType: "text/html; charset=utf-8",
   208  			Size:     6238,
   209  			Name:     "fan_v1_def.svg",
   210  		},
   211  		"fan_v1_original": {
   212  			Image:    "b4820c5939fe6b042888c922dfd1bada.svg",
   213  			MimeType: "text/html; charset=utf-8",
   214  			Size:     5799,
   215  			Name:     "fan_v1_original.svg",
   216  		},
   217  		"door_v1_closed": {
   218  			Image:    "2e3e5c74775360e0274576ba6c83f044.svg",
   219  			MimeType: "text/html; charset=utf-8",
   220  			Size:     509,
   221  			Name:     "door_v1_closed.svg",
   222  		},
   223  		"door_v1_closed_r": {
   224  			Image:    "221f451b426188a2df987163a2ab5715.svg",
   225  			MimeType: "text/html; charset=utf-8",
   226  			Size:     508,
   227  			Name:     "door_v1_closed_r.svg",
   228  		},
   229  		"door_v1_closed_def": {
   230  			Image:    "dd2a735b71b2899869e36c54f140b3fa.svg",
   231  			MimeType: "text/html; charset=utf-8",
   232  			Size:     509,
   233  			Name:     "door_v1_closed_def.svg",
   234  		},
   235  		"door_v1_opened1": {
   236  			Image:    "74cb4de3f70bb7a7e5d651ee6a23bffc.svg",
   237  			MimeType: "text/html; charset=utf-8",
   238  			Size:     725,
   239  			Name:     "door_v1_opened1.svg",
   240  		},
   241  		"door_v1_opened2": {
   242  			Image:    "def90d2778eb6e4465f5808889e2a92c.svg",
   243  			MimeType: "text/html; charset=utf-8",
   244  			Size:     862,
   245  			Name:     "door_v1_opened2.svg",
   246  		},
   247  		"door_v1_opened3": {
   248  			Image:    "fe7c9ecdbbdedc99ab16070da52251a4.svg",
   249  			MimeType: "text/html; charset=utf-8",
   250  			Size:     1179,
   251  			Name:     "door_v1_opened3.svg",
   252  		},
   253  		"md_v1_def": {
   254  			Image:    "3f7482861152f6bf9de3940aa031e7bf.svg",
   255  			MimeType: "text/html; charset=utf-8",
   256  			Size:     1686,
   257  			Name:     "motion_detection_v1_def.svg",
   258  		},
   259  		"md_v1_original": {
   260  			Image:    "763b23acd999eb268deec7320c4b0b88.svg",
   261  			MimeType: "text/html; charset=utf-8",
   262  			Size:     1689,
   263  			Name:     "motion_detection_v1_original.svg",
   264  		},
   265  		"md_v1_r": {
   266  			Image:    "5d6d372b84cb75b80e6447cbc5cecb72.svg",
   267  			MimeType: "text/html; charset=utf-8",
   268  			Size:     1685,
   269  			Name:     "motion_detection_v1_r.svg",
   270  		},
   271  		"md_v1_o": {
   272  			Image:    "0fcdb4e0857adeb71f699b18ce22e403.svg",
   273  			MimeType: "text/html; charset=utf-8",
   274  			Size:     1711,
   275  			Name:     "motion_detection_v1_o.svg",
   276  		},
   277  		"md_v1_y": {
   278  			Image:    "55a011dcd81772b9752470471842d365.svg",
   279  			MimeType: "text/html; charset=utf-8",
   280  			Size:     1689,
   281  			Name:     "motion_detection_v1_y.svg",
   282  		},
   283  		"md_v2_def": {
   284  			Image:    "fe4fad32a33ef2448debab3cf2dc4c6f.svg",
   285  			MimeType: "text/html; charset=utf-8",
   286  			Size:     1388,
   287  			Name:     "motion_detection_v2_def.svg",
   288  		},
   289  		"md_v2_original": {
   290  			Image:    "73125c0ca60b84b647fe5f7bc2833432.svg",
   291  			MimeType: "text/html; charset=utf-8",
   292  			Size:     1391,
   293  			Name:     "motion_detection_v2_original.svg",
   294  		},
   295  		"md_v2_r": {
   296  			Image:    "82799f02881efe45f2a5211ab357e2e2.svg",
   297  			MimeType: "text/html; charset=utf-8",
   298  			Size:     1387,
   299  			Name:     "motion_detection_v2_r.svg",
   300  		},
   301  		"md_v2_o": {
   302  			Image:    "58b1695473e44277c0306a726a601aef.svg",
   303  			MimeType: "text/html; charset=utf-8",
   304  			Size:     1391,
   305  			Name:     "motion_detection_v2_o.svg",
   306  		},
   307  		"md_v2_y": {
   308  			Image:    "aab3542bb028c61a4343bfc4e8c92daf.svg",
   309  			MimeType: "text/html; charset=utf-8",
   310  			Size:     1391,
   311  			Name:     "motion_detection_v2_y.svg",
   312  		},
   313  	}
   314  
   315  	var subDir string
   316  	for _, image := range imageList {
   317  		if _, err = i.adaptors.Image.GetByImageName(ctx, image.Image); err == nil {
   318  			continue
   319  		}
   320  
   321  		image.Id, err = i.adaptors.Image.Add(ctx, image)
   322  		So(err, ShouldBeNil)
   323  
   324  		fullPath := common.GetFullPath(image.Image)
   325  		to := path.Join(path.Join(i.dir, fullPath), image.Image)
   326  
   327  		//log.Infof("create dir %s", path.Join(i.dir, fullPath))
   328  		_ = os.MkdirAll(path.Join(i.dir, fullPath), os.ModePerm)
   329  
   330  		if exist := common.FileExist(to); !exist {
   331  
   332  			switch {
   333  			case strings.Contains(image.Name, "button"):
   334  				subDir = "buttons"
   335  			case strings.Contains(image.Name, "lamp"):
   336  				subDir = "lamp"
   337  			case strings.Contains(image.Name, "socket"):
   338  				subDir = "socket"
   339  			case strings.Contains(image.Name, "temp"):
   340  				subDir = "temp"
   341  			case strings.Contains(image.Name, "fan"):
   342  				subDir = "fan"
   343  			case strings.Contains(image.Name, "map"):
   344  				subDir = "map"
   345  			case strings.Contains(image.Name, "door"):
   346  				subDir = "door"
   347  			case strings.Contains(image.Name, "motion_detection"):
   348  				subDir = "motion_detection"
   349  			}
   350  
   351  			from := path.Join("data", "icons", subDir, image.Name)
   352  			//log.Infof("copy %s --> %s", from, to)
   353  			common.CopyFile(from, to)
   354  		}
   355  	}
   356  
   357  	return nil
   358  }